r - 通过插入符调整 mtry 返回奇怪的值

标签 r random-forest r-caret

我使用 caret 包中的 train 函数调整 randomForestmtry 参数。我的 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/

相关文章:

r - 用deqn和roxygen记录方程式

r - 我应该如何处理多项选择/响应(检查所有适用的)数据?

python - 在网状中找不到 sklearn (LocalOutlierFactor) 的私有(private)函数

python - 从 sklearn 随机森林回归器可视化决策树

r - 为什么函数参数名称需要用 pmap 匹配列表名称?

machine-learning - 为什么在决策树中使用交叉熵而不是0/1损失

python - 如何在 Python scikit-learn 中输出随机森林中每棵树的回归预测?

r - 使用插入符号调整参数

r - Shiny 的应用程序产生错误 : "arguments imply differing number of rows: 0, 1"

r - 使用插入符号和data.table创建数据分区