Principal components analysis

Install required packages

mvtnorm, robustbase, pcaPP

PCA

Using prcomp()

Standard deviations (1, .., p=2):
[1] 2.114979 1.099903

Rotation (n x k) = (2 x 2):
           PC1        PC2
[1,] 0.6877487 -0.7259489
[2,] 0.7259489  0.6877487
Importance of components:
                          PC1    PC2
Standard deviation     2.1150 1.0999
Proportion of Variance 0.7871 0.2129
Cumulative Proportion  0.7871 1.0000
[1] 0.787119 0.212881
plot of chunk rerMultPCA01
plot of chunk rerMultPCA01

For rotated principal components, see principal() from package psych.

Using princomp()

Call:
princomp(x = X)

Standard deviations:
  Comp.1   Comp.2 
2.093723 1.088849 

 2  variables and  50 observations.

Loadings:
     Comp.1 Comp.2
[1,]  0.688  0.726
[2,]  0.726 -0.688

               Comp.1 Comp.2
SS loadings       1.0    1.0
Proportion Var    0.5    0.5
Cumulative Var    0.5    1.0

Find principal components

Principal component values for original data.

           PC1        PC2
[1,] -1.633097  0.4595479
[2,]  2.503028 -1.4578202
[3,]  2.624500  1.1630316
[4,] -1.498896 -1.3124061
[5,] -2.191086  0.4319243

Principal component values for new data.

           PC1       PC2
[1,] 0.4241819 0.7484588
[2,] 1.8378795 0.7102586

Illustration

plot of chunk rerMultPCA02
plot of chunk rerMultPCA02

Dimension reduction using principal components

Full reproduction using all principal components

[1] TRUE
[1] 1.365715e-29

Dimension reduction based on approximation by only the first principal component

[1] 59.27955
[1] 1
plot of chunk rerMultPCA03
plot of chunk rerMultPCA03

Approximate the covariance matrix using principal components

         [,1]     [,2]
[1,] 2.753346 1.629294
[2,] 1.629294 2.929578
         [,1]     [,2]
[1,] 2.753346 1.629294
[2,] 1.629294 2.929578
         [,1]     [,2]
[1,] 2.115786 2.233305
[2,] 2.233305 2.357351

Robust PCA

Call:
princomp(x = X, covmat = covMcd(X))

Standard deviations:
  Comp.1   Comp.2 
2.466551 1.047429 

 2  variables and  50 observations.
Call:
PCAproj(x = X, k = ncol(X), method = "qn")

Standard deviations:
  Comp.1   Comp.2 
2.100548 1.170746 

 2  variables and  50 observations.

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts