14
$\begingroup$

I am a bit confused concerning how I should perform cross-validation to evaluate a statistical learning model.

If I have a data set of 500 observations, should i divide it in a train and test set with for example 375 (75%) train observations and 125 (25%) test observations, and perform cross-validation on the train set? Or should I perform the cross-validation on the entire data set?

So long as the aim of performing cross-validation is to acquire a more robust estimate of the test MSE, and not to optimize some tuning parameter, my understanding is that you should use the entire data set. The reason for this is that you would not acquire a model that you can use to predict on unseen test observations, just a measure of MSE for the train set where cross-validation is performed.

If I am mistaken, how could I use the cross-validation result to predict out of sample observations?

Could someone be kind and clarify this for me?

If relevant, the problem I am solving is performing cross-validation to assess the model performance of a random forest model in R. Thanks in advance!

$\endgroup$
1
  • $\begingroup$ What do you mean by 'If I am mistaken, how could I use the cross-validation result to predict out of sample observations?' I'm afraid I've read this a couple of times and can't be sure what you mean. Do you mean 'how can I use CV result to estimate performance beyond my experimental set?' $\endgroup$
    – ReneBt
    Commented Apr 11, 2018 at 8:58

3 Answers 3

11
$\begingroup$

should i divide it in a train and test set with for example 375 (75%) train observations and 125 (25%) test observations, and perform cross-validation on the train set?

Yes

Or should I perform the cross-validation on the entire data set?

No

The test set should be handled independently of the training set so you could do a separate CV block for the test set if you really wish, and may provide some useful insight but is not universal practice. CV may be useful if you plan to apply the model to a completely new set of ‘real world’ data. Given that the test set is drawn from the same population as the training set this may not be that useful as you would expect it to have similar charcateristics to the training set if the split was performed correctly and without bias. Mind you, may be worth checking this assumption.

What is the purpose of CV?

So long as the aim of performing cross-validation is to acquire a more robust estimate of the test MSE

This is not the purpose of CV, rather it is to estimate the robustness of your performance metrics. As @user86895 states it does not measure MSE, see Mean squared error versus Least squared error, which one to compare datasets? for further reading. CV creates multiple models on subsets of the data and applies them to the data withheld from that subset. It iterates over the dataset, building new models until all have been included in training subsets and all have been included in test subsets. The final model is built on all the training set not any of the individual CV round models, the purpose of CV is not to build models but to assess stability of the model performance, i.e. how generalisable the model is.

When comparing different data processing or analysis algorithms on a dataset it provides a first filter to identify the work pathways that provide the most stable models. It does this by providing estimates of how variable the performance is between sub-sets of your training set. This allows you to detect models with a very high risk of overfitting and filter them out. Without cross validation you would be picking based solely on the maximum performance without concern to its stability. But when you come to apply a model in a deployed situation its stability (relevance across the real world population) will be more important than moderate differences in raw performance on a subset of curated samples (i.e you original experimental set).

Cross validation is in fact essential for choosing the crudest parameters for a model such as number of components in PCA or PLS using the Q2 statistic (which is R2 but on the held out data, see What is the Q² value for each component of a PCA) to determine when overfitting starts to degrade model performance.

If I am mistaken, how could I use the cross-validation result to predict out of sample observations?

I am taking this to mean 'how can I use CV result to estimate performance beyond my experimental set?', but will update this section of my answer if it is clarified differently.

CV is used as a first line estimate of model stability, not to estimate performance in real world settings. The only way to do this is to test the final model in a real-world situation. What CV does is provide you a risk analysis, if it appears stable then you could decide it is time to risk the model on a real-world test. If it is not stable then you need to probably expand your training set considerably (ensuring an even representation of important sub groups and confounding factors as these are one source, other than random noise, of overfitting as all relevant variation needs to be given an equal exposure to the model building process to be properly weighted for) and build a new model.

And a note on real world validation, if it works it doesn’t prove your model is generalisable, only that it works under the specific mechnisms whereby it has been deployed in the real-world.

