我正在尝试为价格预测问题建立随机森林模型。我已经完成了以下步骤:
1)将数据分成3组训练,测试和有效(不仅分为训练和测试,还需要分成3套)
set.seed(1234)
assignment <- sample(1:3, size = nrow(train), prob = c(0.7, 0.15, 0.15), replace = TRUE)
#Create a train, validation and tests from the train data
train_train <- train[assignment == 1, ]
train_valid <- train[assignment == 2, ]
train_test <- train[assignment == 3, ]
2)我用火车集中的x和y建立了模型
fit_rf_train <- train(x = train_train[, -which(names(train_train) %in%
c("Item_Identifier", "Item_Outlet_Sales"))],
y = train_train$Item_Outlet_Sales,
method = "ranger",
metric = "RMSE",
tuneGrid = expand.grid(
.mtry = 6,
.splitrule = "variance",
.min.node.size = c(10,15,20)),
trControl = trControl,
importance = "permutation",
num.trees = 350)
对于同一火车数据上的模型输出,我有以下屏幕截图:
3)使用预测函数,我将模型与其他两个数据集一起使用,并使用以下代码行进行了测试:
prediction_test <- predict(fit_rf_train, train_test)
prediction_valid <- predict(fit_rf_train, train_valid)
我的问题是如何在看不见的数据(测试和有效)上衡量模型的性能?
答案 0 :(得分:1)
如果您想坚持使用caret
,则可以执行以下操作:
library(caret)
trainda<-createDataPartition(iris$Sepal.Length,p=0.8,list=F)
valid_da<-iris[-trainda,]
trainda<-iris[trainda,]
ctrl<-trainControl(method="cv",number=5)
set.seed(233)
m<-train(Sepal.Length~.,data=trainda,method="rf",metric="RMSE",trControl = ctrl,verbose=F)
m1<-predict(m,valid_da)
RMSE(m1,valid_da$Sepal.Length)
结果:
[1] 0.3499783