There's more...

Another principal component analysis function is princomp. In this function, the calculation is performed by using eigen on a correlation or covariance matrix instead of a single value decomposition used in the prcomp function. In general practice, using prcomp is preferable; however, we cover how to use princomp here:

  1. First, use princomp to perform PCA:
    > swiss.princomp = princomp(swiss,
    + center = TRUE,
    + scale  = TRUE)
    >swiss.princomp
    Output
    Call:
    princomp(x = swiss, center = TRUE, scale = TRUE)
    
    Standard deviations:
       Comp.1    Comp.2    Comp.3    Comp.4    Comp.5 
    42.896335 21.201887  7.587978  3.687888  2.721105 
    
     5 variables and 47 observations. 
  1. You can then obtain the summary information:
        > summary(swiss.princomp)
        Output
        Importance of components:
                                   Comp.1     Comp.2     Comp.3  
Comp.4 Comp.5 Standard deviation 42.8963346 21.2018868 7.58797830
3.687888330 2.721104713 Proportion of Variance 0.7770024 0.1898152 0.02431275
0.005742983 0.003126601 Cumulative Proportion 0.7770024 0.9668177 0.99113042
0.996873399 1.000000000
  1. You can use the predict function to obtain principal components from the input features:
        > predict(swiss.princomp, swiss[1,])
        Output
                      Comp.1    Comp.2   Comp.3   Comp.4   Comp.5
        Courtelary -38.95923 -20.40504 12.45808 4.713234 -1.46634

In addition to the prcomp and princomp functions from the stats package, you can use the principal function from the psych package:

  1. First, install and load the psych package:
        > install.packages("psych")
        > install.packages("GPArotation")
        > library(psych)  
  1. You can then use the principal function to retrieve the principal components:
        > swiss.principal = principal(swiss, nfactors=5, rotate="none")
        > swiss.principal
        Output
        Principal Components Analysis
        Call: principal(r = swiss, nfactors = 5, rotate = "none")
        Standardized loadings (pattern matrix) based upon correlation
matrix PC1 PC2 PC3 PC4 PC5 h2 u2 Agriculture -0.85 -0.27 0.00 0.45 -0.03 1 -6.7e-16 Examination 0.93 -0.01 -0.04 0.24 0.29 1 4.4e-16 Education 0.80 0.20 0.49 0.19 -0.23 1 2.2e-16 Catholic -0.63 0.38 0.66 -0.06 0.17 1 -2.2e-16 Infant.Mortality -0.15 0.90 -0.38 0.12 -0.03 1 -8.9e-16 PC1 PC2 PC3 PC4 PC5 SS loadings 2.63 1.07 0.82 0.31 0.17 Proportion Var 0.53 0.21 0.16 0.06 0.03 Cumulative Var 0.53 0.74 0.90 0.97 1.00 Proportion Explained 0.53 0.21 0.16 0.06 0.03 Cumulative Proportion 0.53 0.74 0.90 0.97 1.00 Test of the hypothesis that 5 components are sufficient. The degrees of freedom for the null model are 10 and the objective
function was 2.13 The degrees of freedom for the model are -5 and the objective
function was 0 The total number of observations was 47 with MLE Chi Square = 0
with prob< NA Fit based upon off diagonal values = 1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset