[英]Levels of Variable and Random Forest
Consider a data set train : 考虑一个数据集训练 :
z a
1 1
0 2
0 1
1 3
0 1
1 2
1 1
0 3
0 1
1 3
with a binary outcome variable z and a categorical predictor a with three levels: 1,2,3. 具有二元结果变量z和具有三个级别的分类预测变量a :1、2、3。
Now consider a data set test : 现在考虑一个数据集测试 :
z a
1
1
2
1
2
2
1
When I run the following code: 当我运行以下代码时:
library(randomForest)
set.seed(825)
RFfit1 <- randomForest(z~a, data=train, importance=TRUE, ntree=2000)
RFprediction1 <- predict(RFfit1, test)
I get the following error message: 我收到以下错误消息:
Error in predict.randomForest(RFfit1, test1) :
Type of predictors in new data do not match that of the training data.
I am assuming this is because the variable a in the test data set does not have three levels. 我假设这是因为测试数据集中的变量a没有三个级别。 How would I fix this?
我该如何解决?
您必须为其分配与火车相同的级别
test$a <- factor(test$a, levels=levels(train$a))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.