【问题标题】:There are no fitted values for the final model when using train from CARET使用 CARET 中的 train 时,最终模型没有拟合值
【发布时间】:2016-04-24 18:36:21
【问题描述】:

代码如下:

ctrl <- trainControl(method="cv",number = 5, summaryFunction=twoClassSummary, classProbs=T, savePredictions = T, verboseIter = T)
    grid=expand.grid(.trials=c(1,100),.model=c("tree","rules"),.winnow=c(T,F))
    m=train(Category1 ~ ., data = tr.bal,method="C5.0", metric="ROC",trControl=ctrl, tuneGrid=grid)

我认为在 m$finalModel 下会有一个拟合值列。但我没有看到任何东西,或者我错过了什么。那么我如何获得最终模型的预测值。我想计算 ROC therafter。

样本数据如下:

 structure(list(production_year = c(2009L, 2011L, 2011L, 2010L,  2011L, 2010L), movie_sequel = structure(c(1L, 2L, 2L, 2L, 2L,  1L), .Label = c("0", "1"), class = "factor"), creative_type = structure(c(2L,  2L, 2L, 2L, 2L, 2L), .Label = c("other", "mainstream"), class = "factor"),
    source = structure(c(3L, 1L, 1L, 3L, 1L, 1L), .Label = c("based", 
    "other", "Original Screenplay"), class = "factor"), production_method = structure(c(1L, 
    1L, 1L, 1L, 2L, 1L), .Label = c("other", "Live Action"), class = "factor"), 
    genre = structure(c(1L, 2L, 1L, 2L, 2L, 2L), .Label = c("Action", 
    "Adventure", "other", "Comedy", "Drama", "Romantic Comedy", 
    "Thriller/Suspense"), class = "factor"), language = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("other", "English"), class = "factor"), 
    movie_board_rating_display_name = structure(c(3L, 3L, 3L, 
    1L, 3L, 2L), .Label = c("other", "PG", "PG-13", "R"), class = "factor"), 
    movie_release_pattern_display_name = structure(c(7L, 7L, 
    7L, 7L, 7L, 7L), .Label = c("Exclusive", "Expands Wide", 
    "IMAX", "Limited", "Oscar Qualifying Run", "Special Engagement", 
    "Wide"), class = "factor"), Category1 = structure(c(2L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("nothit", "hit"), class = "factor")), .Names = c("production_year",  "movie_sequel", "creative_type", "source", "production_method",  "genre", "language", "movie_board_rating_display_name", "movie_release_pattern_display_name",  "Category1"), row.names = c(NA, 6L), class = "data.frame")

【问题讨论】:

  • @topepo 我想你可以帮忙

标签: r decision-tree r-caret


【解决方案1】:

您可以使用 predict 函数从 caret 包中的 train 类模型中获取拟合值。然后,您可以使用pROC::roc 得出 ROC 曲线。

    p = predict(m)
    curve = pROC::roc(tr.bal$Category1, as.numeric(p))
    plot(curve)

或者作为一个可重现的例子:

    library(caret)
    data(mtcars)

    ctrl <- trainControl(method="cv",number = 5, 
         summaryFunction=twoClassSummary, classProbs=T, 
         savePredictions = T, verboseIter = T)

    grid=expand.grid(trials=c(1,100),
         model=c("tree","rules"),winnow=c(T,F))
    m=train(factor(am) ~ ., data = mtcars,
            method="C5.0",metric="ROC",
            trControl=ctrl, tuneGrid=grid)
    predict(m)
    library(pROC)
    curve = roc(response = factor(mtcars$am), 
                predictor = as.numeric(predict(m)))
    plot(curve)

令人讨厌的是,roc 函数需要一个数字向量而不是一个因子,因此是 as.numeric

【讨论】:

  • 感谢您。是的,你的例子可以预测概率。但是当我做同样的事情时我得到一个错误(选择了未定义的列)或者是因为我的最终模型有规范模型=树,这意味着没有预测。另外我想这一定是模型选择的最终模型!
  • 您的回复是否有数字编码?在我上面的可重现示例中,当响应为 0/1 时,插入符号会引发有关变量名称的警告,该示例禁止概率计算。在将其切换为具有因子标签时,可以毫无问题地计算概率。我还强制最终模型为tree,并且仍然可以生成预测。尝试更改响应的标签,看看是否有效。否则,使用dput 使用包含数据子集的可重现示例更新您的问题,我将再看一遍。这是最终选择的模型。
  • ya 对于数字类它给出了一个错误,所以我将我的响应转换为字符因子类并且它起作用了。我的模型仍然无法正常工作。
  • 如果您使用有助于解决问题的数据示例更新您的问题,请使用 dput(tr.bal[1:100,]) 之类的内容并将结果添加到您的问题中
  • 当您说您的模型不工作时,您的意思是什么?你得到什么样的错误。在您的小型可重现示例中,我遇到了问题,因为感兴趣的变量中只有 "hit" 响应。这给出了一个错误,说There were missing values in resampled performance measures. 这是您在完整数据集上运行时所拥有的吗?还是你得到一个不同的错误?
猜你喜欢
  • 2014-09-13
  • 2020-07-18
  • 2012-05-16
  • 1970-01-01
  • 2021-12-19
  • 2018-07-15
  • 2021-06-16
  • 2017-01-13
  • 1970-01-01
相关资源
最近更新 更多