Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • table/collect PCA proportion of variance

    Hi,

    I'm trying to create a table (Stata 17) of several PCA models that shows the share of variance explained by each principal component ("Proportion" column in the results), but I'm unsuccessful and searching for help on this did not help either.

    Code:
    . sysuse auto
    (1978 automobile data)
    
    . pca mpg price weight
    
    Principal components/correlation                 Number of obs    =         74
                                                     Number of comp.  =          3
                                                     Trace            =          3
        Rotation: (unrotated = principal)            Rho              =     1.0000
    
        --------------------------------------------------------------------------
           Component |   Eigenvalue   Difference         Proportion   Cumulative
        -------------+------------------------------------------------------------
               Comp1 |       2.2225      1.63291             0.7408       0.7408
               Comp2 |      .589588      .401676             0.1965       0.9374
               Comp3 |      .187912            .             0.0626       1.0000
        --------------------------------------------------------------------------
    
    Principal components (eigenvectors) 
    
        ----------------------------------------------------------
            Variable |    Comp1     Comp2     Comp3 | Unexplained 
        -------------+------------------------------+-------------
                 mpg |  -0.6021    0.4307    0.6723 |           0 
               price |   0.5037    0.8582   -0.0987 |           0 
              weight |   0.6195   -0.2792    0.7337 |           0 
        ----------------------------------------------------------
    Any advice would be more than welcome.


  • #2
    Ariel Karlinsky I do not know if it possible to manipulate matrices once collected, and I also don't know how to pass a matrix to -collect- unless it is a stored result. Thus here is my code, which feels unnecessarily complicated. Perhaps someone else has a much easier way of doing it!

    Code:
    collect clear
    
    capture program drop make_prop
    program define make_prop, rclass
        args eigenval trace
        mat wanted = `eigenval' / `trace'
        return mat Prop = wanted
    end
    
    sysuse auto, clear
    pca mpg price weight
    
    make_prop e(Ev) e(trace)
    
    collect get r(Prop)
    collect label levels rowname r1 "Proportion", modify
    collect style cell rowname, nformat(%5.4f)
    collect layout (colname) (rowname)
    noi collect preview
    which produces:
    Code:
    ------------------
          | Proportion
    ------+-----------
    Comp1 |     0.7408
    Comp2 |     0.1965
    Comp3 |     0.0626
    ------------------
    Last edited by Hemanshu Kumar; 14 Sep 2022, 07:09.

    Comment


    • #3
      Thanks! for my purposes this is probably good enough!

      Comment

      Working...
      X