1
$\begingroup$

Based on this answer, and following the Applied Predictive Modeling text, I am running the following 10-fold cross-validated PCR analysis:

library(AppliedPredictiveModeling) # data
library(caret) # train, cv
library(pls) # pcr function

data(solubility)

# cross-validation setup

set.seed(1)
cv_index <- createFolds(solTrainY, returnTrain = TRUE)
cv_control <- trainControl(method = "cv", index = cv_index)

# run model

set.seed(1)
PCR_Train <- train(solTrainXtrans, y = solTrainY,
                   method = "pcr",
                   tuneGrid = expand.grid(ncomp = 89),
                   trControl = cv_control
)

I have created 10 folds of my data, calculated 89 unsupervised components (independently for each fold), and then performed a regression.

I know I can apply this to my test set to get predicted values using the following code:

predict(PCR_Train, solTestXtrans)

which I would then compare to solTestY.

This is a bit "black-box-y" for me. Specifically, I am wondering what PCA coefficients should be used for my test set. Since I ran PCA for each of the 10 folds, the weights of the 10 sets should be different. I know it is not based on the test data, otherwise something like predict(PCR_Train, solTestXtrans[1, ]) would not return the same value?

Do we just randomly take one of the folds, or does it combine the results somehow?

$\endgroup$
6
  • 3
    $\begingroup$ Possible duplicate of How to choose a predictive model after k-fold cross-validation? $\endgroup$
    – amoeba
    Commented Jan 17, 2017 at 10:25
  • $\begingroup$ @amoeba thanks, that answer is helpful - however, that answer talks about how to build your final model. What do we use for the test set? $\endgroup$
    – Chris
    Commented Jan 17, 2017 at 14:02
  • $\begingroup$ The final model is what you use for the test set. I am not sure what is unclear, can you be more specific? $\endgroup$
    – amoeba
    Commented Jan 17, 2017 at 14:03
  • $\begingroup$ @amoeba I think I am getting confused by two "final models". Is there (A) a final model with the full training data that we use on the test set, and (B) a final model with training + test data that we use to make subsequent predictions? And we use folds to evaluate (A)? $\endgroup$
    – Chris
    Commented Jan 17, 2017 at 14:30
  • $\begingroup$ You have a training set on which you run 10-fold CV to find optimal values of hyperparameters. Then you use the whole training set and these optimal values to train Final Model A. You apply it to the test set (that was not part of the training set). If you then want to make subsequent predictions, then yes, you would train Final Model B on the all available data (training set + test set). Does that make sense? This is called "nested cross-validation". $\endgroup$
    – amoeba
    Commented Jan 17, 2017 at 14:35

1 Answer 1

4
$\begingroup$

You are doing the 10-fold CV to find the optimal hyper parameters of your model, so you just fit it again on all your data with the optimal parameters you just found. Your predict function is probably doing it automatically. When in doubt read the documentation e.g: ??caret

$\endgroup$
0

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.