Announcement

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

  • Calculating the inverse of a variance covariance matrix in stata error 3251

    Hello statalist members i have the variance covariance matrix of BG that is a matrix that combines two vectors created as follows:
    mat BG=G,B
    mat li BG
    // var-covar matrix
    mat var_cov=get(VCE)
    mat li var_cov

    I​ have a problem when i try to calculate the inverse of var_cov matrix since when i input:

    mata: inv_cov=cholinv("var_cov")

    it pops out error 3251: cholinv(): 3251 <tmp> [1,1] found where real or complex required
    <istmt>: - function returned error

    My values should be all real and i don't understand why it tells me that they are not.
    Anybody has an answer?
    Would really appreciate your help

    Best
    Donald
    Last edited by Donald Merkuri; 26 Nov 2015, 07:22.

  • #2
    Hi Donald,
    the code should work if you write:
    Code:
    mata: inv_cov=cholinv(var_cov)
    So just leave out the quotes.
    Best.
    Jan

    Comment


    • #3
      var_cov is not a mata object, but a Stata matrix

      Code:
      mata:
      v = st_matrix("var_cov")
      inv_cov=cholinv(v)
      
      end
      Or

      Code:
      mata: inv_cov=cholinv(st_matrix("var_cov"))

      Comment


      • #4
        If you work in interactive mode and you want to copy the content of a Stata variable in the memory of Mata then you can either use getmata or st_data()/st_view(). In non-interactive mode you can only use st_data()/st_view().
        If you want to get a matrix or a vector (different from a variable) then use st_matrix(). If you need a Stata scalar use st_numscalar(). If you need a local then use st_local(), etc.

        More generally take a look at the "Stata" entry in the Mata manual. It is a good investment if you plan to use Mata in the future. The general principle is to transform Stata object into Mata objects. the st_* functions are here to just achieve that, i.e to allow Stata and Mata to communicate together.

        Here is an example

        Code:
        clear
        set obs 10
        
        drawnorm x1 x2
        
        mat bStata = (1,1)
        
        mata:
        xMata = st_data(.,"x1 x2")
        bMata = st_matrix("bStata")
        
        product = x*b'
        
        product
        
        
        end




        Comment

        Working...
        X