Announcement

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

  • how to custom a correlation table

    Hi,

    I have run a pairwise correlation below and I would like to make it exported to look like the image below. I do not know how to custom this table, any help will be appreciated.


    ----------------------- copy starting from the next line -----------------------
    Code:
    pwcorr var1 var2 var3 var4 var5 var6 var7 var8 var8 var9 var10 var11 var12 var13 var14 var15 var16 var17 var18, star(.05) bonferroni
    ------------------ copy up to and including the previous line ------------------
    Click image for larger version

Name:	Wvl6t.png
Views:	1
Size:	334.4 KB
ID:	1778071

    Last edited by May Blake; 27 May 2025, 14:09.

  • #2
    Correlation matrices are often represented by their upper or lower triangular portions because they are symmetric - and thus you want to avoid redundancy. Here is a way using estout from SSC. Add the option -nostar- to get rid of the stars, if that is what you want.

    Code:
    sysuse auto, clear
    pwcorr mpg weight turn displacement headroom length
    
    *REPLICATING PWCORR OUTPUT AND EXPORTING USING ESTTAB
    quietly estpost corr mpg weight turn displacement headroom length, matrix
    
    local eqlabels
    forval var= 1/6{
        local eqlabels "`eqlabels' (`var')"
    }
    
    esttab ., replace b(3) unstack nonum nomtitle not noobs ///
    label compress eqlabels(`eqlabels', lhs("Variables"))
    Res.:

    Code:
    . pwcorr mpg weight turn displacement headroom length
    
                 |      mpg   weight     turn displa~t headroom   length
    -------------+------------------------------------------------------
             mpg |   1.0000 
          weight |  -0.8072   1.0000 
            turn |  -0.7192   0.8574   1.0000 
    displacement |  -0.7056   0.8949   0.7768   1.0000 
        headroom |  -0.4138   0.4835   0.4245   0.4745   1.0000 
          length |  -0.7958   0.9460   0.8643   0.8351   0.5163   1.0000 
    
    . 
    . *REPLICATING PWCORR OUTPUT AND EXPORTING USING ESTTAB
    . quietly estpost corr mpg weight turn displacement headroom length, matrix
    
    . 
    . local eqlabels
    
    . forval var= 1/6{
      2.     local eqlabels "`eqlabels' (`var')"
      3. }
    
    . 
    . esttab ., replace b(3) unstack nonum nomtitle not noobs ///
    > label compress eqlabels(`eqlabels', lhs("Variables"))
    
    ----------------------------------------------------------------------------------------------
    Variables              (1)          (2)          (3)          (4)          (5)          (6)   
    ----------------------------------------------------------------------------------------------
    Mileage (mpg)        1.000                                                                    
    Weight (lbs.)       -0.807***     1.000                                                       
    Turn circ..)        -0.719***     0.857***     1.000                                          
    Displacem.. in.)    -0.706***     0.895***     0.777***     1.000                             
    Headroom (in.)      -0.414***     0.483***     0.424***     0.474***     1.000                
    Length (in.)        -0.796***     0.946***     0.864***     0.835***     0.516***     1.000   
    ----------------------------------------------------------------------------------------------
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      Row numbers can additionally be added using a loop over variables. The following assumes that variables are labeled (as in the auto dataset).

      Code:
      sysuse auto, clear
      local vars mpg weight turn displacement headroom length
      pwcorr `vars'
      
      *REPLICATING PWCORR OUTPUT AND EXPORTING USING ESTTAB
      quietly estpost corr `vars', matrix
      
      local eqlabs
      local coeflabs
      local i 0
      foreach var of local vars{
          local++i 
          local eqlabels "`eqlabels' (`i')"
          local coeflabs `coeflabs' `var' "(`i') `:var lab `var''" 
      }
      
      esttab ., replace b(3) unstack nonum nomtitle not noobs ///
      label compress coeflab(`coeflabs') eqlabels(`eqlabels', lhs("Variables"))

      Res.:

      Code:
      . esttab ., replace b(3) unstack nonum nomtitle not noobs ///
      > label compress coeflab(`coeflabs') eqlabels(`eqlabels', lhs("Variables"))
      
      ----------------------------------------------------------------------------------------------
      Variables              (1)          (2)          (3)          (4)          (5)          (6)   
      ----------------------------------------------------------------------------------------------
      (1) Mileage (m~)     1.000                                                                    
      (2) Weigh..)        -0.807***     1.000                                                       
      (3) Turn ..)        -0.719***     0.857***     1.000                                          
      (4) Displ.. in.)    -0.706***     0.895***     0.777***     1.000                             
      (5) Headr..)        -0.414***     0.483***     0.424***     0.474***     1.000                
      (6) Length (in.)    -0.796***     0.946***     0.864***     0.835***     0.516***     1.000   
      ----------------------------------------------------------------------------------------------
      * p<0.05, ** p<0.01, *** p<0.001

      Comment

      Working...
      X