【问题标题】:GAM method without resampling in caret produces stop error没有在插入符号中重新采样的 GAM 方法会产生停止错误
【发布时间】:2016-12-08 18:55:17
【问题描述】:

我在 lapply 中编写了一个函数来为数据帧中响应变量向量中的每个元素拟合 GAM(带样条曲线)。我选择使用caret 来拟合模型,而不是直接使用mgcvgam 包,因为我希望最终将我的数据拆分为训练/测试集以进行验证并使用各种重采样技术。现在,我只是将trainControl 方法设置为'none',如下所示:

  # Set resampling method
  # tc <- trainControl(method = "boot", number = 100)
  # tc <- trainControl(method = "repeatedcv", number = 10, repeats = 1)
  tc <- trainControl(method = "none")

  fm <- lapply(group, function(x) {
  printFormula <- paste(x, "~", inf.factors)
  inputFormula <- as.formula(printFormula)
  # Partition input data for model training and testing
  # dpart <- createDataPartition(mdata[,x], times = 1, p = 0.7, list = FALSE)
  # train <- mdata[ data.partition, ]
  # test <- mdata[ -data.partition, ]
  
  cat("Fitting:", printFormula, "\n")
  # gam(inputFormula, family = binomial(link = "logit"), data = mdata)
  train(inputFormula, family = binomial(link = "logit"), data = mdata, method = "gam",
        trControl = tc)
})

当我执行此代码时,我收到以下错误:

Error in train.default(x, y, weights = w, ...) : 
  Only one model should be specified in tuneGrid with no resampling

如果我在调试模式下重新运行代码,我可以找到caret 停止训练过程的位置:

if (trControl$method == "none" && nrow(tuneGrid) != 1) 
    stop("Only one model should be specified in tuneGrid with no resampling")

显然train 函数由于第二个条件而失败,但是当我查找tuning parameters for a GAM(带样条线)时,只有一个特征选择选项(不感兴趣,我想将所有预测变量保留在模型)和方法。因此,当我调用train 时,我不包括tuneGrid 数据框。这就是模型以这种方式失败的原因吗?我将提供什么参数以及 tuneGrid 的外观?

我应该补充一点,当我使用 bootstrapping 或 k-fold CV 时,模型已经成功训练,但是这些重采样方法需要更长的时间来计算,我还不需要使用它们。

对于这个问题的任何帮助将不胜感激!

【问题讨论】:

    标签: r r-caret training-data gam


    【解决方案1】:

    对于该模型,调整网格会查看 select 参数的两个值:

    > getModelInfo("gam", regex = FALSE)[[1]]$grid
    function(x, y, len = NULL, search = "grid") {
       if(search == "grid") {
          out <- expand.grid(select = c(TRUE, FALSE), method = "GCV.Cp")
       } else {
          out <- data.frame(select = sample(c(TRUE, FALSE), size = len, replace = TRUE),
                             method = sample(c("GCV.Cp", "ML"), size = len, replace = TRUE))
       }
        out[!duplicated(out),]
     }
    

    您应该使用tuneGrid = data.frame(select = FALSE, method = "GCV.Cp") 之类的东西来仅评估单个模型(如错误消息所述)。

    【讨论】:

    • 我能够使用您建议的调整参数成功拟合 GAM 模型。我知道功能选择被禁用并查找 GCV.Cp 我看到它已传递给 mgcv 包中的 gam 函数。将来,用户可以从哪些来源咨询调整参数的选项?在 GitHub 网站上,我认为文档至少可以将用户链接到调整参数的潜在选项。那只是我的两分钱。感谢您的帮助,我相信我将来会有更多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2016-01-26
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多