Knowledge base

Some details about prcomp function in R

Introduction

As we all known, prcomp funcion can be used to run principle component analysis. However, if use default arguments of pccomp, center is TRUE and scale. = FALSE. One important thing that we have to known is that the principle component predicted is from the centered data.

Code exmaple

a <- mtcars[1:5, 1:5]
b <- prcomp(a)
predict(b, a)  # Prediected PCs from data a
                         PC1        PC2          PC3           PC4
Mazda RX4          -49.91370  -4.594507  0.646968937  4.586334e-15
Mazda RX4 Wag      -49.91370  -4.594507  0.646968937  4.586334e-15
Datsun 710        -104.63151  -2.752341 -1.029432481 -2.484688e-14
Hornet 4 Drive      44.15032  22.907890  0.006227388 -1.431935e-14
Hornet Sportabout  160.30858 -10.966536 -0.270732781  2.737366e-14
                            PC5
Mazda RX4         -2.659712e-15
Mazda RX4 Wag     -2.659712e-15
Datsun 710         5.224960e-15
Hornet 4 Drive     5.344100e-15
Hornet Sportabout -5.463025e-15

It is equal to the follows:

apply(a, 2, function(x) x- mean(x)) %*% b$rotation
                         PC1        PC2          PC3           PC4
Mazda RX4          -49.91370  -4.594507  0.646968937  4.586334e-15
Mazda RX4 Wag      -49.91370  -4.594507  0.646968937  4.586334e-15
Datsun 710        -104.63151  -2.752341 -1.029432481 -2.484688e-14
Hornet 4 Drive      44.15032  22.907890  0.006227388 -1.431935e-14
Hornet Sportabout  160.30858 -10.966536 -0.270732781  2.737366e-14
                            PC5
Mazda RX4         -2.659712e-15
Mazda RX4 Wag     -2.659712e-15
Datsun 710         5.224960e-15
Hornet 4 Drive     5.344100e-15
Hornet Sportabout -5.463025e-15

References

None

Original

None

Leave a Reply

Your email address will not be published. Required fields are marked *