在插入符号中拟合随机森林模型后使用 partialPlot
Using partialPlot after fitting a Random Forest model in caret
在我使用 train() 函数拟合 randomForest 之后,我在调用 partialPlot() 和 plotmo() 时遇到问题。这是一些可重现的代码:
library(AER)
library(caret)
data(Mortgage)
fitControl <- trainControl(method = "repeatedcv"
,number = 5
,repeats = 10
,allowParallel = TRUE)
library(doMC)
registerDoMC(cores=10)
Final.rfModel <- train(form=networth ~ ., data=Mortgage, method = "rf", metric='RMSE', trControl = fitControl, tuneLength=10, importance = TRUE)
#### partial plots fail
partialPlot(Final.rfModel$finalModel, Mortgage, "liquid")
library(plotmo)
plotmo(Final.rfModel$finalModel)
某些函数(包括 randomForest
和 train
)处理虚拟变量的方式有些不一致。 R 中使用公式方法的大多数函数会将因子预测变量转换为虚拟变量,因为它们的模型需要数据的数字表示。例外情况是基于树和规则的模型(可以根据分类预测变量进行拆分)、朴素贝叶斯和其他一些模型。
因此,当您使用 randomForest(y ~ ., data = dat)
时,randomForest
将 不会 创建虚拟变量,但 train
(以及大多数其他人)将使用类似的调用train(y ~ ., data = dat)
。
错误的发生是因为 rate
、married
和其他一些预测变量是因素。 train
创建的虚拟变量名称不同,因此 partialPlot
无法找到它们。
将非公式方法与 train
一起使用会将因子预测变量传递给 randomForest
,一切都会起作用。
TL;DR
在这种情况下使用train
的非公式方法:
Final.rfModel <- train(form=networth ~ ., data=Mortgage,
method = "rf",
metric='RMSE',
trControl = fitControl,
tuneLength=10,
importance = TRUE)
最大
在我使用 train() 函数拟合 randomForest 之后,我在调用 partialPlot() 和 plotmo() 时遇到问题。这是一些可重现的代码:
library(AER)
library(caret)
data(Mortgage)
fitControl <- trainControl(method = "repeatedcv"
,number = 5
,repeats = 10
,allowParallel = TRUE)
library(doMC)
registerDoMC(cores=10)
Final.rfModel <- train(form=networth ~ ., data=Mortgage, method = "rf", metric='RMSE', trControl = fitControl, tuneLength=10, importance = TRUE)
#### partial plots fail
partialPlot(Final.rfModel$finalModel, Mortgage, "liquid")
library(plotmo)
plotmo(Final.rfModel$finalModel)
某些函数(包括 randomForest
和 train
)处理虚拟变量的方式有些不一致。 R 中使用公式方法的大多数函数会将因子预测变量转换为虚拟变量,因为它们的模型需要数据的数字表示。例外情况是基于树和规则的模型(可以根据分类预测变量进行拆分)、朴素贝叶斯和其他一些模型。
因此,当您使用 randomForest(y ~ ., data = dat)
时,randomForest
将 不会 创建虚拟变量,但 train
(以及大多数其他人)将使用类似的调用train(y ~ ., data = dat)
。
错误的发生是因为 rate
、married
和其他一些预测变量是因素。 train
创建的虚拟变量名称不同,因此 partialPlot
无法找到它们。
将非公式方法与 train
一起使用会将因子预测变量传递给 randomForest
,一切都会起作用。
TL;DR
在这种情况下使用train
的非公式方法:
Final.rfModel <- train(form=networth ~ ., data=Mortgage,
method = "rf",
metric='RMSE',
trControl = fitControl,
tuneLength=10,
importance = TRUE)
最大