Announcement

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

  • Accessing Variance-Covariance matrix elements by variable names.

    Hi consider the following
    Code:
    clear all
    set more off
    
    sysuse auto
    quiet reg price mpg foreign weight
    mat vc = e(V)
    mat list vc
    di vc[mpg,foreign]
    di vc[1,2]
    This produces the following output:
    Code:
    . clear all
    . set more off
    . 
    . sysuse auto
    (1978 Automobile Data)
    . quiet reg price mpg foreign weight
    . mat vc = e(V)
    . mat list vc
    
    symmetric vc[4,4]
                    mpg     foreign      weight       _cons
        mpg   5508.7774
    foreign   9089.6431   467826.27
     weight     36.2912     218.835   .39784425
      _cons   -229604.2  -993431.72  -2039.2381    11404044
    
    . di vc[mpg,foreign]
    .
    . di vc[1,2]
    9089.6431
    Why is it that, even though we can clearly see that the variance-covariance matrix has row names and column names, we can't access its elements using them? Clearly this is not a problem for the diagonal elements, i.e. the variances, since we can always access them through the system vector _se, but what about the covariances? I find this very surprising. Is there any way to access them via the variables' names, or _cons for the intercept? I have searched the forum and Google, and have found no solution for this. I apologize if I have missed it.
    Alfonso Sanchez-Penalver

  • #2
    You have to do it indirectly:
    Code:
    . local r = rownumb(vc, "mpg")
    
    . local c = colnumb(vc, "foreign")
    
    . display vc[`r', `c']
    9089.6431
    When working with commands that begin with -matrix-, you don't have to intermediate with rownumb() and colnumb(), you can access them directly with the row and column names, but only in those commands. So another approach would be:
    Code:
    . matrix one = vc["mpg", "foreign"]
    
    . matrix list one
    
    symmetric one[1,1]
           foreign
    mpg  9089.6431
    
    . display one[1, 1]
    9089.6431

    Comment


    • #3
      Hi Clyde, yes I was aware of what I call the rownumb and colnumb workaround. I find that cumbersome.

      I wasn't aware of the second option you give me, which I think is slightly better, but still cumbersome.

      I believe the downfall is that the operator vc["mpg", "foreign"] is returning a matrix instead of a scalar. In a matrix if you access one of its elements, whether by index or by name, it should return a scalar. Otherwise you have to create new macros or matrices that load the memory more. Clearly if you access only a column or a row, also by index or by name, you should return a matrix, but when both row and column in a matrix are specified it should return a scalar.
      Alfonso Sanchez-Penalver

      Comment


      • #4
        I believe the downfall is that the operator vc["mpg", "foreign"] is returning a matrix instead of a scalar. In a matrix if you access one of its elements, whether by index or by name, it should return a scalar. Otherwise you have to create new macros or matrices that load the memory more. Clearly if you access only a column or a row, also by index or by name, you should return a matrix, but when both row and column in a matrix are specified it should return a scalar.
        Yes. Actually, this might be something to add to the Wishlist.

        Comment

        Working...
        X