【问题标题】:Calculate model calibration during cross-validation in caret?在插入符号交叉验证期间计算模型校准?
【发布时间】:2015-04-06 02:24:57
【问题描述】:

第一次发帖,新手错误请见谅

我在 R 中使用 caret 包进行分类。我在训练集上使用重复的 10 倍交叉验证来拟合一些模型(GBM、线性 SVM、NB、LDA)。使用自定义 trainControl,插入符号甚至可以为我提供一系列模型性能指标,例如 ROC、Spec/sens、Kappa、测试折叠的准确性。这真是太棒了。我还希望有一个指标:模型校准的一些指标。

我注意到插入符号中有一个function,它可以创建一个校准图来估计模型性能在部分数据之间的一致性。在交叉验证的模型构建过程中,是否可以为每个测试折叠计算插入符号?或者它只能应用于我们正在预测的一些新的保留数据?

在某些情况下,目前我有这样的事情:

fitControl <- trainControl(method = "repeatedcv", repeats=2, number = 10, classProbs = TRUE, summaryFunction = custom.summary)
gbmGrid <-  expand.grid(.interaction.depth = c(1,2,3),.n.trees = seq(100,800,by=100),.shrinkage = c(0.01))
gbmModel <- train(y= train_target, x = data.frame(t_train_predictors),
              method = "gbm",
              trControl = fitControl,
              tuneGrid = gbmGrid,
              verbose = FALSE)

如果有帮助,我将使用约 25 个数字预测变量和 N=2,200,预测一个二分类因子。

非常感谢您提供任何帮助/建议。 亚当

【问题讨论】:

    标签: r machine-learning classification r-caret calibration


    【解决方案1】:

    calibration 函数获取您提供的任何数据。您可以从train 子对象pred 中获取重采样值:

    > set.seed(1)
    > dat <- twoClassSim(2000)
    > 
    > set.seed(2)
    > mod <- train(Class ~ ., data = dat, 
    +              method = "lda",
    +              trControl = trainControl(savePredictions = TRUE,
    +                                       classProbs = TRUE))
    > 
    > str(mod$pred)
    'data.frame':   18413 obs. of  7 variables:
     $ pred     : Factor w/ 2 levels "Class1","Class2": 1 2 2 1 1 2 1 1 2 1 ...
     $ obs      : Factor w/ 2 levels "Class1","Class2": 1 2 2 1 1 2 1 1 2 2 ...
     $ Class1   : num  0.631 0.018 0.138 0.686 0.926 ...
     $ Class2   : num  0.369 0.982 0.8616 0.3139 0.0744 ...
     $ rowIndex : int  1 3 4 10 12 13 18 22 25 27 ...
     $ parameter: Factor w/ 1 level "none": 1 1 1 1 1 1 1 1 1 1 ...
     $ Resample : chr  "Resample01" "Resample01" "Resample01" "Resample01" ...
    

    那么你可以使用:

    > cal <- calibration(obs ~ Class1, data = mod$pred)
    > xyplot(cal)
    

    请记住,对于许多重采样方法,单个训练集实例将被多次保留:

    > table(table(mod$pred$rowIndex))
    
      2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17 
      2  11  30  77 135 209 332 314 307 231 185  93  48  16   6   4 
    

    如果愿意,您可以平均每个 rowIndex 的类概率。

    最大

    【讨论】:

    • 噢,谢谢!这就是我的想法-感谢您的解决方案。我想支持您的解决方案,但我还没有任何声誉。再次感谢您的帮助和出色的软件包,Max。
    猜你喜欢
    • 2016-12-07
    • 2020-01-09
    • 2015-04-02
    • 1970-01-01
    • 2023-04-04
    • 2018-10-22
    • 2015-11-11
    • 2014-07-17
    • 2013-12-26
    相关资源
    最近更新 更多