Announcement

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

  • Saving margins estimates as a new variable

    Hi Statalisters,
    Is it possible to save margins estimates as a new variable? In the example below, I would like to create a variable that contains the marginal effects of ycn across the whole range of age in order to then use this variable in a two-way graph (that way I will be able to combine several "margins plots" in one graph). You can of course create this variable manually, but that is so tiresome...

    I found a similar post (https://www.statalist.org/forums/for...=1566557316931) but I do not manage to get the code provided in that post to work in my case.


    Code:
    .  . use http://www.stata-press.com/data/r13/margex, clear
    (Artificial data for margins)
    
    . . reg outcome c.ycn c.age c.age#c.ycn
    
          Source |       SS       df       MS              Number of obs =    3000
    -------------+------------------------------           F(  3,  2996) =  182.34
           Model |  65.2517193     3  21.7505731           Prob > F      =  0.0000
        Residual |  357.387947  2996  .119288367           R-squared     =  0.1544
    -------------+------------------------------           Adj R-squared =  0.1535
           Total |  422.639667  2999  .140926865           Root MSE      =  .34538
    
    ------------------------------------------------------------------------------
         outcome |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             ycn |  -.0018226   .0010404    -1.75   0.080    -.0038625    .0002173
             age |    .008331   .0019362     4.30   0.000     .0045346    .0121275
                 |
     c.age#c.ycn |   .0000576   .0000252     2.29   0.022     8.18e-06    .0001071
                 |
           _cons |  -.1989264   .0783917    -2.54   0.011    -.3526334   -.0452194
    ------------------------------------------------------------------------------
    
    . margins, dydx (ycn) at (age=(20(1)60))
    Thanks in advance!

    Kind regards,
    David

  • #2
    margins has an undocumented saving() option that lets you capture the dataset that marginsplot would use; you can then create the graphs yourself. See

    Code:
    help margins saving

    Applied to your example

    Code:
    . use http://www.stata-press.com/data/r13/margex, clear
    (Artificial data for margins)
    
    . reg outcome c.ycn c.age c.age#c.ycn
    
    (output omitted)
    
    . tempfile tmp
    
    . margins, dydx (ycn) at (age=(20(1)60)) saving("`tmp'")
    
    Average marginal effects                        Number of obs     =      3,000
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    dy/dx w.r.t. : ycn
    
    1._at        : age             =          20
    
    2._at        : age             =          21
    
    3._at        : age             =          22
    
    4._at        : age             =          23
    
    5._at        : age             =          24
    
    (output omitted)
    
    41._at       : age             =          60
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    ycn          |
             _at |
              1  |  -.0006699   .0005732    -1.17   0.243    -.0017939     .000454
              2  |  -.0006123   .0005516    -1.11   0.267    -.0016938    .0004693
              3  |  -.0005546   .0005303    -1.05   0.296    -.0015944    .0004851
              4  |   -.000497   .0005093    -0.98   0.329    -.0014957    .0005016
              5  |  -.0004394   .0004888    -0.90   0.369    -.0013977     .000519
    
    (output omitted)
    
             41  |   .0016354   .0005895     2.77   0.006     .0004796    .0027913
    ------------------------------------------------------------------------------
    
    .
    . use "`tmp'" , clear
    (Created by command margins; also see char list)
    
    . list
    
         +------------------------------------------------------------------------------------------------------------------------------------+
         | _deriv   _term   _predict   _at          _atopt     _margin        _se   _statis~c    _pvalue      _ci_lb     _ci_ub   _at1   _at2 |
         |------------------------------------------------------------------------------------------------------------------------------------|
      1. |    ycn   _cons          .     1   age=(20(1)60)   -.0006699   .0005732   -1.168669   .2426302   -.0017939    .000454     .o     20 |
      2. |    ycn   _cons          .     2   age=(20(1)60)   -.0006123   .0005516   -1.110015   .2670818   -.0016938   .0004693     .o     21 |
      3. |    ycn   _cons          .     3   age=(20(1)60)   -.0005546   .0005303   -1.045947     .29567   -.0015944   .0004851     .o     22 |
      4. |    ycn   _cons          .     4   age=(20(1)60)    -.000497   .0005093   -.9758284   .3292283   -.0014957   .0005016     .o     23 |
      5. |    ycn   _cons          .     5   age=(20(1)60)   -.0004394   .0004888   -.8989505   .3687513   -.0013977    .000519     .o     24 |
         |------------------------------------------------------------------------------------------------------------------------------------|
    
    (output omitted)
    
     41. |    ycn   _cons          .    41   age=(20(1)60)    .0016354   .0005895    2.774313   .0055661    .0004796   .0027913     .o     60 |
         +------------------------------------------------------------------------------------------------------------------------------------+

    Best
    Daniel

    Comment

    Working...
    X