1

I'm a chemist and about an year ago I decided to know something more about chemometrics.

I'm working with this problem that I don't know how to solve:

I performed an experimental design (Doehlert type with 3 factors) recording several analyte concentrations as Y. Then I performed a PCA on Y and I used scores on the first PC (87% of total variance) as new y for a linear regression model with my experimental coded settings as X.

Now I need to perform a leave-one-out cross validation removing each object before perform the PCA on the new "training set", then create the regression model on the scores as I did before, predict the score value for the observation in the "test set" and calculate the error in prediction comparing the predicted score and the score obtained by the projection of the object in the test set in the space of the previous PCA. So repeated n times (with n the number of point of my experimental design). I'd like to know how can I do it with R.

4

2 に答える 2

2

Do the calculations e.g. by prcomp and then lm. For that you need to apply the PCA model returned by prcomp to new data. This needs two (or three) steps:

  1. Center the new data with the same center that was calculated by prcomp
  2. Scale the new data with the same scaling vector that was calculated by prcomp
  3. Apply the rotation calculated by prcomp

The first two steps are done by scale, using the $center and $scale elements of the prcomp object. You then matrix multiply your data by $rotation [, components.to.use]

You can easily check whether your reconstruction of the PCA scores calculation by calculating scores for the data you input to prcomp and comparing the results with the $x element of the PCA model returned by prcomp.

Edit in the light of the comment:

If the purpose of the CV is calculating some kind of error, then you can choose between calculating error of the predicted scores y (which is how I understand you) and calculating error of the Y: the PCA lets you also go backwards and predict the original variates from scores. This is easy because the loadings ($rotation) are orthogonal, so the inverse is just the transpose.

Thus, the prediction in original Y space is scores %*% t (pca$rotation), which is faster calculated by tcrossprod (scores, pca$rotation).

于 2013-02-20T20:38:13.083 に答える
0

There is also R library pls (Partial Least Squares), which has tools for PCR (Principal Component Regression)

于 2015-02-04T11:05:52.800 に答える