PCA on train and test datasets: do I need to merge them? I have train and test data set to work on. I would like to apply PCA to reduce dimension. 
Do I need to merge train and test data sets together before applying PCA? Or I should apply PCA on train data set, select reduced dimension, and work on reduced data set for both train and test data set?
 A: Principal component analysis will provide you with a number of principal components $W$; these components will qualitatively represent the principal and orthogonal modes of variation in your sample. You will use (some) of these $W$ to project your original dataset $X$ to a lower dimensional subspace $T$. This is your new dataset and the PCs are in effect an axis system over which we can represent data $X$ in a compact form.
Now, as @RobertKubrick mentions you need to make sure that information from your testing dataset is not "leaked" into your training dataset. If this takes place then you will utilize information that "should be unknown" during your prediction; your error estimates will be wrong. The generalization of your model will suffer.
For your case in particular you should do the following: Calculate the principal components $W$s on the training dataset and then utilize the training sample $W$ to reduce the dimensions of the testing dataset. I say this because: 


*

*if you merged both your training and testing dataset to calculate your PC you will evidently utilize information from the testing set. This is clearly wrong.

*If you did two independent PCAs you will be comparing data registered on different axes (if anything princ. components are not sign-identifiable so the estimated parameters from them will also have the same issue). The axes over which you project your data should be the same, otherwise you are in a typical "orange-apples situation".


Clearly if you do $k$-fold cross validation, or something similar (eg. jack-knifing) that you will need to calculate new principal components $W$ each time. I.T. Jolliffe's Principal Component Analysis is a standard and great reference on PCA; I would strongly recommend it.
A: The test set should never be included in your modeling decisions or else you will be lose the benefit of unfitted data. This is true for regression, PCA or whatever other fitting technique.
You want to calculate the prediction error on data "unseen" by your model.
