【问题标题】:Getting error "variables in the training data missing in newdata" in R在 R 中获取错误“训练数据中的变量在 newdata 中丢失”
【发布时间】:2015-05-20 11:02:25
【问题描述】:

当我在 R 中对数据集执行预测时,出现以下错误:

Mode.rf <- randomForest(trX,trYc,ntree=1000,proximity=TRUE)
trts = rbind(trX,ts)
Plu.pred <- predict(Mode.rf,trts,proximity=TRUE)

predict.randomForest 中的错误(Mode.rf,trts,proximity = TRUE): newdata 中缺少训练数据中的变量

以下细节描述了前几个样本的数据。

trts #In total, 1376 observations out of 95 variables

     V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
1     0  0  0  0  0  0  0  0  1   0   0   0   0   0   0   0   1   0   0   0   0
2     0  0  0  1  0  0  0  0  0   0   0   0   0   0   0   0   0   1   1   1   0 

Mode.rf

# Call:
#  randomForest(x = trX, y = trYc, ntree = 1000, proximity = TRUE) 
#                Type of random forest: classification
#                      Number of trees: 1000
# No. of variables tried at each split: 9

#         OOB estimate of  error rate: 80.29%
# Confusion matrix:
#     1  2   3  4 class.error
# 1  74 85 129 99   0.8087855
# 2 106 84 105 58   0.7620397
# 3 125 90  51 76   0.8508772
# 4 114 45  72 62   0.7883959

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您的ts 数据中可能有不在trX 中的类。

标签: r random-forest missing-data


【解决方案1】:

根据您分享的内容,您似乎是:

1:建立一个提供自变量 (trX) 和 1 个因变量 (trYc) 的模型

这对我来说很好

2:将训练集中的自变量行绑定到其他东西

如果 ts 是一组自变量,我将它们绑定到旧数据是没有意义的。最好自己测试新数据集。 如果 ts 是一个新的因变量,您可能会使用 cbind 而不是 rbind(并且错误地预测不匹配旧数据的新结果)。

如果目标是在新数据集中使用您的模型,请确保为新数据集提供与第一个完全相同的列名。

您还可以将因变量和自变量放在 1 个数据框中,并使用 randomForst 中的公式选项(Y ~ .、data=dataname 等)

“。”表示数据框中的所有其他变量。

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 2018-07-07
    • 2019-10-07
    • 2021-10-06
    • 2013-08-17
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多