Announcement

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

  • Trying to get the square root of the diagonal of e(V)

    Hello,
    I am able to extract the diagonal of the variance covariance matrix that results from the glm() procedure but when I try to take the square root of it I get the error "type mismatch". What's going on?
    thanks, chris

    matrix dvcovb=vecdiag(e(V))'
    matrix list dvcovb

    matrix list dvcovbt

    dvcovbt[24,1]
    r1
    outcome:age 3.879e-06
    outcome:sex .00252515
    outcome:0b.Region 0
    outcome:1.Region .00256302
    <...more rows...>

    . matrix srdvcovbt=sqrt(dvcovbt)
    type mismatch
    r(109);




  • #2
    The -sqrt()- function in Stata is defined only for numbers, not matrices. You need to loop over the rows of dvcovbt and apply sqrt() to each element. Alternatively, do this in Mata, where you can apply scalar functions elementwise to a matrix with the : operator.

    Comment


    • #3
      You might also keep in mind that r(table) includes a row for the standard errors, e.g.

      Code:
      webuse nhanes2f, clear
      glm diabetes weight height, link(logit)
      mat list r(table)
      
      . mat list r(table)
      
      r(table)[9,3]
                diabetes:   diabetes:   diabetes:
                  weight      height       _cons
           b   .02537124  -.04181401   2.1493016
          se   .00202846   .00451704   .71197977
           z   12.507651  -9.2569548   3.0187678
      pvalue   6.780e-36   2.103e-20   .00253805
          ll   .02139553  -.05066724   .75384687
          ul   .02934694  -.03296078   3.5447563
          df           .           .           .
        crit    1.959964    1.959964    1.959964
       eform           0           0           0
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Chris: I would heartily concur with Clyde that operations like this are most easily handled in Mata. For examples like yours it's not even necessary to enter the Mata environment if you don't want to; instead one or two lines of Stata command-line or do-file code issuing Mata commands should do the trick. E.g. perhaps try something like this
        Code:
        . mata st_matrix("srdvcovbt",sqrt(diagonal(st_matrix("e(V)"))))
        . matrix list srdvcovbt
        Here, the inner st_matrix(.) fetches your e(V) result while the outer st_matrix(.) sends back to the Stata environment the vector of square roots of its diagonal elements.

        Comment


        • #5
          John,
          thanks for your reply. I knew there had to be a simple one- or two-line solution to this problem. I am new to STATA, I have not used the MATA environment before and I do not have a lot of time to find a way to do this. Your solution is optimal (to me) because I can include it in my DO file.
          chris

          Comment

          Working...
          X