Announcement

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

  • How to retrieve/extract some specific values from the output of -SEM-

    Hi all,

    I am running some SEM models and would like to store/save some values from the output table as scalars. I know how to extract the coefficients and standard error (by using _b and _se). But how to extract some other value, such as z value, p value, and confidence interval values?

    For example, I used the code below
    Code:
    use https://stats.idre.ucla.edu/stat/data/hsb2, clear
    
    rename science y  /* dependent variable   */
    rename math x     /* independent variable */
    rename read m     /* mediator variable    */
    rename write w    /* moderator variable 1 */
    rename socst z    /* moderator variable 2 */
    
    generate mx=m*x  /*  mv by iv interaction */
    
    sem (m <- x)(y <- m x mx)
    Then I got the output. How to exact the values highlighted in red? Can anyone help me?

    Code:
    ------------------------------------------------------------------------------
                 |                 OIM
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    Structural   |
      m          |
               x |    .724807   .0579824    12.50   0.000     .6111636    .8384504
           _cons |   14.07254   3.100201     4.54   0.000     7.996255    20.14882
      -----------+----------------------------------------------------------------
      y          |
               m |   .9766164   .2875081     3.40   0.001     .4131109    1.540122
               x |    1.03094   .2969707     3.47   0.001     .4488881    1.612992
              mx |  -.0115869   .0053091    -2.18   0.029    -.0219926   -.0011812
           _cons |  -20.83921   15.16952    -1.37   0.170    -50.57092    8.892495
    -------------+----------------------------------------------------------------
         var(e.m)|   58.71925   5.871925                      48.26811    71.43329
         var(e.y)|   49.70994   4.970994                      40.86232    60.47326
    ------------------------------------------------------------------------------
    LR test of model vs. saturated: chi2(1)   =    594.37, Prob > chi2 = 0.0000
    Last edited by Victor Smith; 21 Jun 2018, 08:44.

  • #2
    Victor,

    After running the -sem- command, type

    Code:
    return list
    You should see that it returns a matrix called r(table). Type

    Code:
    matrix list r(table)
    And you'll see that it looks a lot like an unformatted version of your output ... in fact, it is an unformatted version of your output. Most Stata commands post that matrix (anything run through -mi estimate- is an exception, as I know through some painful personal experience). Matrices are indexed by row, then column, and the z-score, p-value, and lower and upper CI limits are in the 3rd through 6th rows.

    I'm not sure what you want to do with the CIs, but the stock command -putexcel- allows you to write matrices to Excel. Ben Jann's -estout- command also allows you to write matrices, although it will calculate CIs and p-values from e(b) and e(V). If you have anything specific in mind, please ask specifically, and we will try to refer you to an existing command or try to tell you how to code things.

    In the rare case when you're working with a command that doesn't post the matrix r(table), you can calculate the p-values and CIs directly. Refer to this Stata tipMaarten Buis and thank the author if you do.
    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

    Comment


    • #3
      Originally posted by Weiwen Ng View Post
      Victor,

      After running the -sem- command, type

      Code:
      return list
      You should see that it returns a matrix called r(table). Type

      Code:
      matrix list r(table)
      And you'll see that it looks a lot like an unformatted version of your output ... in fact, it is an unformatted version of your output. Most Stata commands post that matrix (anything run through -mi estimate- is an exception, as I know through some painful personal experience). Matrices are indexed by row, then column, and the z-score, p-value, and lower and upper CI limits are in the 3rd through 6th rows.

      I'm not sure what you want to do with the CIs, but the stock command -putexcel- allows you to write matrices to Excel. Ben Jann's -estout- command also allows you to write matrices, although it will calculate CIs and p-values from e(b) and e(V). If you have anything specific in mind, please ask specifically, and we will try to refer you to an existing command or try to tell you how to code things.

      In the rare case when you're working with a command that doesn't post the matrix r(table), you can calculate the p-values and CIs directly. Refer to this Stata tipMaarten Buis and thank the author if you do.

      Weiwen, thanks a lot!

      Basically, I just would like to store some values from the r(table) as scalars. I realize that I didn't explain my problem clearly enough. So let me try again.

      After I use the -r(table)- command, I got:

      Code:
      r(table)[9,8]
                       m:          m:          y:          y:          y:          y:          /:
                       x       _cons           m           x          mx       _cons    var(e.m)
           b   .72480697   14.072537   .97661637     1.03094  -.01158689  -20.839211   58.719247
          se   .05798238   3.1002009   .28750807   .29697067   .00530912   15.169517   5.8719247
           z   12.500469   4.5392338   3.3968311   3.4715212   -2.182452  -1.3737558          .b
      pvalue   7.421e-36   5.646e-06   .00068171   .00051752   .02907619   .16951751          .b
          ll   .61116359   7.9962547   .41311091   .44888815  -.02199257  -50.570918   48.268113
          ul   .83845035   20.148819   1.5401218   1.6129918  -.00118122   8.8924952   71.433286
          df           .           .           .           .           .           .           .
        crit    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964
       eform           0           0           0           0           0           0           0
      
                       /:
                var(e.y)
           b   49.709937
          se   4.9709937
           z          .b
      pvalue          .b
          ll   40.862323
          ul   60.473257
          df           .
        crit    1.959964
      I want to store the number highlighted in red (column = "y: mx ", row = "ul") to a scalar (let's say we call the scalar "yxul"). I guess the possible command could be "scalar yxul = ******", but I am not sure how to write the ****** part. So, the question actually is how to extract a specific value from a matrix and store it as a scalar. Do you know how to do this?
      Last edited by Victor Smith; 21 Jun 2018, 21:06.

      Comment


      • #4
        Ah, I found a way to do it, the commands are as below

        Code:
        matrix test = r(table)
        scalar ymxul = test[6,4]
        display ymxul

        Comment


        • #5
          Originally posted by Victor Smith View Post
          Ah, I found a way to do it, the commands are as below

          Code:
          matrix test = r(table)
          scalar ymxul = test[6,4]
          display ymxul
          Yes, exactly. That's what I tried to convey when I said that matrices are indexed by row and column, but I forgot to say exactly how you referred to an element of a matrix! Most of the time, we don't directly manipulate the Stata matrices, but if you are working in Mata, or in R, you have to know how to do this. Anyway, you could also have said,

          Code:
          matrix test = r(table)
          matrix ul = test[6,]
          That would get you a matrix called ul (actually, maybe vector is the more proper term) which contains the entire series of upper confidence interval limits.
          Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

          When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

          Comment

          Working...
          X