Announcement

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

  • Finding the marginal effects of interactions terms

    Dear Stata Forum,

    I would like to determine the marginal effects of the interaction terms
    Code:
    ib1998.year#c.logbite3
    and graph them on the vertical axis and year on the horizontal axis.
    The regression I am running is the following:
    Code:
    regress self_c ib1998.year##c.logbite3
    Code:
    margins, dydx(c.logbite3) at(year(1993(1)2022))
    However, the margins command gives me an error message.
    I would appreciate you help.
    Here you find a data sample:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(self_c logbite3 year)
             .          . 1991
             .          . 1992
             .          . 1993
             .          . 1994
             .          . 1995
             .          . 1996
             0          . 1997
             0 -2.0409904 1998
             0  -.7741088 1999
             0  -.4291192 2000
             0  -.6162011 2001
            .2  -.6442134 2002
           .25 -.40889165 2003
      .3333333  -.7611812 2004
             0 -1.2132735 2005
             0 -1.0570409 2006
             0  -1.248903 2007
             0 -1.3217868 2008
    .037037037 -1.3166054 2009
           .05  -.7443178 2010
      .0952381  -.7682025 2011
             0  -.8468925 2012
           .05    -.70977 2013
    .071428575  -.7923768 2014
      .0952381    -.74603 2015
           .05  -.8348222 2016
     .05263158  -.6987573 2017
             0  -.6215868 2018
     .07692308  -.6011775 2019
             0  -.5080077 2020
             0   -.419554 2021
             0  -.4576086 2022
             .          . 1991
             0          . 1992
             0 -1.6483593 1993
             0 -1.7577775 1994
             0  -1.416572 1995
             0 -1.7994494 1996
             0  -1.833351 1997
     .11111111 -1.7447444 1998
          .125  -.6034867 1999
             0 -.50562984 2000
             0  -.4476819 2001
      .3333333 -.54213685 2002
      .3333333  -.9721524 2003
      .3333333  -.8914877 2004
             1 -1.2174888 2005
      .3333333          . 2006
      .3333333 -1.1037213 2007
             1  -.9452747 2008
             0          . 2009
             0  -.5233285 2010
     .15384616  -.3239966 2011
      .0909091  -.4137999 2012
     .08333334  -.3003126 2013
    .037037037 -.31665915 2014
     .04761905   -.437026 2015
     .26086956 -.25689384 2016
      .0909091  -.3406061 2017
           .05 -.15568754 2018
           .08  -.4489613 2019
     .05555556  -.1754757 2020
     .08695652 -.27745017 2021
             0 -.27312517 2022
             .          . 1991
      .0952381          . 1992
     .08333334  -1.714637 1993
           .08  -1.860475 1994
      .0909091   -1.81326 1995
     .08333334 -2.0264544 1996
     .04545455  -1.948107 1997
             0 -2.0730786 1998
             0  -.6481928 1999
             0 -.58449304 2000
             0  -.4257042 2001
             0  -.2682134 2002
             0  -.3425188 2003
             0 -.29530093 2004
             0  -.4778255 2005
             0  -.4895479 2006
             0  -.3615529 2007
             0  -.6257899 2008
     .08333334 -.43232635 2009
    .014492754 -.56207335 2010
     .05714286  -.3648208 2011
     .09756097  -.5524424 2012
    .071428575 -.57270056 2013
     .04878049  -.4556111 2014
     .08333334  -.4946106 2015
      .2051282  -.4723745 2016
     .11538462  -.4804807 2017
     .14285715 -.30491325 2018
     .13333334  -.4541697 2019
     .11111111   -.226769 2020
    .071428575   -.307143 2021
             0  -.2073963 2022
             .          . 1991
     .03921569          . 1992
             0 -1.7754605 1993
     .04651163  -1.861579 1994
    end


  • #2
    I would appreciate your help.
    Thanks,
    Best,
    Nico

    Comment


    • #3
      You need to type


      Code:
      margins, dydx(c.logbite3) at(year=(1993(1)2022))
      Best wishes

      (Stata 16.1 MP)

      Comment


      • #4
        Hi Felix,
        Thanks for your reply, but that was just a typo of mine. The same issue remains:
        Code:
         margins, dydx(c.logbite3) at(year=(1993(1)2023))
        invalid at() option;
        variable year may not be present in model as factor and continuous predictor
        I would appreciate further help.
        Thanks,
        Nico

        Comment


        • #5
          Then try

          Code:
           regress self_c i.year##c.logbite3
          The reference coding does not affect the margins estimation.
          Best wishes

          (Stata 16.1 MP)

          Comment


          • #6
            I don't think O.P. is showing us what actually happened.
            margins, dydx(c.logbite3) at(year=(1993(1)2023))
            invalid at() option;
            variable year may not be present in model as factor and continuous predictor
            simply is not what happens. That error message only appears when some variable appears as both a discrete and continuous explanatory variable in the regression, or when it is treated as one of those in the regression and the other in -margins-. None of those things happen in the code that has been shown so far.

            With the correction of the omitted = in the -at()- option the code shown by O.P. runs without error messages and produces sensible results, at least in the example data:
            Code:
            . regress self_c ib1998.year##c.logbite3
            
                  Source |       SS           df       MS      Number of obs   =        85
            -------------+----------------------------------   F(59, 25)       =      0.34
                   Model |  1.05139164        59  .017820197   Prob > F        =    0.9996
                Residual |  1.30651651        25   .05226066   R-squared       =    0.4459
            -------------+----------------------------------   Adj R-squared   =   -0.8618
                   Total |  2.35790815        84  .028070335   Root MSE        =    .22861
            
            ---------------------------------------------------------------------------------
                     self_c | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            ----------------+----------------------------------------------------------------
                       year |
                      1993  |   -.730753   4.695339    -0.16   0.878    -10.40098    8.939478
                      1994  |  -1.798479   5.254437    -0.34   0.735     -12.6202    9.023238
                      1995  |  -1.051059   2.194853    -0.48   0.636    -5.571443    3.469325
                      1996  |  -1.386999   3.241528    -0.43   0.672    -8.063052    5.289054
                      1997  |  -1.452608     5.6088    -0.26   0.798    -13.00415    10.09893
                      1999  |  -.2977607   2.144436    -0.14   0.891     -4.71431    4.118789
                      2000  |  -.7264229   2.046143    -0.36   0.726    -4.940533    3.487688
                      2001  |  -.7264229   1.915604    -0.38   0.708    -4.671684    3.218838
                      2002  |  -.8754679     1.7997    -0.49   0.631     -4.58202    2.831085
                      2003  |  -.7507196   1.774401    -0.42   0.676    -4.405167    2.903727
                      2004  |  -.8941742   1.785658    -0.50   0.621    -4.571806    2.783457
                      2005  |  -1.055966   1.792174    -0.59   0.561    -4.747017    2.635085
                      2006  |  -.7264229   1.810862    -0.40   0.692    -4.455963    3.003117
                      2007  |  -.7477614   1.780687    -0.42   0.678    -4.415154    2.919631
                      2008  |  -.3175785   1.810143    -0.18   0.862    -4.045638    3.410481
                      2009  |  -.6204551   1.785318    -0.35   0.731    -4.297386    3.056475
                      2010  |  -.8369212   1.942798    -0.43   0.670    -4.838188    3.164346
                      2011  |   -.604723   1.782885    -0.34   0.737    -4.276643    3.067196
                      2012  |  -.5252647   1.808746    -0.29   0.774    -4.250447    3.199917
                      2013  |  -.6180313   1.801076    -0.34   0.734    -4.327416    3.091354
                      2014  |  -.7112244   1.787518    -0.40   0.694    -4.392686    2.970237
                      2015  |   -.719247   1.838225    -0.39   0.699    -4.505142    3.066648
                      2016  |  -.3607673   1.777555    -0.20   0.841     -4.02171    3.300176
                      2017  |  -.5779592   1.811707    -0.32   0.752    -4.309239    3.153321
                      2018  |    -.60406   1.771019    -0.34   0.736    -4.251541    3.043421
                      2019  |  -.5347362   1.989021    -0.27   0.790    -4.631202     3.56173
                      2020  |  -.5969107   1.775244    -0.34   0.739    -4.253094    3.059273
                      2021  |  -.4666381   1.896701    -0.25   0.808    -4.372967    3.439691
                      2022  |  -.7264229   1.796744    -0.40   0.689    -4.426886    2.974041
                            |
                   logbite3 |   .3529994   .8930221     0.40   0.696    -1.486214    2.192213
                            |
            year#c.logbite3 |
                      1993  |   -.371745   2.695096    -0.14   0.891    -5.922398    5.178908
                      1994  |  -.9629962   2.854872    -0.34   0.739    -6.842715    4.916723
                      1995  |  -.5821697   1.209008    -0.48   0.634    -3.072168    1.907829
                      1996  |  -.7200985   1.681011    -0.43   0.672    -4.182205    2.742008
                      1997  |  -.7490968    2.95541    -0.25   0.802    -6.835878    5.337684
                      1999  |   .2201042    2.03365     0.11   0.915    -3.968277    4.408486
                      2000  |  -.3529994   2.264237    -0.16   0.877    -5.016283    4.310285
                      2001  |  -.3529994   1.789748    -0.20   0.845    -4.039054    3.333055
                      2002  |  -1.027063   1.220113    -0.84   0.408    -3.539933    1.485808
                      2003  |   -.733736   1.007877    -0.73   0.473    -2.809497    1.342025
                      2004  |   -.953584   1.031243    -0.92   0.364    -3.077469    1.170301
                      2005  |  -1.036709   .9703552    -1.07   0.296    -3.035193    .9617747
                      2006  |  -.3529994   1.059264    -0.33   0.742    -2.534594    1.828596
                      2007  |  -.4993969   .9554215    -0.52   0.606    -2.467124    1.468331
                      2008  |  -.2746915   1.006368    -0.27   0.787    -2.347345    1.797962
                      2009  |  -.3006445   .9649643    -0.31   0.758    -2.288026    1.686737
                      2010  |  -.5694192   1.635231    -0.35   0.731    -3.937241    2.798403
                      2011  |  -.3125933   1.109479    -0.28   0.780    -2.597609    1.972422
                      2012  |  -.1241117   1.153986    -0.11   0.915    -2.500791    2.252567
                      2013  |  -.2769228   1.182801    -0.23   0.817    -2.712947    2.159102
                      2014  |  -.4243578   1.110934    -0.38   0.706     -2.71237    1.863655
                      2015  |  -.4749919   1.328613    -0.36   0.724    -3.211321    2.261337
                      2016  |   .0184426   1.050629     0.02   0.986    -2.145369    2.182254
                      2017  |   -.230312    1.26472    -0.18   0.857    -2.835052    2.374428
                      2018  |  -.1920001   1.122113    -0.17   0.866    -2.503035    2.119035
                      2019  |  -.1636741   2.072787    -0.08   0.938    -4.432659     4.10531
                      2020  |  -.1092542   1.269949    -0.09   0.932    -2.724764    2.506256
                      2021  |   .2654052   2.334138     0.11   0.910    -4.541842    5.072653
                      2022  |  -.3529994   1.533098    -0.23   0.820    -3.510475    2.804476
                            |
                      _cons |   .7264229   1.749004     0.42   0.681    -2.875718    4.328564
            ---------------------------------------------------------------------------------
            
            .
            . margins, dydx(c.logbite3) at(year=(1993(1)2022))
            
            Average marginal effects                                    Number of obs = 85
            Model VCE: OLS
            
            Expression: Linear prediction, predict()
            dy/dx wrt:  logbite3
            1._at:  year = 1993
            2._at:  year = 1994
            3._at:  year = 1995
            4._at:  year = 1996
            5._at:  year = 1997
            6._at:  year = 1998
            7._at:  year = 1999
            8._at:  year = 2000
            9._at:  year = 2001
            10._at: year = 2002
            11._at: year = 2003
            12._at: year = 2004
            13._at: year = 2005
            14._at: year = 2006
            15._at: year = 2007
            16._at: year = 2008
            17._at: year = 2009
            18._at: year = 2010
            19._at: year = 2011
            20._at: year = 2012
            21._at: year = 2013
            22._at: year = 2014
            23._at: year = 2015
            24._at: year = 2016
            25._at: year = 2017
            26._at: year = 2018
            27._at: year = 2019
            28._at: year = 2020
            29._at: year = 2021
            30._at: year = 2022
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
            logbite3     |
                     _at |
                      1  |  -.0187457   2.542843    -0.01   0.994    -5.255829    5.218338
                      2  |  -.6099968   2.711606    -0.22   0.824    -6.194653     4.97466
                      3  |  -.2291703   .8149921    -0.28   0.781    -1.907678    1.449337
                      4  |  -.3670991   1.424187    -0.26   0.799    -3.300267    2.566069
                      5  |  -.3960974   2.817261    -0.14   0.889    -6.198355     5.40616
                      6  |   .3529994   .8930221     0.40   0.696    -1.486214    2.192213
                      7  |   .5731036   1.827087     0.31   0.756    -3.189852    4.336059
                      8  |   3.86e-14   2.080693     0.00   1.000    -4.285267    4.285267
                      9  |   2.44e-15   1.551035     0.00   1.000    -3.194416    3.194416
                     10  |  -.6740635   .8313773    -0.81   0.425    -2.386317     1.03819
                     11  |  -.3807366   .4672547    -0.81   0.423    -1.343066    .5815924
                     12  |  -.6005846    .515727    -1.16   0.255    -1.662744     .461575
                     13  |  -.6837098   .3796059    -1.80   0.084    -1.465523    .0981032
                     14  |  -1.22e-15   .5696944    -0.00   1.000    -1.173307    1.173307
                     15  |  -.1463975     .33962    -0.43   0.670    -.8458581     .553063
                     16  |   .0783079   .4639912     0.17   0.867    -.8772999    1.033916
                     17  |   .0523549   .3656058     0.14   0.887    -.7006244    .8053341
                     18  |  -.2164198   1.369852    -0.16   0.876    -3.037682    2.604842
                     19  |   .0404061   .6583737     0.06   0.952     -1.31554    1.396352
                     20  |   .2288877   .7308868     0.31   0.757    -1.276402    1.734177
                     21  |   .0760766   .7755837     0.10   0.923    -1.521268    1.673421
                     22  |  -.0713584   .6608229    -0.11   0.915    -1.432349    1.289632
                     23  |  -.1219926   .9837294    -0.12   0.902    -2.148021    1.904036
                     24  |    .371442   .5534742     0.67   0.508    -.7684593    1.511343
                     25  |   .1226874   .8955603     0.14   0.892    -1.721754    1.967128
                     26  |   .1609993    .679448     0.24   0.815     -1.23835    1.560349
                     27  |   .1893252    1.87055     0.10   0.920    -3.663144    4.041795
                     28  |   .2437452   .9029302     0.27   0.789    -1.615874    2.103365
                     29  |   .6184046   2.156551     0.29   0.777    -3.823095    5.059905
                     30  |   2.50e-15   1.246155     0.00   1.000    -2.566504    2.566504
            ------------------------------------------------------------------------------
            O.P. has already acknowledged that the code shown in his posts has been typed into the Forum editor. So it is probably not exactly what was really run. In order to get effective help, it is critical to post the actual code and actual outputs (including error messages). To avoid introducing changes that obscure the situation, this should always be done using the computer's copy/paste operations, not by retyping.

            If O.P. really did obtain that error message with the code shown, then something is wrong with his Stata installation. First step for dealing with that would be -update all, force-.

            Comment


            • #7
              My apologies to both of you, Clyde is correct, I have made a silly mistake and have not shown all relevant bits.
              This will not happen again, thanks for your help.
              Best,
              Nico

              Comment

              Working...
              X