【问题标题】:{caret} how to pass `trainControl` parameters to custom function in r?{caret} 如何将 `trainControl` 参数传递给 r 中的自定义函数?
【发布时间】:2014-09-30 21:34:13
【问题描述】:

我正在处理一个数据框列表 (72),我希望将其用作插入符号 train 的输入,但在使用自定义函数时遇到了问题。我找到了一个潜在的答案here,但它指的是tuneGrid,而不是trainControl。我编写的两个函数都无法将指定的trainControl 参数传递给train:

fun.train.rf <- function(x) {
  ctrl <- trainControl(method = "repeatedcv", repeats = 3)
  train(index ~ .,  data = x, method = "rf", 
        trainControl = ctrl) 
}

model.list <- lapply(list.partition, fun.train.rf)

或:

fun.train.rf <- function(x) {
  train(index ~ .,  data = x, method = "rf", 
        trainControl = list(method="repeatedcv", repeats = 3, p = 0.75)) 
}

model.list <- lapply(list.partition, fun.train.rf)

上面的两个函数都“工作”,但两个返回的模型似乎都忽略了指定的trainControl 参数。当我检查训练模型的结果列表时,两个示例似乎都使用默认训练参数(例如,method = boot):

model.list$modelA$control$method
[1] "boot"
...

这是我第一次真正尝试使用 lapply 和列表​​,所以我假设上述示例可能构思不当,而不是 caret 本身的不足。

如何正确地将trainControl 参数传递给使用插入符号的train 的自定义函数?

【问题讨论】:

    标签: r function lapply r-caret


    【解决方案1】:

    你没有正确传递它。而不是

    trainControl = list(method="repeatedcv", repeats = 3, p = 0.75)
    

    尝试使用

    trControl = trainControl(method="repeatedcv", repeats = 3, p = 0.75)
    

    最大

    【讨论】:

      猜你喜欢
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多