# Preprocessing data using caret

I've been reading a bunch of posts that advise people to not include test data when preprocessing. So I've proceeded by first setting aside a test dataset to be used to assess how well my classifier at the final stage.

I have two sources of data: clinical data and gene expression data. Since the gene expression data has many, many dimensions, I wanted to use PCA to reduce the number of dimensions to about 15. Then I combined the PCs with the clinical predictors to obtain a "full" features dataset. Then, I trained a model using train in the caret package. However, I want to use that classifier to make predictions, but when I put my test dataset into the predict function, I get an error because the principal components are not in the test dataset.

So my question is, do I run PCA on the test dataset as well? Or am I misunderstanding the whole concept of preprocessing? Thank you!

You have found the principal component direction, say $v_1, \ldots, v_k$, from the training data set. Let $V= [v_1, \ldots, v_k] \in \mathbb{R}^{n \times k}$
Given any data point $x \in \mathbb{R}^n$ (assuming they have been centralized), we can project it to a $k$-dimensional space by computing $V^Tx \in \mathbb{R}^k$.
While $V$ is obtained only from the training data, give any data, including training, validation, or test data, we can perform such projection, and hence we can compute $V^Tx_{\text{test}}$ and make it compatible with the training data.