Announcement

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

  • Export covariance matrix using esttab : Is it really impossible?

    On this thread here we figured out how to export a covariance matrix to Excel using -putexcel- : https://www.statalist.org/forums/for...atrix-to-excel

    However I like using Ben Jann's -esttab- for its superior formatting abilities.

    Unfortunately it seems that it is not possible to export a covariance matrix using -esttab-.

    Ben Jann might consider extending his command -estpost correlate- to accommodate the export of covariance matrices.

    So when I export correlation matrix, everything works fine:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    .  correlate price mpg headroom
    (obs=74)
    
                 |    price      mpg headroom
    -------------+---------------------------
           price |   1.0000
             mpg |  -0.4686   1.0000
        headroom |   0.1145  -0.4138   1.0000
    
    
    . qui estpost correlate price mpg headroom, matrix
    
    . eststo correlations
    
    . esttab correlations using TEMPcorrelations.csv, unstack compress b(2)
    (output written to TEMPcorrelations.csv)
    But when I try to export my covariance matrix, computer says no:

    Code:
    .  correlate price mpg headroom, cov
    (obs=74)
    
                 |    price      mpg headroom
    -------------+---------------------------
           price |  8.7e+06
             mpg | -7996.28   33.472
        headroom |  285.721 -2.02536  .715707
    
    
    . qui estpost correlate price mpg headroom, matrix cov
    option cov not allowed
    r(198);
    Reading the help file of -estpost correlate- indeed it turns out that the option cov is not allowed. But then how do we export covariance matrices?


  • #2
    There is nothing impossible about it. One just needs the right command:
    Code:
    correlate price mpg headroom, cov
    esttab r(C)
    
    ---------------------------------------------------
                         r(C)                          
                        price          mpg     headroom
    ---------------------------------------------------
    price             8699526    -7996.283     285.7209
    mpg             -7996.283     33.47205    -2.025361
    headroom         285.7209    -2.025361     .7157071
    ---------------------------------------------------

    Comment


    • #3
      Maybe the following helps:

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . correlate price mpg headroom, cov
      (obs=74)
      
                   |    price      mpg headroom
      -------------+---------------------------
             price |  8.7e+06
               mpg | -7996.28   33.472
          headroom |  285.721 -2.02536  .715707
      
      
      . matrix C = r(C)
      
      . esttab matrix(C)
      
      ---------------------------------------------------
                              C                          
                          price          mpg     headroom
      ---------------------------------------------------
      price             8699526    -7996.283     285.7209
      mpg             -7996.283     33.47205    -2.025361
      headroom         285.7209    -2.025361     .7157071
      ---------------------------------------------------
      ben

      Comment


      • #4
        sorry, did not see the post by Fernando.

        Comment


        • #5
          Thank you Ben, thank you Fernando.

          The syntax you propose did come to me as a possibility, and I had the feeling that I had exported matrices like this before. (But I did not find help file about this, and I did not look enough. Now I see that this is documented here http://repec.org/bocode/e/estout/advanced.html)

          In an interesting quirk of Stata 15.1 and below, all my guesses did not work, and in fact Fernando's syntax does not work either:

          Code:
          . sysuse auto
          (1978 Automobile Data)
          
          . correlate price mpg headroom, cov
          (obs=74)
          
                       |    price      mpg headroom
          -------------+---------------------------
                 price |  8.7e+06
                   mpg | -7996.28   33.472
              headroom |  285.721 -2.02536  .715707
          
          
          . esttab r(C) using TEMPcov.cvs
          matrix r(C) not found
          r(111);
          
          . esttab matrix(r(C)) using TEMPcov.cvs
          ( invalid name
          r(198);
          
          . matrix C = r(C)
          
          . esttab matrix(C) using TEMPcov.cvs
          (output written to TEMPcov.cvs)

          Comment

          Working...
          X