我想使用gg_variable
包中的ggRandomForests
函数为我的模型创建部分依赖图的一个方面。我有以下内容,但不起作用。
我该怎么做?
library("caret")
library("ggRandomForests")
library("randomForest")
data("iris")
iris$Species<-NULL
control = trainControl(method="cv", number=5,savePredictions = TRUE)
in_train= createDataPartition(iris$Sepal.Length, p=.66, list=FALSE)
train_st=iris[in_train,]
test_st=iris[-in_train,]
trf_sep = train(Sepal.Length ~ .,
data=train_st,ntree=800,method="rf",metric="Rsquared",trControl=control,importance = TRUE)
gg_variable(trf_sep)#Here is the problem
gg_variable
要求从randomForest
模型输出。它不适用于caret::train
功能的输出。在这种情况下,您可以使用train
程序包中的caret
函数来调整mtry并使用带有调整后的mtry的randomForest
程序包来拟合随机森林模型,然后在[like]上应用gg_variable
library("caret")
library("ggRandomForests")
library("randomForest")
data("iris")
iris$Species<-NULL
control = trainControl(method="cv", number=5,savePredictions = TRUE)
in_train= createDataPartition(iris$Sepal.Length, p=.66, list=FALSE)
train_st=iris[in_train,]
test_st=iris[-in_train,]
trf_sep = train(Sepal.Length ~ .,
data=train_st,ntree=800,method="rf",metric="Rsquared",trControl=control,importance = TRUE)
try <- randomForest(Sepal.Length ~ .,
data=train_st,ntree=800, mtry=3)
gg_dta <- gg_variable(try)
plot(gg_dta, xvar="Sepal.Width")
最终模型存储在train
对象中,并且属于RandomForest类。除非它没有调用整个数据集。