5
$\begingroup$

I got a problem understanding the meaning of the finalModel when using a repeated CV.

ctrl = trainControl(method="repeatedcv", number=10, repeats = 300, savePredictions = TRUE, classProbs = TRUE)
mdl = train("Label~.", data=Data, method = "glm", trControl = ctrl)
pred = predict(mdl, newdata = Data, type="prob")
roc.1 = roc(Data$Label, pred$control)
roc.2 = roc(mdl$pred$obs,mdl$pred$control)

from what I understand :

  1. In roc.1 I tested the "finalModel" of the rCV training on all my database then build the ROC curve associated to this "finalModel".
  2. In roc.2 I build the "average" ROC curve using all the rCV process results.

What I don't get is: what does the "finalModel" represent?

  • Is it a model averaging all the trained models coefficients?
  • Is it correct to use it a predictive model (on both the training dataset and a different set)?
$\endgroup$

2 Answers 2

8
$\begingroup$

For all caret models, the final model is trained on the full dataset. caret::train uses the cross-validation scheme you chose to select model parameters (e.g. mtry for a random forest) and estimate out-of-sample performance of the model. Once the cross-validation is done, caret retrains the model on the full dataset, using the parameters it selected during cross-validation.

So roc.1 is an in-sample roc curve.

The model does not average the trained model's coefficients. It re-fits the model on the full dataset.

It is NOT correct to use the final model on the training data, but it is correct to use on a different dataset.

$\endgroup$
4
  • $\begingroup$ OK, I checked and it seems that you are right the final model is just the regular one ( caret is clearly under-documented ). So to cross validate you really need to add "savePredictions = TRUE, classProbs = TRUE" to your control options and extract manually the results as in roc.3 $\endgroup$
    – Thomas Jvr
    Commented Apr 14, 2016 at 8:15
  • $\begingroup$ @ThomasJvr If you'd bothered to read ?train, you could have figured this out on your own: "The combination with the optimal resampling statistic is chosen as the final model and the entire training set is used to fit a final model. " $\endgroup$
    – Zach
    Commented Apr 14, 2016 at 14:42
  • $\begingroup$ Actually I did, and I'm probably dumb but "The combination with the optimal resampling statistic is chosen as the final model" got me lost. What does it mean ? $\endgroup$
    – Thomas Jvr
    Commented Apr 15, 2016 at 9:34
  • $\begingroup$ @ThomasJvr Sounds like a case of too much documentation! =D. You're using a glm, so that methodology doesn't really apply. A glm is a glm— there are no parameters to tune. However, for OTHER models (e.g a random forest) there are parameters that control the model fit (e.g. mtry). caret tunes the parameters during cross-validation using "grid search," and then fits the final model using the best parameters from the grid search. Since a glm has no parameters, this doesn't really apply, but caret was really designed for models like random forests or gbms that have tunable parameters. $\endgroup$
    – Zach
    Commented Apr 15, 2016 at 12:53
3
$\begingroup$

So finally to summarize :

ctrl = trainControl(method="repeatedcv", number=10, repeats = 300, savePredictions = TRUE, classProbs = TRUE)
mdl = train("Label~.", data=Data, method = "glm", trControl = ctrl)
pred = predict(mdl, newdata = Data, type="prob")
roc.1 = roc(Data$Label, pred$control)
roc.2 = roc(mdl$pred$obs,mdl$pred$control)
roc.3 = roc(as.numeric(mdl$trainingData$.outcome=='case'),aggregate(case~rowIndex,mdl$pred,mean)[,'case'])
  • roc.1 is irrelevant as it evaluates a model on the same data used to train it (the finalModel is just the fit on Data ignoring the CV argument, built to apply on a different dataset for future prediction)
  • roc.2 is 'almost' accurate as it will consider each prediction independently (averaging the prediction, not the probabilities)
  • roc.3 is the correct way to do it as it averages the prediction probabilities for each sample among the repeated CV (contrary to roc.2 where the prediction results are averaged)
$\endgroup$
5
  • $\begingroup$ This seems very much like a request for clarification. It might fit better as a comment on another answer, or an edit to your question? $\endgroup$
    – Silverfish
    Commented Apr 14, 2016 at 9:43
  • $\begingroup$ Silverfish has a point. If you were to elaborate a bit more on how your comments answer your question, that would help. Wrt your roc.1 about the "irrelevance" of the calibration data...don't be so quick to reject that information. Here's a post from Frank Harrell on how calibration information can help in model evaluation... stats.stackexchange.com/questions/179693/… $\endgroup$
    – user78229
    Commented Apr 14, 2016 at 9:46
  • $\begingroup$ Sorry for the mess, not used to the forum's edition guidelines. For the roc.1 it just apply the model on the trained data, the results will always be good as long as there is a separation in the data (t-test <0.05). It can't be used for 'prediction' in an 'external' population. I read your link, but I don't get the point of the 'Calibration' of the model, especially if you already tested your descriptors one by one with t-test or non parametric equivalent. $\endgroup$
    – Thomas Jvr
    Commented Apr 14, 2016 at 10:14
  • $\begingroup$ Based on roc.1, your belief is that "calibration" information is irrelevant. Harrell's comment clearly articulates a different opinion about that. $\endgroup$
    – user78229
    Commented Apr 15, 2016 at 12:30
  • $\begingroup$ I didn't say irrelevant, I said it is irrelevant if you already know that your descriptors are discriminant for the outcome you are learning on (minus the interactions that you normally tested using an ANCOVA previously). Calibration model doesn't have any "predictive" validity because it will be overfitted unless you use bootstrap/CV as Harrell's explained in his post. $\endgroup$
    – Thomas Jvr
    Commented Apr 18, 2016 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.