Announcement

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

  • looping through matrix colnames and then summing values in the matrix row

    Hi Fellow Statalisters,

    I need to loop through a post estimation r(table) after a mixed model, find the each "var(_cons)" (random effect variances) an sum them all together. I have code that is ridiculously long and tedious, so I am looking for some help in streamlining it.

    Here is an example:

    Code:
    . webuse productivity, clear
    . mixed gsp private emp hwy water other unemp || region: || state:, reml
    . mat li r(table)
    . matrix coleq B = ""
    . mat li B
    Code:
    B[9,10]
               private         emp         hwy       water       other       unemp       _cons  var(_cons)  var(_cons)      var(e)
         b   .26603079   .75550594   .07188572   .07615521  -.10053959  -.00588152   2.1269949   .00189637   .00644388    .0013543
        se   .02154709   .02645561   .02334776   .01399523   .01701732   .00090925   .15748651   .00162251   .00153343   .00006949
         z   12.346482   28.557495   3.0789125   5.4415121   -5.908074  -6.4685316   13.505886          .b          .b          .b
    pvalue   5.089e-35   2.27e-179   .00207758   5.283e-08   3.461e-09   9.896e-11   1.444e-41          .b          .b          .b
        ll   .22379927    .7036539   .02612494   .04872506  -.13389293  -.00766361    1.818327   .00035453   .00404194   .00122472
        ul   .30826232   .80735798   .11764649   .10358535  -.06718626  -.00409942   2.4356627   .01014378   .01027318   .00149759
        df           .           .           .           .           .           .           .           .           .           .
      crit    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964
     eform           0           0           0           0           0           0           0           0           0           0
    as you can see, there are 2 "var(_cons)". I need to create a scalar that sums the two related betas ("b" in row 1) to get (.00189637 + .00644388) = .00834025

    I am too embarrassed to show the code I wrote to try and do this since it is very long and clunky...

    The good news is that the colnames I need will always be "var(_cons)"

    Thanks in advance!

    Ariel

  • #2
    Originally posted by Ariel Linden View Post
    I have code that is ridiculously long and tedious, so I am looking for some help in streamlining it.
    . . . The good news is that the colnames I need will always be "var(_cons)"
    I'm not sure whether it's any shorter or less tedious, but does this help?
    Code:
    version 18.0
    
    clear *
    
    quietly webuse productivity
    quietly mixed gsp private emp hwy water other unemp || region: || state: , reml
    
    local colnames : colnames r(table)
    scalar define varsum = 0
    
    forvalues col = 1/`=wordcount("`colnames'")' {
        if word("`colnames'", `col') == "var(_cons)" ///
            scalar define varsum = varsum + r(table)["b", `col']
    }
    display in smcl as result varsum
    
    exit
    Alternatively
    Code:
    local colnames : colnames r(table)
    scalar define varsum = 0
    local col 1
    
    foreach candidate of local colnames {
        if "`candidate'" == "var(_cons)" ///
            scalar define varsum = varsum + r(table)["b", `col']
        local ++col
    }

    Comment


    • #3
      Here is another way to extract the matrices and sum. Note that this does not depend on the number of levels in the RE model.

      Code:
      webuse productivity, clear
      mixed gsp private emp hwy water other unemp || region: || state:, reml
      mat S= r(table)[1, `=colnumb(r(table), "var(_cons)")'..`=colnumb(r(table), "var(e)")-1'] * J(`=colnumb(r(table), "var(e)")'- `=colnumb(r(table), "var(_cons)")', 1, 1)
      Res.:

      Code:
      . mat l S
      
      symmetric S[1,1]
                c1
      b  .00834025

      Comment


      • #4
        Thank you Joseph and Andrew! You both always have great ideas!

        Andrew, your code works great when there is at least 1 "var(_cons)", but fails if there is no "var(_cons)". Can your code be easily edited to return 0 if there is no "var(_cons)"?

        Thank you both, again!

        Ariel

        Code:
        mixed gsp private emp hwy water other unemp  , reml
         
        mat S = r(table)[1, `=colnumb(r(table), "var(_cons)")'..`=colnumb(r(table), "var(e)")-1'] * J(`=colnumb(r(table), "var(e)")'- `=colnumb(r(table), "var(_cons)")', 1, 1)
        Code:
        invalid syntax
        r(198);

        Comment


        • #5
          Making Andrew's code a bit more readable, IMHO:

          Code:
          local col_of_var_cons = colnumb(r(table),"var(_cons)")
          local col_of_var_e    = colnumb(r(table),"var(e)")
          
          if ( missing(`col_of_var_cons') ) ///
              matrix sum = 0
          else ///
              matrix sum = ///
                  r(table)[1,`col_of_var_cons'..`=`col_of_var_e'-1'] ///
                  * J(`col_of_var_e'-`col_of_var_cons',1,1)
          
          matlist sum
          handles cases where there is no RE equation.

          Comment


          • #6
            Thank you, Daniel! These are all great solutions!

            Comment

            Working...
            X