Search code examples
rdataframepca

How do I extract summary of PCA as a dataframe in R using Prcomp?


res.pca = prcomp(y, scale = TRUE)
summ=summary(res.pca)
summ

Gives me the output Desired Output

I want to change this Summary in to a Data Frame,

I've Tried to use the do.call(cbind, lapply(res.pca, summary)) but it gives me the summary of Min/Max but not the one I desire.

Please See That I dont want to extract values from column names, I seek a general solution That I can use.


Solution

  • What you are looking for is in the "element" importance of summary(res.pca):

    Example taken from Principal Components Analysis - how to get the contribution (%) of each parameter to a Prin.Comp.?:

    a <- rnorm(10, 50, 20)
    b <- seq(10, 100, 10)
    c <- seq(88, 10, -8)
    d <- rep(seq(3, 16, 3), 2)
    e <- rnorm(10, 61, 27)
    
    my_table <- data.frame(a, b, c, d, e)
    res.pca <- prcomp(my_table, scale = TRUE)
    
    summary(res.pca)$importance 
    #                          PC1    PC2    PC3     PC4       PC5
    #Standard deviation     1.7882 0.9038 0.8417 0.52622 9.037e-17
    #Proportion of Variance 0.6395 0.1634 0.1417 0.05538 0.000e+00
    #Cumulative Proportion  0.6395 0.8029 0.9446 1.00000 1.000e+00
    
    class(summary(res.pca)$importance)
    #[1] "matrix"
    

    N.B.:
    When you want to "study" an object, it can be convenient to use str on it. Here, you can do str(summary(pca) to see where the information are and hence where you can get what you want:

    str(summary(res.pca))
    
    List of 6
     $ sdev      : num [1:5] 1.79 9.04e-01 8.42e-01 5.26e-01 9.04e-17
     $ rotation  : num [1:5, 1:5] 0.278 0.512 -0.512 0.414 -0.476 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:5] "a" "b" "c" "d" ...
      .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
     $ center    : Named num [1:5] 34.9 55 52 9 77.8
      ..- attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
     $ scale     : Named num [1:5] 22.4 30.28 24.22 4.47 26.11
      ..- attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
     $ x         : num [1:10, 1:5] -2.962 -1.403 -1.653 -0.537 1.186 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : NULL
      .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
    
     $ importance: num [1:3, 1:5] 1.788 0.64 0.64 0.904 0.163 ...
     ..- attr(*, "dimnames")=List of 2
     .. ..$ : chr [1:3] "Standard deviation" "Proportion of Variance" "Cumulative Proportion"
     .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
    - attr(*, "class")= chr "summary.prcomp"