Announcement

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

  • Saving estimates from -margins-

    Hi Statalisters,

    I have a rather simple question. I am trying to save the estimate from margins after running margins multiple times to evaluate the margins of a 1 unit increase of age for different groups at the mean age of the group being evaluated. For example:

    Code:
    use http://www.stata-press.com/data/r13/margex, clear
    logistic outcome i.sex c.age##i.group
    
    foreach num of numlist 1/3 { 
    margins if group==`num', at((mean)age) dydx(age)
    }
    
    Results below: 
    
     use http://www.stata-press.com/data/r13/margex, clear
    (Artificial data for margins)
    
    . logistic outcome i.sex c.age##i.group
    
    Logistic regression                               Number of obs   =       3000
                                                      LR chi2(6)      =     596.53
                                                      Prob > chi2     =     0.0000
    Log likelihood = -1067.8068                       Pseudo R2       =     0.2183
    
    ------------------------------------------------------------------------------
         outcome | Odds Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             sex |
         female  |   1.645435   .2217915     3.69   0.000     1.263414    2.142969
             age |   1.095059   .0092606    10.74   0.000     1.077058    1.113361
                 |
           group |
              2  |    .524537   .3500767    -0.97   0.334     .1418022    1.940301
              3  |   .4550779   .4768744    -0.75   0.452     .0583599    3.548597
                 |
     group#c.age |
              2  |   1.001389   .0140286     0.10   0.921     .9742675    1.029266
              3  |   .9852544   .0255426    -0.57   0.567     .9364425    1.036611
                 |
           _cons |   .0037717   .0016669   -12.63   0.000     .0015862    .0089686
    ------------------------------------------------------------------------------
    
    . 
    . foreach num of numlist 1/3 { 
      2. margins if group==`num', at((mean)age) dydx(age)
      3. }
    
    Average marginal effects                          Number of obs   =       1199
    Model VCE    : OIM
    
    Expression   : Pr(outcome), predict()
    dy/dx w.r.t. : age
    at           : age             =    46.71893 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             age |   .0183786   .0015931    11.54   0.000     .0152562     .021501
    ------------------------------------------------------------------------------
    
    Average marginal effects                          Number of obs   =       1118
    Model VCE    : OIM
    
    Expression   : Pr(outcome), predict()
    dy/dx w.r.t. : age
    at           : age             =     37.9025 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             age |   .0064137   .0006617     9.69   0.000     .0051169    .0077105
    ------------------------------------------------------------------------------
    
    Average marginal effects                          Number of obs   =        683
    Model VCE    : OIM
    
    Expression   : Pr(outcome), predict()
    dy/dx w.r.t. : age
    at           : age             =    30.75549 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             age |   .0013779   .0004068     3.39   0.001     .0005805    .0021753
    ------------------------------------------------------------------------------
    How would I save the dy/dx and the Std. Err.?

    Thanks!
    I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

  • #2
    -margins- returns the estimates in matrix r(b) and the squares of the standard errors are found in the diagonal elements of r(V). You can store those matrices immediately after -margins- runs and then store the parameters of interest in scalars or macros, or combine them in whatever way suits you using the various matrix commands in Stata.

    Comment


    • #3
      By the way, are you sure those are the marginal effects you want? A more usual approach would be:

      Code:
      margins group, dydx(age) at((mean) age)
      The matrices r(b) and r(V) will contain the marginal effects and the squares of the standard errors for all of the values of group.

      But these are different statistics from what your code generates. The loop gives you estimates restricted to three subsamples of the data, identified by the value of group. Note also that with this approach, the mean value of age differs: it is the mean value for observations with that value of group. The one-liner here gives you estimates using the entire sample, but with each observation treated as if it had age = (mean) age [meaning mean age in the entire sample], and group = particular value of group shown in output.

      It's your call as to which set of statistics is the one you need for your purposes. I'm just noting that the statistics generated by the one-line version are more commonly wanted.
      Last edited by Clyde Schechter; 05 Feb 2016, 20:11.

      Comment


      • #4
        This
        Code:
         
         foreach num of numlist 1/3 {  margins if group==`num', at((mean)age) dydx(age) }
        can be rewritten as
        Code:
        margins , at((mean)age) dydx(age) over(group)

        Comment


        • #5
          Clyde and Scott,

          Thank you for your help. 2 points:

          1) I do believe that
          Code:
          margins , at((mean)age) dydx(age) over(group)
          gives me what I want since it gives me the effect of an additional year of age on the probability of outcome if the age was the mean age at the subgroup being predicted. And done for each subgroup. (A mouthful, but I think I got that right??)

          2) Apologies, I am not very familiar with saving return matrices, I tried to look it up and tried to following to no avail. I am just trying to save the dy/dx and the Std. Err. for each subgroup with the subgroup identifier.

          Code:
          margins , at((mean)age) dydx(age) over(group)
          
          Average marginal effects                          Number of obs   =       3000
          Model VCE    : OIM
          
          Expression   : Pr(outcome), predict()
          dy/dx w.r.t. : age
          over         : group
          at           : 1.group
                             age             =    46.71893 (mean)
                         2.group
                             age             =     37.9025 (mean)
                         3.group
                             age             =    30.75549 (mean)
          
          ------------------------------------------------------------------------------
                       |            Delta-method
                       |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          age          |
                 group |
                    1  |   .0183786   .0015931    11.54   0.000     .0152562     .021501
                    2  |   .0064137   .0006617     9.69   0.000     .0051169    .0077105
                    3  |   .0013779   .0004068     3.39   0.001     .0005805    .0021753
          ------------------------------------------------------------------------------
          
          .
          . gen dydx = r(b)
          type mismatch
          r(109);
          Last edited by Karl Yesler; 05 Feb 2016, 23:46.
          I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

          Comment


          • #6
            Yes, -gen dydx = r(b)- won't work because you are trying to create a variable, but r(b) is a matrix. So you have to do a little more work:

            Code:
            use http://www.stata-press.com/data/r13/margex, clear
            logistic outcome i.sex c.age##i.group
            
            margins, dydx(age) at((mean) age) over(group)
            
            matrix B = r(b)
            matrix list B // JUST SO YOU CAN SEE WHAT IT IS, NOT NECESSARY
            
            matrix V = r(V)
            matrix list V // AGAIN, JUST SO YOU CAN SEE IT
            
            gen dydx = .
            gen se_dydx = .
            forvalues g = 1/3 {
                replace dydx = B[1, `g'] if group == `g'
                replace se_dydx = sqrt(V[`g', `g']) if group == `g'
            }

            Comment


            • #7
              Clyde,

              Thank you. That worked perfectly.
              I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

              Comment

              Working...
              X