Announcement

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

  • Estout with PCA output

    Hi,

    I am trying to export PCA output to a word document. I have found useful documentation online (http://repec.org/bocode/e/estout/adv...ml#advanced404) that was shared a few years back on another forum. However, my attempts to replicate what was done and adapt it to PCA output have not been successful so far - I keep getting either error messages or output that does not look right. Does anyone have experience doing this?

    Thank you!

    Alexandra.

  • #2
    Welcome to Statalist.

    I have no experience doing what you seek. So I looked at the code for factor analysis output in the link you gave and adapted it to PCA using one of the examples of PCA given in the output of help pca . Perhaps this will start you in a useful direction. If not, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.
    Code:
    sysuse auto, clear
    pca trunk weight length headroom, components(3)
    matrix list e(L)
    esttab e(L)
    esttab, ///
        cells("L[1](transpose) L[2](transpose) L[3](transpose) Psi") ///
        nogap noobs nonumber nomtitle
    esttab, ///
        cells("L[1](t label(Comp 1)) L[2](t) L[3](t) Psi") ///
        nogap noobs nonumber nomtitle
    esttab, ///
        cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
        nogap noobs nonumber nomtitle
    esttab using pca.rtf, rtf replace ///
        cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
        nogap noobs nonumber nomtitle
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . pca trunk weight length headroom, components(3)
    
    Principal components/correlation                 Number of obs    =         74
                                                     Number of comp.  =          3
                                                     Trace            =          4
        Rotation: (unrotated = principal)            Rho              =     0.9874
    
        --------------------------------------------------------------------------
           Component |   Eigenvalue   Difference         Proportion   Cumulative
        -------------+------------------------------------------------------------
               Comp1 |      3.02027      2.36822             0.7551       0.7551
               Comp2 |      .652053       .37494             0.1630       0.9181
               Comp3 |      .277113      .226551             0.0693       0.9874
               Comp4 |     .0505616            .             0.0126       1.0000
        --------------------------------------------------------------------------
    
    Principal components (eigenvectors)
    
        ----------------------------------------------------------
            Variable |    Comp1     Comp2     Comp3 | Unexplained
        -------------+------------------------------+-------------
               trunk |   0.5068    0.2327   -0.8249 |    .0004293
              weight |   0.5221   -0.4536    0.2677 |      .02275
              length |   0.5361   -0.3903    0.1370 |      .02738
            headroom |   0.4280    0.7667    0.4786 |   1.645e-06
        ----------------------------------------------------------
    
    . matrix list e(L)
    
    e(L)[4,3]
                   Comp1       Comp2       Comp3
       trunk   .50677767   .23269981  -.82494625
      weight   .52208231  -.45358004   .26771062
      length   .53611307  -.39032013   .13704972
    headroom   .42800611   .76665908   .47855207
    
    . esttab e(L)
    
    ---------------------------------------------------
                         e(L)                          
                        Comp1        Comp2        Comp3
    ---------------------------------------------------
    trunk            .5067777     .2326998    -.8249462
    weight           .5220823      -.45358     .2677106
    length           .5361131    -.3903201     .1370497
    headroom         .4280061     .7666591     .4785521
    ---------------------------------------------------
    
    . esttab, ///
    >     cells("L[1](transpose) L[2](transpose) L[3](transpose) Psi") ///
    >     nogap noobs nonumber nomtitle
    
    ----------------------------------------------------------------
                         L[1]         L[2]         L[3]          Psi
    ----------------------------------------------------------------
    trunk            .5067777     .2326998    -.8249462     .0004293
    weight           .5220823      -.45358     .2677106     .0227541
    length           .5361131    -.3903201     .1370497     .0273766
    headroom         .4280061     .7666591     .4785521     1.65e-06
    ----------------------------------------------------------------
    
    . esttab, ///
    >     cells("L[1](t label(Comp 1)) L[2](t) L[3](t) Psi") ///
    >     nogap noobs nonumber nomtitle
    
    ----------------------------------------------------------------
                       Comp 1         L[2]         L[3]          Psi
    ----------------------------------------------------------------
    trunk            .5067777     .2326998    -.8249462     .0004293
    weight           .5220823      -.45358     .2677106     .0227541
    length           .5361131    -.3903201     .1370497     .0273766
    headroom         .4280061     .7666591     .4785521     1.65e-06
    ----------------------------------------------------------------
    
    . esttab, ///
    >     cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
    >     nogap noobs nonumber nomtitle
    
    ----------------------------------------------------------------
                        Comp1        Comp2        Comp3  Unexplained
    ----------------------------------------------------------------
    trunk            .5067777     .2326998    -.8249462     .0004293
    weight           .5220823      -.45358     .2677106     .0227541
    length           .5361131    -.3903201     .1370497     .0273766
    headroom         .4280061     .7666591     .4785521     1.65e-06
    ----------------------------------------------------------------
    
    . esttab using pca.rtf, rtf replace ///
    >     cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
    >     nogap noobs nonumber nomtitle
    (note: file pca.rtf not found)
    (output written to pca.rtf)
    Click image for larger version

Name:	pca.png
Views:	1
Size:	273.2 KB
ID:	1496992

    Last edited by William Lisowski; 06 May 2019, 14:10. Reason: Added example with Word (RTF) output

    Comment


    • #3
      Hi William, thank you very much for your answer! This is very helpful. I was wondering if you could tell me how to amend the code such that both tables of the PCA output appear?

      Thanks again,
      Alexandra.

      Comment


      • #4
        This was more difficult, because the first table is not returned in a matrix by the pca command in the way that the table of eigenvectors is.
        Code:
        sysuse auto, clear
        pca trunk weight length headroom, components(3)
        
        matrix ev = e(Ev)'
        matrix roweq ev = ""
        matrix colnames ev = "Eigenvalue"
        
        matrix d = ev - ( ev[2...,1] \ . )
        matrix colnames d = "Difference"
        
        matrix p = table[1...,1] / e(trace)
        matrix colnames p = "Proportion"
        
        // I don't know a neat way of doing a cumulative sum
        matrix c = J(e(trace),1,0)
        matrix c[1,1] = p[1,1]
        forvalues i=2/`e(trace)' {
            matrix c[`i',1] = c[`=`i'-1',1] + p[`i',1]
            }
        matrix colnames c = "Cumulative"
        
        matrix t = ( ev , d , p , c )
        matrix list t
        
        estadd matrix table = t
        
        esttab, ///
            cells("table[Eigenvalue](t) table[Difference](t) table[Proportion](t fmt(4)) table[Cumulative](t fmt(4))") ///
            nogap noobs nonumber nomtitle
        esttab using pca.rtf, rtf replace ///
            cells("table[Eigenvalue](t) table[Difference](t) table[Proportion](t fmt(4)) table[Cumulative](t fmt(4))") ///
            nogap noobs nonumber nomtitle
        
        esttab, ///
            cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
            nogap noobs nonumber nomtitle
        esttab using pca.rtf, rtf append ///
            cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
            nogap noobs nonumber nomtitle
        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . pca trunk weight length headroom, components(3)
        
        Principal components/correlation                 Number of obs    =         74
                                                         Number of comp.  =          3
                                                         Trace            =          4
            Rotation: (unrotated = principal)            Rho              =     0.9874
        
            --------------------------------------------------------------------------
               Component |   Eigenvalue   Difference         Proportion   Cumulative
            -------------+------------------------------------------------------------
                   Comp1 |      3.02027      2.36822             0.7551       0.7551
                   Comp2 |      .652053       .37494             0.1630       0.9181
                   Comp3 |      .277113      .226551             0.0693       0.9874
                   Comp4 |     .0505616            .             0.0126       1.0000
            --------------------------------------------------------------------------
        
        Principal components (eigenvectors) 
        
            ----------------------------------------------------------
                Variable |    Comp1     Comp2     Comp3 | Unexplained 
            -------------+------------------------------+-------------
                   trunk |   0.5068    0.2327   -0.8249 |    .0004293 
                  weight |   0.5221   -0.4536    0.2677 |      .02275 
                  length |   0.5361   -0.3903    0.1370 |      .02738 
                headroom |   0.4280    0.7667    0.4786 |   1.645e-06 
            ----------------------------------------------------------
        
        . 
        . matrix ev = e(Ev)'
        
        . matrix roweq ev = ""
        
        . matrix colnames ev = "Eigenvalue"
        
        . 
        . matrix d = ev - ( ev[2...,1] \ . )
        
        . matrix colnames d = "Difference"
        
        . 
        . matrix p = table[1...,1] / e(trace)
        
        . matrix colnames p = "Proportion"
        
        . 
        . // I don't know a neat way of doing a cumulative sum
        . matrix c = J(e(trace),1,0)
        
        . matrix c[1,1] = p[1,1]
        
        . forvalues i=2/`e(trace)' {
          2.     matrix c[`i',1] = c[`=`i'-1',1] + p[`i',1]
          3.         }
        
        . matrix colnames c = "Cumulative"
        
        . 
        . matrix t = ( ev , d , p , c )
        
        . matrix list t
        
        t[4,4]
               Eigenvalue  Difference  Proportion  Cumulative
        Comp1   3.0202724   2.3682192    .7550681    .7550681
        Comp2   .65205323   .37494048   .16301331    .9180814
        Comp3   .27711274    .2265511   .06927819   .98735959
        Comp4   .05056164           .   .01264041           1
        
        . 
        . estadd matrix table = t
        
        added matrix:
                      e(table) :  4 x 4
        
        . 
        . esttab, ///
        >     cells("table[Eigenvalue](t) table[Difference](t) table[Proportion](t fmt(4)) table[Cumulat
        > ive](t fmt(4))") ///
        >     nogap noobs nonumber nomtitle
        
        ----------------------------------------------------------------
                       Eigenvalue   Difference   Proportion   Cumulative
        ----------------------------------------------------------------
        Comp1            3.020272     2.368219       0.7551       0.7551
        Comp2            .6520532     .3749405       0.1630       0.9181
        Comp3            .2771127     .2265511       0.0693       0.9874
        Comp4            .0505616            .       0.0126       1.0000
        ----------------------------------------------------------------
        
        . esttab using pca.rtf, rtf replace ///
        >     cells("table[Eigenvalue](t) table[Difference](t) table[Proportion](t fmt(4)) table[Cumulat
        > ive](t fmt(4))") ///
        >     nogap noobs nonumber nomtitle
        (note: file pca.rtf not found)
        (output written to pca.rtf)
        
        . 
        . esttab, ///
        >     cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
        >     nogap noobs nonumber nomtitle
        
        ----------------------------------------------------------------
                            Comp1        Comp2        Comp3  Unexplained
        ----------------------------------------------------------------
        trunk            .5067777     .2326998    -.8249462     .0004293
        weight           .5220823      -.45358     .2677106     .0227541
        length           .5361131    -.3903201     .1370497     .0273766
        headroom         .4280061     .7666591     .4785521     1.65e-06
        ----------------------------------------------------------------
        
        . esttab using pca.rtf, rtf append ///
        >     cells("L[Comp1](t) L[Comp2](t) L[Comp3](t) Psi[Unexplained]") ///
        >     nogap noobs nonumber nomtitle
        (output written to pca.rtf)
        Click image for larger version

Name:	pca.png
Views:	1
Size:	310.0 KB
ID:	1497027

        Comment


        • #5
          Fantastic, thank you! Appreciate the help. That solves my problem entirely.

          Alexandra.

          Comment


          • #6
            In the topic at

            https://www.statalist.org/forums/for...tput-after-pca

            Clyde Schechter has pointed out that commands in the post #4 that reads
            Code:
            matrix p = table[1...,1] / e(trace)
            should in fact read
            Code:
            matrix p = ev[1...,1] / e(trace)

            Comment


            • #7
              Hello!

              I am trying to adopt this to factor analysis in order to be able to export the first table. However, even with the corrected code I get an error "matrix has missing values" after that line. Would you know why? Or would you know how to adopt this to factor analysis?

              Thank you very much,

              Gaspard

              Edit: I realized that all I needed to do was to change all "e(trace)" to "e(df_i)", except in line 11, where "e(trace)" needed to be changed as "e(evsum)", like;
              matrix p = table[1...,1] / e(trace)
              Thank you very much!
              Last edited by Gaspard Gaston; 08 Jan 2021, 08:29.

              Comment

              Working...
              X