$\endgroup$
1
  • $\begingroup$ Hi - this is a very useful answer. So it seems like the preferred workflow is: 1) Split into train/test 2) Use train to train 3) -OPTIONAL- Use crossvalidation on train to tune hyperparameters, then re-train on train 4) Use test to score 5) Cross validate on train to assess the stability of scores you got from test 6) If not satisfied, start over 7) If satisfied, train the model with the entire available dataset (i.e train + test) and deploy Is this correct? $\endgroup$ Commented Aug 2, 2021 at 20:56
8
$\begingroup$

So long as the aim of performing cross-validation is to acquire a more robust estimate of the test MSE, and not to optimize some tuning parameter, my understanding is that you should use the entire data set.

Correct.

The reason for this is that you would not acquire a model that you can use to predict on unseen test observations, just a measure of MSE for the train set where cross-validation is performed.

Actually you acquire a measure of MSE on test data, not train set

If I am mistaken, how could I use the cross-validation result to predict out of sample observations?

Update:

CV as an evaluation technique provides one output: an evaluation. It does not provide a model that you can use.

CV as a tuning technique provides one output: a model (or component of a model such as the best hyperparameter). It does not provide an evaluation. Hence, the evaluation must be made on an independent test set, which is why data is held out.

In this question "cross-validation to evaluate a statistical learning model", CV I think is intended for evaluation (right?), not tuning. So, you would not use the 10 models from the folds in any way; they are not real models, they are simply byproducts of the evaluative process.

That probably does not answer your question, however. I am guessing you really wanted to know: so, now I have an evaluation, but now I get some new data and want to make predictions. What do I use to make predictions? In other words, you have an evaluation, but not a model. The answer is that you must re-train on the entire dataset. It is this model to which your CV evaluation corresponds.

$\endgroup$
4
  • $\begingroup$ Thanks for answering! To claify: If I were to somehow use the results from the cross-validation on new data, how could you do that? Because the 10 fits resulting from a 10 fold cv for instance are very different, and you are not left with a "cv model" if i understand correctly. The reason im asking, is because i often see that people perform cv on a training set, while additionally have a held out test set. Im confused about what the purpose of this held out test set then is. $\endgroup$
    – Kaangy
    Commented Apr 10, 2018 at 19:25
  • $\begingroup$ That makes sense. "CV as a tuning technique provides one output: a model. It does not provide an evaluation. Hence, the evaluation must be made on an independent test set, which is why data is held out." Doesn't the CV as a tuning technique provide only the optimal tuning value, e.g. number of K neighbors, which you then will apply to the training set, but not a model in itself? $\endgroup$
    – Kaangy
    Commented Apr 10, 2018 at 19:48
  • $\begingroup$ Technically yes it provides the performance of the model under k. Then you should take the best k and retrain on the full training set. $\endgroup$ Commented Apr 10, 2018 at 20:03
  • $\begingroup$ Is it always true that the best tuning params found through CV are really the best when model is finally fitted to all dataset ? $\endgroup$
    – Rodolphe
    Commented Feb 2, 2020 at 10:26
2
$\begingroup$

If I have a data set of 500 observations, should i divide it in a train and test set with for example 375 (75%) train observations and 125 (25%) test observations, and perform cross-validation on the train set?

Yes, you should do it as the initial step, regardless of the situation (test_size=0.2 is also a reasonable default in sklearn).

Or should I perform the cross-validation on the entire data set?

Try to avoid it as much as possible and to stick to the traditional guidelines, but what most guides and books don't tell you is that YOU MAY HAVE TO.
There is theory, and then there is practice... If the labeled data set is small, if there is no practical way to increase it and there is a high risk of the training set being non-representative to a large extent, you may have to give up a separate test set.

Before you launch into non-standard analytic pathways, however, you need to ensure that you understand what the risks associated with them are and the impact on your conclusions. Ultimately, the decision is a pragmatic one based on the needs and the risks associated with the project. In regulated fields, such as finance or healthcare, you need to be cautious, but if all the outcome is whether to colour a 'Buy' button green or blue then the risks are lower.

