Jase Villam - 3 months ago 68

R Question

I want to reverse the PCA calculated from prcomp to get back to my original data.

I thought something like the following would work:

`pca$x %*% t(pcal$rotation)`

but it doesn't.

The following link shows how to get back the original data from PCs, but explains it only for PCA using eigen on the covariance matrix

http://www.di.fc.ul.pt/~jpn/r/pca/pca.html

prcomp doesn't calcluate PCs that way.

"The calculation is done by a singular value decomposition of the (centered and possibly scaled) data matrix, not by using eigen on the covariance matrix." -prcomp

Answer

`prcomp`

will center the variables so you need to add the subtracted means back

```
t(t(pca$x %*% t(pca$rotation)) + pca$center)
```

If `pca$scale`

is `TRUE`

you will also need to re-scale

```
t(t(pca$x %*% t(pca$rotation)) * pca$scale + pca$center)
```

Source (Stackoverflow)

Comments