Announcement

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

  • Creating dataset variables from inverse of covariance matrix

    Hello,

    I am using Stata 13.1. I have variables a b c d e f. I want to create 15 new variables using the values from the inverse of the covariance matrix of these variables. Typically, to get the covariance matrix I use "corr varlist, cov", however I could not work out how to get the inverse of this. Therefore, through a great deal of trawling the Stata forums I have so far done the following:

    Code:
    mkmat a b c d e f, matrix(test1)
    mata test1 = st_matrix("test1")
    mata st_matrix("vce", variance(test1))
    mat li vce
    
    mata inv_cov=cholinv(st_matrix("vce"))
    mata inv_cov
    At this point, I can see the inverted covariance matrix tabled. After any attempt I have made using svmat, or even just trying to create another matrix with the same values, Stata informs me that "matrix inv_cov cannot be found", and therefore I cannot begin to work out how to create my variables.

    I would appreciate any help to further my efforts in creating these variables.

    Best,
    Sarah Rhatigan

  • #2
    Here is some technique may point you in a useful direction. In it, your Stata matrix test1 is taken into Mata, and the inverse VCE matrix is returned from Mata to the Stata matrix inv_cov. I think your problems were perhaps due to a confusion between Stata matrices and Mata matrices: the inv_cov matrix is a Mata matrix, not a Stata matrix, and you neglected to return it to Stata in the same way you returned the Mata vce matrix.

    Code:
    // create example data
    sysuse auto, clear
    rename (price-weight) (a b c d e f)
    
    // your code
    mkmat a b c d e f, matrix(test1)
    mata test1 = st_matrix("test1")
    mata st_matrix("vce", variance(test1))
    mat li vce
    
    mata inv_cov=cholinv(st_matrix("vce"))
    mata inv_cov
    
    // return Mata matrix inv_cov to Stata matrix inv_cov
    mata st_matrix("inv_cov",inv_cov)
    mat li inv_cov
    
    // all in one Mata command
    mata st_matrix("inv_cov2",cholinv(variance(st_matrix("test1"))))
    mat li inv_cov2

    Comment

    Working...
    X