【发布时间】:2015-02-21 20:37:54
【问题描述】:
我正在拟合一个随机森林,并使用以下代码将我的数据分成训练集和测试集:
train <- sample( 1:nrow(Boston), (nrow(Boston))/2) )
编辑:在这里,train 显然只是一个索引,因此测试集如下:
testB <- Boston[-train,]; head(test); length(test)
响应变量名称为 medy,位于第十四列。
我的随机森林也有以下代码(实际上我在这里装袋,因为我的数据集中的变量总数为 13):
bag.boston1 <- randomForest(medv~., data=Boston, subset=train, mtry=13,
importance=TRUE, ytest=testB$medv, xtest= )
我对 ytest= 选项的论证是否正确?我假设是因为它只是测试数据集中的响应变量。
另外,我应该为 xtest= 选项使用什么参数?
我的一个想法是从我的测试数据集中消除响应变量,从而创建一个仅测试数据集中预测变量的数据框,然后我可以让 xtest 参数成为结果 x 矩阵:
`x <- testB`
x[14] <- NULL # because the 14th column is the response variable
bag.boston1 <- randomForest(medv~., data=Boston, subset=train, mtry=13,
importance=TRUE, ytest=testB$medv, xtest=x)
【问题讨论】:
-
我建议将下面的答案标记为解决方案
标签: r random-forest