Search code examples
statacorrelation

Get formatted correlation coefficients with significance levels in stata


I have a correlation matrix I estimate in Stata:

  estpost corr var1 var2 var3 var4, matrix listwise 

I want to get the output to LaTeX, simultaneously displaying only 3 decimal places and significance stars. I can do the two things separately, but somehow can't get both.

 esttab using TABLE2B.tex, replace cells("rho(fmt(%9.3f))")  nonum  not unstack compress label  //version with correct formatting

 esttab using TABLE2Bstars.tex, replace   nonum  not unstack compress label //version with stars. 

I tried changing the formatting of the variables. It did not change the outcomes.


Solution

  • Try this:

    . sysuse auto, clear
    (1978 automobile data)
    
    . estpost corr price mpg weight foreign, matrix listwise
    
                 |      e(b)     e(rho)       e(p)   e(count) 
    -------------+--------------------------------------------
    price        |                                            
           price |         1          1                    74 
             mpg | -.4685967  -.4685967   .0000255         74 
          weight |  .5386115   .5386115   7.42e-07         74 
         foreign |  .0487195   .0487195   .6801851         74 
    mpg          |                                            
             mpg |         1          1                    74 
          weight | -.8071749  -.8071749   3.80e-18         74 
         foreign |  .3933974   .3933974   .0005254         74 
    weight       |                                            
          weight |         1          1                    74 
         foreign | -.5928299  -.5928299   2.62e-08         74 
    foreign      |                                            
         foreign |         1          1                    74 
    
    . esttab using TABLE2B.tex, replace cells("rho(fmt(%9.3f))") nonum not unstack compress label
    (output written to TABLE2B.tex)
    
    . type TABLE2B.tex
    {
    \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
    \begin{tabular}{l*{4}{c}}
    \hline\hline
                    &\multicolumn{4}{c}{}                   \\
                    &    Price&Mileage (mpg)&Weight (lbs.)&Car origin\\
                    &      rho&      rho&      rho&      rho\\
    \hline
    Price           &    1.000&         &         &         \\
    Mileage (mpg)   &   -0.469&    1.000&         &         \\
    Weight (lbs.)   &    0.539&   -0.807&    1.000&         \\
    Car origin      &    0.049&    0.393&   -0.593&    1.000\\
    \hline
    Observations    &       74&         &         &         \\
    \hline\hline
    \end{tabular}
    }
    
    . esttab using TABLE2Bstars.tex, replace  cells(rho(fmt(%9.3f) star)) nonum not unstack compress label
    (output written to TABLE2Bstars.tex)
    
    . type TABLE2Bstars.tex
    {
    \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
    \begin{tabular}{l*{4}{c}}
    \hline\hline
                    &\multicolumn{4}{c}{}                                                       \\
                    &    Price         &Mileage (mpg)         &Weight (lbs.)         &Car origin         \\
                    &      rho         &      rho         &      rho         &      rho         \\
    \hline
    Price           &    1.000         &                  &                  &                  \\
    Mileage (mpg)   &   -0.469\sym{***}&    1.000         &                  &                  \\
    Weight (lbs.)   &    0.539\sym{***}&   -0.807\sym{***}&    1.000         &                  \\
    Car origin      &    0.049         &    0.393\sym{***}&   -0.593\sym{***}&    1.000         \\
    \hline
    Observations    &       74         &                  &                  &                  \\
    \hline\hline
    \end{tabular}
    }
    

    esttab is a wrapper for estout. Adding the noisily option to esttab lets you see what estout is doing under the hood. This can often help you find a solution to such problems.