【发布时间】:2020-08-28 16:31:18
【问题描述】:
我正在尝试计算通过更改 mtry、nodesize 和 ntree 参数创建的多个随机森林的 MSE。我将这些参数用作 randomForest 函数中的变量,并使用这些变量作为索引创建了 3 个“for”循环。我正在尝试将这些 MSE 变量存储在一维数组中并比较结果。我的问题是在代码的最后一行,我尝试在数组中添加 729 个 MSE 值。如何将它们存储在如下的嵌套循环中?
set.seed(425)
toyota_idx =sample(1:nrow(ToyotaCorolla),nrow(ToyotaCorolla)*0.7)
toyota_train = ToyotaCorolla[toyota_idx,]
toyota_test=ToyotaCorolla[-toyota_idx,]
##random forest
forest.mse=rep(0,729)
for (i in 1:9){
for (j in 1:9){
for (k in 1:9){
bag.toyota=randomForest(Price~.,data=toyota_train,mtry=i,nodesize=j,ntree=k,importance =TRUE)
toyota.prediction = predict(bag.toyota ,newdata=toyota_test)
forest.mse <- c(forest.mse, mean((toyota.prediction-toyota_test$Price)^2))
}
}
}
【问题讨论】:
标签: r loops vector random-forest mse