I followed the instructions from this open Stanford lecture on PCR.
I have a couple of questions, but first I'll post the code with my comments.
#Principal Components Regression library('pls') #split data in half train <- sample(nrow(Boston_Ready), (nrow(Boston_Ready)/2)) #calculates pcr and uses cross-validation to determine the number of components pcr.fit <- pcr(medv ~., data=Boston_Ready, subset=train, scale=TRUE, validation="CV") summary(pcr.fit) #plots MSE by number of components validationplot(pcr.fit, val.type='MSEP') #choose number of components from the validation plot pcr.pred <- predict(pcr.fit, Boston_Ready[-train,], ncomp=5) #Test MSE for the model sqrt(mean((pcr.pred - Boston_Ready[-train,]$medv)^2)) #train on the whole dataset with the right number of components pcr.fit_full <- pcr(medv ~., data=Boston_Ready, ncomp = 5, scale=TRUE)
My goal is to compare whether PCR creates a better model than linear regression.
I have a couple of questions. I hope that's alright.
1- Splitting my data into a train and test set seems costly. It is a small set with 500 rows and 14 variables.
Could I run PCR with all of the data and then approximate test error with cross validation?
2- Are there other visuals that are helpful for understanding PCR? I looked at the loadings to get an understanding of the components.
3- Which is my final model? Is it
I ask because some tutorials included a part where they take the principal components and run a linear regression model where the components are the predictors.