我使用 caret
包中的 train
函数调整 randomForest
的 mtry
参数。我的 X
数据中只有 48
列,但是 train
返回 mtry=50
作为最佳值,而这个不是有效值 (>48
)。对此有何解释?
> dim(X)
[1] 93 48
> fit <- train(level~., data=data.frame(X,level), tuneLength=13)
> fit$finalModel
Call:
randomForest(x = x, y = y, mtry = param$mtry)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 50
OOB estimate of error rate: 2.15%
Confusion matrix:
high low class.error
high 81 1 0.01219512
low 1 10 0.09090909
如果我不设置 tuneLength
参数,情况会更糟:
> fit <- train(level~., data=data.frame(X,level))
> fit$finalModel
Call:
randomForest(x = x, y = y, mtry = param$mtry)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 55
OOB estimate of error rate: 2.15%
Confusion matrix:
high low class.error
high 81 1 0.01219512
low 1 10 0.09090909
我不提供数据,因为它是 secret 的。但这些数据没有什么特别之处:每一列都是数值或者是一个因子,并且没有缺失值。
最佳答案
最有可能[1]数据集中的列数与预测变量数之间存在明显差异,如果任何列是因子,则预测变量数可能会不同。您使用了公式方法,它将因子扩展为虚拟变量。例如:
> head(model.matrix(Sepal.Width ~ ., data = iris))
(Intercept) Sepal.Length Petal.Length Petal.Width Speciesversicolor Speciesvirginica
1 1 5.1 1.4 0.2 0 0
2 1 4.9 1.4 0.2 0 0
3 1 4.7 1.3 0.2 0 0
4 1 4.6 1.5 0.2 0 0
5 1 5.0 1.4 0.2 0 0
6 1 5.4 1.7 0.4 0 0
因此,iris
中有 3 个预测变量列,但最终得到 5 个(非截距)预测变量。
最大
[1] 这就是您需要提供可重现示例的原因。通常,当我准备提出问题时,当我花时间写下问题的详细描述时,答案就会变得显而易见。
关于r - 通过插入符调整 mtry 返回奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763977/