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?