Announcement

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

  • Marginal Effects with indicator variable.

    Hi Folks,

    I'd like to calculate marginal effects for an OLS regression which includes an indicator variable. For ease this is the following regression.

    Code:
    regress loggrant logpop lpop19 logunemploy logsport  i.ysport, robust
    My indicator variable, ysport has four different categories 1 to 4.

    I've been reading the following paper on the Stata Journal, however, it appears they only deal with binary indicators (0 1)

    I would like to see calculate the marginal effect for logsport on the DV loggrant.

    http://www.stata-journal.com/sjpdf.h...iclenum=st0260

    I would like to carry out something similar to what is presented on page 319 of the above link.

    Any help welcome.
    Last edited by Sean O'Connor; 08 Jun 2016, 06:44.

  • #2
    Sean:
    are you looking for something along the following lines?
    Code:
    . reg price i.rep78 i.foreign i.rep78#i.foreign, robust
    note: 1b.rep78#1.foreign identifies no observations in the sample
    note: 2.rep78#1.foreign identifies no observations in the sample
    note: 5.rep78#1.foreign omitted because of collinearity
    
    Linear regression                               Number of obs     =         69
                                                    F(7, 61)          =       4.35
                                                    Prob > F          =     0.0006
                                                    R-squared         =     0.0428
                                                    Root MSE          =     3008.5
    
    -------------------------------------------------------------------------------
                  |               Robust
            price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
            rep78 |
               2  |   1403.125   1289.296     1.09   0.281    -1174.981    3981.231
               3  |   2042.574   786.1341     2.60   0.012     470.6017    3614.546
               4  |   1317.056   600.3089     2.19   0.032     116.6638    2517.447
               5  |       -360   323.5989    -1.11   0.270    -1007.076     287.076
                  |
          foreign |
         Foreign  |   2088.167   939.1478     2.22   0.030     210.2247    3966.109
                  |
    rep78#foreign |
       1#Foreign  |          0  (empty)
       2#Foreign  |          0  (empty)
       3#Foreign  |  -3866.574   1355.819    -2.85   0.006    -6577.702   -1155.446
       4#Foreign  |  -1708.278   1251.718    -1.36   0.177    -4211.242    794.6861
       5#Foreign  |          0  (omitted)
                  |
            _cons |     4564.5   277.8811    16.43   0.000     4008.842    5120.158
    -------------------------------------------------------------------------------
    
    . margins i.rep78 i.foreign i.rep78#i.foreign, atmeans
    
    Adjusted predictions                            Number of obs     =         69
    Model VCE    : Robust
    
    Expression   : Linear prediction, predict()
    at           : 1.rep78         =    .0289855 (mean)
                   2.rep78         =     .115942 (mean)
                   3.rep78         =    .4347826 (mean)
                   4.rep78         =    .2608696 (mean)
                   5.rep78         =    .1594203 (mean)
                   0.foreign       =    .6956522 (mean)
                   1.foreign       =    .3043478 (mean)
    
    -------------------------------------------------------------------------------
                  |            Delta-method
                  |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
            rep78 |
               1  |          .  (not estimable)
               2  |          .  (not estimable)
               3  |    6065.82   547.8941    11.07   0.000     4970.238    7161.402
               4  |   5997.174   417.4089    14.37   0.000     5162.513    6831.834
               5  |   4840.029   304.0684    15.92   0.000     4232.007    5448.051
                  |
          foreign |
        Domestic  |   5901.445    378.905    15.58   0.000     5143.778    6659.113
         Foreign  |          .  (not estimable)
                  |
    rep78#foreign |
      1#Domestic  |     4564.5   277.8811    16.43   0.000     4008.842    5120.158
       1#Foreign  |          .  (not estimable)
      2#Domestic  |   5967.625   1258.994     4.74   0.000     3450.111    8485.139
       2#Foreign  |          .  (not estimable)
      3#Domestic  |   6607.074   735.3836     8.98   0.000     5136.584    8077.564
       3#Foreign  |   4828.667   644.5603     7.49   0.000     3539.789    6117.545
      4#Domestic  |   5881.556   532.1211    11.05   0.000     4817.514    6945.597
       4#Foreign  |   6261.444    633.755     9.88   0.000     4994.173    7528.716
      5#Domestic  |     4204.5   165.8262    25.35   0.000      3872.91     4536.09
       5#Foreign  |   6292.667   924.3918     6.81   0.000     4444.231    8141.102
    -------------------------------------------------------------------------------
    As an aside, your code can be even more efficient:
    Code:
    regress price i.rep78##i.foreign, robust
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Since logsport is continuous, you can just say something like

      margins, dydx(logsport)

      However, Patrick Royston's mcp command lets you do nice things that help with interpreting the effects of continuous variables. For a summary, see

      http://www3.nd.edu/~rwilliam/xsoc73994/Margins03.pdf

      Or, see Royston's article at

      http://www.stata-journal.com/article...article=gr0056
      -------------------------------------------
      Richard Williams
      Professor Emeritus of Sociology
      University of Notre Dame
      StataNow Version: 19.5 MP (2 processor)

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

      Comment


      • #4
        Carlo and Richard,

        Thank you for your helpful comments.

        Carlo what I'd like to do with the eventual output is create a table which looks at the estimate Marginal Effect of Distance (logsport) by different Ministers (ysport). The dependent variable would be loggrant. The table in the end would look like something as follows.

        Ysport dy/dx z
        1 -0.035** (0.85)
        2
        3
        4

        I imagine there is a large degree of heterogeneity amongst different Minister's awarding larger grants to their located. So I would like to estimate the marginal effects to examine this.

        Comment


        • #5
          Originally posted by Richard Williams View Post
          Since logsport is continuous, you can just say something like

          margins, dydx(logsport)

          However, Patrick Royston's mcp command lets you do nice things that help with interpreting the effects of continuous variables. For a summary, see

          http://www3.nd.edu/~rwilliam/xsoc73994/Margins03.pdf

          Or, see Royston's article at

          http://www.stata-journal.com/article...article=gr0056
          Hi Richard,

          Thank you for your links. I've read through them and I've a few further questions which I hop I could ask you?

          Code:
           
          
           regress loggrant logpop lpop19 logunemploy logurban c.logsport##i.ysport logfin, robust
          
          Linear regression                               Number of obs     =      7,615
                                                          F(79, 7535)       =      47.04
                                                          Prob > F          =     0.0000
                                                          R-squared         =     0.3052
                                                          Root MSE          =     1.0577
          
          -----------------------------------------------------------------------------------
                            |               Robust
                   loggrant |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          ------------------+----------------------------------------------------------------
                     logpop |   .0950617   .0965349     0.98   0.325    -.0941736    .2842969
                     lpop19 |    .059837   .0746141     0.80   0.423    -.0864275    .2061016
                logunemploy |  -.0921323    .043174    -2.13   0.033    -.1767654   -.0074993
                   logurban |    .025675   .0128149     2.00   0.045     .0005542    .0507958
                   logsport |  -.1550974   .0529415    -2.93   0.003    -.2588776   -.0513173
                            |
                     ysport |
                         2  |  -.2633332   .3411057    -0.77   0.440    -.9319956    .4053292
                         3  |  -.8969121   .3932313    -2.28   0.023    -1.667755   -.1260691
                         4  |  -1.416553   .3510297    -4.04   0.000    -2.104669   -.7284371
                            |
          ysport#c.logsport |
                         2  |   .0535929   .0643183     0.83   0.405    -.0724889    .1796748
                         3  |   .1194119   .0774986     1.54   0.123    -.0325069    .2713307
                         4  |   .1957342   .0681619     2.87   0.004     .0621179    .3293506
                            |
                     logfin |   .0779167   .0831441     0.94   0.349    -.0850689    .2409023
                   logtaois |  -.1074037    .022681    -4.74   0.000    -.1518647   -.0629426
          
          
          
          
          margins, dydx(logsport)
          
          Average marginal effects                        Number of obs     =      7,615
          Model VCE    : Robust
          
          Expression   : Linear prediction, predict()
          dy/dx w.r.t. : logsport
          
          ------------------------------------------------------------------------------
                       |            Delta-method
                       |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
              logsport |   -.056458   .0192533    -2.93   0.003    -.0941999   -.0187162
          ------------------------------------------------------------------------------
          The marginal effects which is show below is for the entire sample, but I would like to be able to calculate the marginal effects for different periods, identified by (ysport).

          So the marginal effects when ysport 1 was in charge, when ysport 2 was in charge and so on.

          Just to clarify the main variables of interest loggrant - is the natural log of a grant awarded to an individual.
          logsport is the log distance between the Minister for Sport and that individual.
          While ysport identifies whether if Minister 1, 2, 3 or 4 was in charge for the award of that grant.

          Comment


          • #6
            Something like this maybe?

            Code:
            webuse nhanes2f, clear
            reg weight i.race##c.age
            margins, dydx(age) at(race = (1 2 3))
            But also consider something like

            Code:
            mcp age race
            -------------------------------------------
            Richard Williams
            Professor Emeritus of Sociology
            University of Notre Dame
            StataNow Version: 19.5 MP (2 processor)

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

            Comment


            • #7
              Originally posted by Richard Williams View Post
              Something like this maybe?

              Code:
              webuse nhanes2f, clear
              reg weight i.race##c.age
              margins, dydx(age) at(race = (1 2 3))
              But also consider something like

              Code:
              mcp age race
              Yes, this is what I had in mind.

              Thank you very much for being so helpful.

              Comment


              • #8
                Richard and others. I've a slight issue which perhaps you could she some light over.

                I've been estimating regressions again in order to ensure their robustness. However, while previously I was able to produce output, for some reason I cannot now.

                Why would one set of marginal effects read not estimable while the other does not?


                Both are using the same club level observation, it is just the dependent variable changes from the natural log of a grant receive for club I to the percentage of grant club I received relative to what it sought.

                Code:
                 regress loggrant logpop pop19p unemploymaterate logurban logsport logfin logtaois c.logaa##i.ygaa i.sportcode i.countyc, robust
                note: 1.sportcode omitted because of collinearity
                
                Linear regression                               Number of obs     =      2,627
                                                                F(41, 2585)       =      10.21
                                                                Prob > F          =     0.0000
                                                                R-squared         =     0.1276
                                                                Root MSE          =      .8661
                
                ----------------------------------------------------------------------------------
                                 |               Robust
                        loggrant |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -----------------+----------------------------------------------------------------
                          logpop |   .0605056   .0279782     2.16   0.031     .0056438    .1153675
                          pop19p |   .8775311   .5476751     1.60   0.109    -.1963953    1.951457
                unemploymaterate |   -1.11561   .4545235    -2.45   0.014    -2.006877   -.2243427
                        logurban |    .043079   .0199643     2.16   0.031     .0039315    .0822266
                        logsport |  -.0570515   .0300878    -1.90   0.058    -.1160502    .0019472
                          logfin |  -.1258532   .0308427    -4.08   0.000     -.186332   -.0653744
                        logtaois |  -.0911275    .036007    -2.53   0.011    -.1617331    -.020522
                           logaa |  -.0200908    .082004    -0.24   0.806     -.180891    .1407093
                                 |
                            ygaa |
                              2  |  -.2141418   .5019458    -0.43   0.670    -1.198398    .7701147
                              3  |  -.0095067   .4776462    -0.02   0.984    -.9461145    .9271011
                              4  |   -.506431    .543523    -0.93   0.352    -1.572215    .5593535
                              5  |  -.0118189   .5781753    -0.02   0.984    -1.145553    1.121915
                                 |
                    ygaa#c.logaa |
                              2  |   .0392937   .1006777     0.39   0.696    -.1581233    .2367108
                              3  |  -.0107798   .0985369    -0.11   0.913    -.2039991    .1824395
                              4  |   .0232533   .1140331     0.20   0.838    -.2003523    .2468588
                              5  |  -.0816977   .1208182    -0.68   0.499     -.318608    .1552126
                                 |
                     1.sportcode |          0  (omitted)
                                 |
                         countyc |
                              2  |   .5773329   .1469035     3.93   0.000     .2892724    .8653934
                              3  |   .2414504    .134505     1.80   0.073    -.0222981    .5051989
                              4  |   .5713599   .1589695     3.59   0.000     .2596393    .8830804
                              5  |   .3338642   .1226216     2.72   0.007     .0934178    .5743106
                              6  |   .4792108   .1585397     3.02   0.003     .1683331    .7900884
                              7  |  -.1529553   .1410699    -1.08   0.278    -.4295768    .1236662
                              8  |   .4655255   .1308127     3.56   0.000     .2090173    .7220336
                              9  |    .356066   .1299818     2.74   0.006      .101187    .6109449
                             10  |   .1899797   .1471386     1.29   0.197    -.0985417    .4785012
                             11  |    .509035   .1255032     4.06   0.000      .262938     .755132
                             12  |   .1537782   .1535999     1.00   0.317    -.1474131    .4549694
                             13  |   .3840156   .1414846     2.71   0.007      .106581    .6614502
                             14  |   .1149066   .1513671     0.76   0.448    -.1819063    .4117196
                             15  |   .3791467   .1513002     2.51   0.012     .0824648    .6758286
                             16  |     .39787   .1236422     3.22   0.001     .1554222    .6403177
                             17  |   .3131271   .1389154     2.25   0.024     .0407303    .5855239
                             18  |   .4636262   .1363336     3.40   0.001     .1962922    .7309602
                             19  |   .5601544   .1286226     4.36   0.000     .3079406    .8123682
                             20  |   .5550848   .1371118     4.05   0.000     .2862247    .8239449
                             21  |   .4038945   .1325943     3.05   0.002     .1438928    .6638962
                             22  |   .4464482   .1354653     3.30   0.001     .1808167    .7120796
                             23  |   .2680429   .1461616     1.83   0.067    -.0185628    .5546487
                             24  |   .3259274   .1468014     2.22   0.026     .0380672    .6137875
                             25  |   .3425764   .1935557     1.77   0.077    -.0369636    .7221163
                             26  |   .7226457   .1490302     4.85   0.000     .4304151    1.014876
                                 |
                           _cons |   11.30486   .4572681    24.72   0.000     10.40821    12.20151
                ----------------------------------------------------------------------------------
                
                .
                end of do-file
                
                . do "C:\Users\SEANOC~1\AppData\Local\Temp\STD08000000.tmp"
                
                . margins, dydx(logaa) at(ygaa = (1 2 3 4 5))
                
                Average marginal effects                        Number of obs     =      2,627
                Model VCE    : Robust
                
                Expression   : Linear prediction, predict()
                dy/dx w.r.t. : logaa
                
                1._at        : ygaa            =           1
                
                2._at        : ygaa            =           2
                
                3._at        : ygaa            =           3
                
                4._at        : ygaa            =           4
                
                5._at        : ygaa            =           5
                
                ------------------------------------------------------------------------------
                             |            Delta-method
                             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                logaa        |
                         _at |
                          1  |          .  (not estimable)
                          2  |          .  (not estimable)
                          3  |          .  (not estimable)
                          4  |          .  (not estimable)
                          5  |          .  (not estimable)
                ------------------------------------------------------------------------------
                
                .
                end of do-file
                
                . do "C:\Users\SEANOC~1\AppData\Local\Temp\STD08000000.tmp"
                
                . regress rec logpop pop19p unemploymaterate logurban logsport ysport logfin logtaois c.logaa##i.ygaa i.countyc, robust
                
                Linear regression                               Number of obs     =      2,627
                                                                F(42, 2584)       =      10.22
                                                                Prob > F          =     0.0000
                                                                R-squared         =     0.1403
                                                                Root MSE          =     .20047
                
                ----------------------------------------------------------------------------------
                                 |               Robust
                             rec |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -----------------+----------------------------------------------------------------
                          logpop |   .0064538   .0064873     0.99   0.320     -.006267    .0191745
                          pop19p |  -.4061217   .1194183    -3.40   0.001     -.640287   -.1719564
                unemploymaterate |    .205774    .104196     1.97   0.048     .0014578    .4100901
                        logurban |  -.0152763    .004708    -3.24   0.001    -.0245082   -.0060445
                        logsport |  -.0007369   .0068706    -0.11   0.915    -.0142094    .0127356
                          ysport |  -.1042729   .0137782    -7.57   0.000    -.1312902   -.0772555
                          logfin |   .0182026   .0068797     2.65   0.008     .0047123    .0316929
                        logtaois |   .0337872   .0080341     4.21   0.000     .0180332    .0495412
                           logaa |  -.0053066    .022042    -0.24   0.810    -.0485284    .0379151
                                 |
                            ygaa |
                              2  |   .0763601   .1288192     0.59   0.553    -.1762391    .3289594
                              3  |  -.0190825   .1252304    -0.15   0.879    -.2646445    .2264796
                              4  |   .2973404   .1422586     2.09   0.037     .0183879    .5762928
                              5  |   .5188462   .1496593     3.47   0.001     .2253818    .8123106
                                 |
                    ygaa#c.logaa |
                              2  |   .0047996   .0256463     0.19   0.852    -.0454898    .0550891
                              3  |   .0171763    .025197     0.68   0.496    -.0322322    .0665847
                              4  |   .0011399   .0281624     0.04   0.968    -.0540832     .056363
                              5  |  -.0345081    .029769    -1.16   0.246    -.0928817    .0238655
                                 |
                         countyc |
                              2  |   .1573928   .0341617     4.61   0.000     .0904056    .2243799
                              3  |  -.0236976   .0293125    -0.81   0.419    -.0811759    .0337807
                              4  |   .0522691    .032802     1.59   0.111    -.0120518    .1165901
                              5  |  -.0384979   .0296062    -1.30   0.194    -.0965522    .0195563
                              6  |   .0107357   .0357817     0.30   0.764     -.059428    .0808995
                              7  |   .0811444   .0302947     2.68   0.007       .02174    .1405488
                              8  |   .0296735   .0309596     0.96   0.338    -.0310346    .0903816
                              9  |  -.1174776   .0285898    -4.11   0.000    -.1735387   -.0614164
                             10  |  -.0509397   .0356745    -1.43   0.153    -.1208933    .0190139
                             11  |  -.0521637   .0287086    -1.82   0.069    -.1084578    .0041305
                             12  |  -.1070167   .0365796    -2.93   0.003     -.178745   -.0352885
                             13  |   .0081474   .0316597     0.26   0.797    -.0539335    .0702284
                             14  |   .0254931   .0359658     0.71   0.479    -.0450317    .0960179
                             15  |  -.1285272   .0356621    -3.60   0.000    -.1984563    -.058598
                             16  |  -.0538689   .0298362    -1.81   0.071    -.1123743    .0046364
                             17  |    .008566   .0331221     0.26   0.796    -.0563825    .0735145
                             18  |   -.147201   .0334724    -4.40   0.000    -.2128364   -.0815656
                             19  |  -.0801821   .0331743    -2.42   0.016    -.1452331   -.0151312
                             20  |   -.083589   .0306284    -2.73   0.006    -.1436476   -.0235303
                             21  |  -.0978557   .0335342    -2.92   0.004    -.1636124    -.032099
                             22  |  -.0722941   .0335803    -2.15   0.031    -.1381411   -.0064472
                             23  |  -.0938154   .0345987    -2.71   0.007    -.1616594   -.0259714
                             24  |  -.1047901   .0331863    -3.16   0.002    -.1698645   -.0397157
                             25  |  -.1574038   .0490724    -3.21   0.001    -.2536289   -.0611787
                             26  |  -.1087259    .037155    -2.93   0.003    -.1815825   -.0358694
                                 |
                           _cons |   .6580066    .119348     5.51   0.000     .4239793     .892034
                ----------------------------------------------------------------------------------
                
                .
                end of do-file
                
                . do "C:\Users\SEANOC~1\AppData\Local\Temp\STD08000000.tmp"
                
                . margins, dydx(logaa) at(ygaa = (1 2 3 4 5))
                
                Average marginal effects                        Number of obs     =      2,627
                Model VCE    : Robust
                
                Expression   : Linear prediction, predict()
                dy/dx w.r.t. : logaa
                
                1._at        : ygaa            =           1
                
                2._at        : ygaa            =           2
                
                3._at        : ygaa            =           3
                
                4._at        : ygaa            =           4
                
                5._at        : ygaa            =           5
                
                ------------------------------------------------------------------------------
                             |            Delta-method
                             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                logaa        |
                         _at |
                          1  |  -.0053066    .022042    -0.24   0.810    -.0485284    .0379151
                          2  |   -.000507   .0105733    -0.05   0.962      -.02124     .020226
                          3  |   .0118696   .0127701     0.93   0.353     -.013171    .0369103
                          4  |  -.0041667   .0169859    -0.25   0.806    -.0374741    .0291407
                          5  |  -.0398148   .0227644    -1.75   0.080    -.0844531    .0048236
                ------------------------------------------------------------------------------

                Comment


                • #9
                  Here is something I don't quite get from this whole discussion. You're using regress. The marginal effects are the estimates of the parameters you get directly from the regression. Now you've interacted a categorical variable yqaa with a continuous variable, logaa. You want to calculate the marginal effect at each of the categories of the categorical variable, which is simply the summation of the coefficients. Notice in your last example: the marginal effect at 1.yqaa is the value of the coefficient for logaa since that is the base category (the one left out). The rest simply add the coefficients on the interaction terms to the base category's coefficient. So the marginal effect of logaa at 2.yqaa is the coefficient on logaa plus the coefficient on 2.yqaa#c.logaa. This is a simple linear combination, that you could even do yourself. You would have to calculate the standard error, but remember that it's the square root of the sample variance of the sum of two estimators, and the variance of a sum is the variance of the first variable plus the variance of the second variable plus 2 times the covariance. Once you have the standard error of the sum you can get the rest of the values that margins calculates for you. If you don't want to do the linear combination by hand, Stata, being the wonderful software it is, provides you with a command that does it for you lincom. Consider the following:
                  Code:
                  sysuse auto, clear
                  
                  regress price i.foreign##c.mpg, vce(robust)
                  
                  forval i = 0/1 {
                      lincom mpg + `i'.foreign#mpg
                  }
                  
                  margins, dydx(mpg) at(foreign = (0/1))
                  This produces the following output
                  Code:
                  . sysuse auto, clear
                  (1978 Automobile Data)
                  
                  . 
                  . regress price i.foreign##c.mpg, vce(robust)
                  
                  Linear regression                                      Number of obs =      74
                                                                         F(  3,    70) =    8.45
                                                                         Prob > F      =  0.0001
                                                                         R-squared     =  0.2888
                                                                         Root MSE      =  2540.1
                  
                  -------------------------------------------------------------------------------
                                |               Robust
                          price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  --------------+----------------------------------------------------------------
                        foreign |
                       Foreign  |  -13.58741   2920.867    -0.00   0.996    -5839.072    5811.897
                            mpg |  -329.2551   81.86064    -4.02   0.000     -492.521   -165.9892
                                |
                  foreign#c.mpg |
                       Foreign  |   78.88826   119.0867     0.66   0.510    -158.6226    316.3991
                                |
                          _cons |   12600.54   1792.323     7.03   0.000     9025.862    16175.21
                  -------------------------------------------------------------------------------
                  
                  . 
                  . forval i = 0/1 {
                    2.         lincom mpg + `i'.foreign#mpg
                    3. }
                  
                   ( 1)  mpg + 0b.foreign#co.mpg = 0
                  
                  ------------------------------------------------------------------------------
                         price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                           (1) |  -329.2551   81.86064    -4.02   0.000     -492.521   -165.9892
                  ------------------------------------------------------------------------------
                  
                   ( 1)  mpg + 1.foreign#c.mpg = 0
                  
                  ------------------------------------------------------------------------------
                         price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                           (1) |  -250.3668    86.4897    -2.89   0.005    -422.8651   -77.86854
                  ------------------------------------------------------------------------------
                  
                  . 
                  . margins, dydx(mpg) at(foreign = (0/1))
                  
                  Average marginal effects                          Number of obs   =         74
                  Model VCE    : Robust
                  
                  Expression   : Linear prediction, predict()
                  dy/dx w.r.t. : mpg
                  
                  1._at        : foreign         =           0
                  
                  2._at        : foreign         =           1
                  
                  ------------------------------------------------------------------------------
                               |            Delta-method
                               |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                  mpg          |
                           _at |
                            1  |  -329.2551   81.86064    -4.02   0.000     -492.521   -165.9892
                            2  |  -250.3668    86.4897    -2.89   0.005    -422.8651   -77.86854
                  ------------------------------------------------------------------------------
                  You can see that in this case they are both equivalent, because it's a linear model. This would not be the case with other estimation commands, but in your case you can use either. So if one's not working, check the other. The only difference I see between your two estimations is that you're not including sportcode in the second one. As to why that would stop margins to work after the first one I don't know. Did you try to run the first regression without that variable and then get the marginal effects?

                  I tried to see if lincom would take more than one linear combination at the same time, but I wasn't able to do it, so that's why I created the loop. Maybe someone knows how to do more than one linear combination in the same command line.
                  Alfonso Sanchez-Penalver

                  Comment


                  • #10
                    I just realized that I didn't show you a code of how to do this by "hand". In case neither margins nor lincom work. Here's the code
                    Code:
                    sysuse auto, clear
                    
                    regress price i.foreign##c.mpg, vce(robust)
                    
                    // Do it yourself
                    mat varcov = e(V)
                    scalar cv = invttail(e(df_r),0.025)
                    local col = colnumb(varcov, "mpg")
                    forval i = 0/1 {
                        if "`i'" == "1" di ""
                        di as text "Marginal effect for Category `i'"
                        di "{hline}"
                        scalar coeff = _b[mpg] + _b[`i'.foreign#mpg]
                        local row = rownumb(varcov, "`i'.foreign#mpg")
                        scalar se = sqrt(varcov[`row',`row'] + varcov[`col',`col'] + 2*varcov[`row',`col'])
                        di as text "Cofficient:       " as result coeff
                        di as text "Std. Error:       " as result se
                        di as text "t:                " as result coeff/se
                        di as text "P > |t|:          " as result 2*ttail(e(df_r), abs(coeff/se))
                        di as text "95% ci low bound: " as result coeff - cv*se
                        di as text "95% ci up bound:  " as result coeff + cv*se
                    }
                    Here's the output of the loop:
                    Code:
                    Marginal effect for Category 0
                    ------------------------------------------------------------------------------------------------------------
                    Cofficient:       -329.25507
                    Std. Error:       81.860644
                    t:                -4.0221412
                    P > |t|:          .00014376
                    95% ci low bound: -492.52097
                    95% ci up bound:  -165.98916
                    
                    Marginal effect for Category 1
                    ------------------------------------------------------------------------------------------------------------
                    Cofficient:       -250.36681
                    Std. Error:       86.489701
                    t:                -2.8947586
                    P > |t|:          .00505679
                    95% ci low bound: -422.86508
                    95% ci up bound:  -77.86854
                    I haven't presented the output of the regression because you already have it in my previous post, and you can check the numbers of the marginal effects here against the marginal effects in my previous post as well. I hope all this helps.
                    Alfonso Sanchez-Penalver

                    Comment


                    • #11
                      Firstly, than you for your detailed replies I have one slight question in regards to your helpful code in #9.

                      Matching your code but including 5 indicators leaves me with the invalid syntax error.

                      Is there a mistake in what I've written?

                      Code:
                       forval i = 1/2/3/4/5 {
                        2.                 lincom logaa + `i'.ygaa#logaa  
                        3. }
                      invalid syntax

                      Comment


                      • #12
                        you don't need the extra characters; just
                        Code:
                        forval i=1/5 {
                        is all that is necessary and it is sufficient

                        Comment

                        Working...
                        X