What you will lose by following this non-standard analytic pathway:

  1. you will be giving up the second round of "generalization testing" (with CV on the training set for model selection/tuning being the first one)
  2. you will be exposing yourself to the risk of overfitting your data by having no hold out to check whether the model is stable or not when applied to new data
  3. you will be performing more hypothesis tests, leading to a higher risk of making a false discovery (related to point 2)
  4. you will exploit more researcher degrees of freedom (garden of forking paths) - another route to overfitting

What you will gain:

  1. you will be gaining the confidence that your model is trained on the more representative samples
  2. you will have a deeper insight into your dataset, hopefully including biases, errors, and the desired data generating processes
  3. you will be better placed to recommend improved plans, should any opportunity arise to redo the whole thing

So, with the understanding of the above risks and benefits, how does one make a decision on giving up a separate test set?
Here's a suggested workflow:

1) After selecting and tuning an algorithm using the standard method (training CV + fit on the entire training set + testing on the separate test set), go back to the train/test split, split the data set differently a few times (e.g. using a different random_state parameter value in scikit-learn), each time re-executing the "training CV + train set fit + testing" cycle and observing the scores.

2) If the difference between the training CV scores and the testing score is relatively consistent - great! No need to do anything else.

3) If, however, the difference between the mean training CV score and the testing score is inconsistent, and especially if the fact which one is larger varies between these random data set splitting iterations - YOU'VE GOT A SAMPLING PROBLEM.
Potential solutions:

a) Find another algorithm that will generalize well to the testing set regardless of the train/test split (this may be very challenging, given that you're probably working with a small labeled data set to begin with).

b) Identify and eliminate the splitting bias systematically (this may be not practical either).

c) Keep acquiring more representative labeled data until this problem goes away.

d) Declare this being a "failed analysis" and postpone/cancel model launch, since getting rid of the separate round of generalization testing has high analytic risks associated with it.

e) Only if all else fails, give up a separate test data set (i.e. cut out the separate generalization testing step from the process) and select the model via the cross-validation on the entire labeled data set.
Observe not just the mean CV test score (which is the mean of the fold means) when making model selection/tuning decisions, but also min/max/std of the mean test scores for the individuals folds.

$\endgroup$
7
  • $\begingroup$ -1 because your answer recommends exploring the garden of forking paths with no detailed caveats of the risk associated with that. Poking repeatedly at one data set has a very high risk of over fitting and over interpretation. You need to clarify that if the test set is inadequate then the original study has failed, any further work on the full dataset is exploratory and non confirmatory, useful for future study design etc, but not able to rescue a failed analysis. $\endgroup$
    – ReneBt
    Commented Feb 1, 2020 at 19:46
  • $\begingroup$ That is a good point. Refusing to train and launch a model into production based on the inadequacy of data is an option that needs to be considered.<br>However, when that is not an option, I'd still go with selecting the model based on CV on the full data set as a replacement for the random unsystematic split between the train and test sub-sets... The lesser of two evils. $\endgroup$
    – Gene M
    Commented Feb 3, 2020 at 13:41
  • $\begingroup$ Your edits improved it a bit, a lot less gung-ho so I'll remove the downvote. I've submitted a suggested edit from how I see it, hope it is useful otherwise ignore it $\endgroup$
    – ReneBt
    Commented Feb 3, 2020 at 14:23
  • $\begingroup$ I think the edits DO add good insights, @ReneBt, so I put them in. Thank you. $\endgroup$
    – Gene M
    Commented Feb 4, 2020 at 17:10
  • 1
    $\begingroup$ Yes, it DOES seem like a better solution, but then we're exposing ourselves more to the risk of over-fitting and producing a model that generalizes poorly in production. The 2nd round of generalization testing on the test set is INTENTIONALLY not fully systematic, in contrast to the CV on the training set (1st round). Please read the "What you will lose ..." section (which I got help with from @ReneBt). In Step 1 of the proposed workflow I am merely suggesting to poke at the generalization performance. $\endgroup$
    – Gene M
    Commented May 1, 2020 at 17:24

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.