Announcement

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

  • How to make an (inverted) covariance matrix a full matrix

    I would like to create a full covariance matrix and then invert the matrix.

    Say that I compute the covariance as follows:

    Code:
    sysuse auto2, clear  
    corr price mpg headroom trunk weight length, covariance
    This produces:

    Code:
                 |    price      mpg headroom    trunk   weight   length
    -------------+------------------------------------------------------
           price |  8.7e+06
             mpg | -7996.28   33.472
        headroom |  285.721 -2.02536  .715707
           trunk |  3965.67 -14.3924  2.39559  18.2962
          weight |  1.2e+06 -3629.43  317.873  2234.66   604030
          length |  28360.3 -102.514  9.72556  69.2025  16370.9   495.79
    My main problem is how to instead get it to compute the matrix such that each cell is filled in. In other words, I would like to create a full covariance matrix instead of the lower triangular matrix.

    And then would this be the correct way to invert the matrix?

    Code:
    matrix a = r(C)
    matrix b = invsym(a)
    matrix list b
    This produces:
    Code:
    symmetric b[6,6]
                   price         mpg    headroom       trunk      weight      length
       price   1.858e-07
         mpg   .00001593   .08966729
    headroom   .00013193   .00988956   2.5935193
       trunk  -.00002065   .00125248  -.31581775   .15498871
      weight  -8.212e-07   .00027199  -.00069926   .00020868   .00002079
      length   .00002008   .00827906   .01079437  -.02088888  -.00059878   .02505605

  • #2
    Your symmetric matrices a and b do contain entries in each cell. Stata doesn't display the upper triangle for symmetric matrices. See -help matrix list- with attention to the -nohalf- option.

    Comment


    • #3
      Ah, I had no idea. Thank you very much!

      Comment

      Working...
      X