{caret}如何将`trainControl`参数传递给r中的自定义函数?

时间:2021-12-05 18:00:47

I'm working with a list of dataframes (72) which I wish to use as input to caret's train, but I'm running into problems when using a custom function. I've found a potential answer here, but it refers to tuneGrid and not trainControl. Both functions I've written fail to pass the specified trainControl parameters to train:

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

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)

or:

要么:

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)

Both functions above "work", but both return models that appear to ignore the specified trainControl parameters. When I examine the resulting list of trained models, both examples appear to be using the default training parameters (e.g., method = boot):

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

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

This is my first real attempt at using lapply and lists, so I'm assuming the above examples are likely ill-conceived rather than a shortfall of caret itself.

这是我第一次真正尝试使用lapply和list,所以我假设上面的例子很可能是错误的,而不是插入本身的缺陷。

How can I properly pass the trainControl parameters to a custom function using caret's train?

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

1 个解决方案

#1


1  

You didn't pass it in correctly. Instead of

你没有正确传递它。代替

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

try using

尝试使用

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

Max

马克斯

#1


1  

You didn't pass it in correctly. Instead of

你没有正确传递它。代替

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

try using

尝试使用

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

Max

马克斯