How to use PCA for prediction? I have some doubts regarding PCA. I have 318 observations with 10 variables, and one response, which is the dataset I want to use for training and building the model. I did the PCA and found that the first 5 PCs account for 96% of the variance, so I plan to use these 5 for the model. In Matlab help the regression is done as follows: 
betaPCR = regress(y-mean(y), PCAScores(:,1:5)); 

But the coefficients are then transformed to the original uncentered variables:
betaPCR = PCALoadings(:,1:5)*betaPCR;
betaPCR = [mean(y) - mean(X)*betaPCR; betaPCR];
yfitPCR = [ones(n,1) X]*betaPCR;

My question is: How can I find the model and how can I apply it to new, test dataset (100 observations with 10 variables and one response)? Should I use the transformed coefficients, and would the predicted y be centered? How can I use the calculated betaPCR on new data?
 A: It seems that you have 418 cases, divided into a training set of 318 cases and a test set of 100 cases. I'll answer your question and suggest a closely related but potentially better approach to your problem. 
As noted on the MATLAB help page, for PCR it's best if the predictors are both centered and scaled to unit variance so that differences in scales don't unduly weight the results. They didn't do that in their example, but if your predictors are on different scales then you might consider scaling in addition to centering.
The code you adapted from that page apparently returns the regression to the original scales of both $x$ and $y$, according to how the plot on that page was obtained (although my memory of MATLAB syntax is too rusty to verify that directly). Replacing the $X$ in the last line of code with your matrix of test data should give you the predicted $y$ values for those test data. To verify, try using a small sample of your original data for $X$ and see if the predictions make sense.
But PCR with a training set and a separate test set might not be the best approach to your problem. PCR picks the principal components that capture the most variance in the predictor variables, but not necessarily those most related to the response variable. The partial least squares approach illustrated on the same help page can be better related to outcomes. Also, the separation into training and test sets, if that's what you've done, doesn't efficiently use all the information in your data.
Ridge regression, provided by the ridge function in MATLAB, is essentially PCR but with different weights placed on the components instead of the all-or-none selection in PCR. Large regression coefficients that don't help much with predicting outcomes are penalized. This helps bring the coefficients better in line with relations to the outcome variable and helps correct for overfitting. You can start with all of your data to set up the model (if my understanding of what you've done is correct), then use cross validation or bootstrapping to choose the penalty that minimizes prediction error.
A: Here are some ways to test whether your understanding of the calculations is correct:  


*

*Take a few of the training cases and calculate the prediction as you think. Then compare with the fitted values from the help page.

*If you use the full PCA model (all loadings), the PCA performs only a rotation of the data. The predictions based on all scores should match the predictions by least squares regression on the original data.

*You can do the PCR in one or two steps: to find out whether your one-step prediction directly from original data to the fitted value is correct, compare it with the two step procedure of first calculating scores for the new data followed by predicting y from the scores. 


Note that if the PCA fitting routine centers (and possibly scales) the data, you need to do that with your new cases as well. And yes, if the regression is set up to predict y - mean (y), then the predicted values will be centered by the mean of the ys. You then need to center and possibly scale X by the center and scaling calculated on the training X, the same applies to y. 
A: I am not a Matlab user, but you are approaching the problem from the wrong angle. PCA should not be used to help with overfitting - regularization is a proper tool for it. This way you are not throwing data away, and you will most likely end up with a model that is easier to explain (e.g. feature importance or weights, for your original variables, not transformed ones).
