5  Marginal effects in models with fixed effects

Published

January 25, 2019

5.1 Marginal effects in a linear model

Stata’s margins command has been a powerful tool for many economists. It can calculate predicted means as well as predicted marginal effects. However, we do need to be careful when we use it when fixed effects are included. In a linear model, everything works out fine. However, in a non-linear model, you may not want to use margins, since it’s not calculating what you have in mind.

In a linear model with fixed effects, we can do it either by “demeaning” every variable, or include dummy variables. They return the same results. Fortunately, marginal effects can be calculated the same way in both models.

For example:

clear
sysuse auto
xtset rep78
xtreg price c.mpg##c.trunk, fe
margins , dydx(mpg)
reg price c.mpg##c.trunk i.rep78
margins , dydx(mpg)

. clear

. sysuse auto
(1978 automobile data)

. xtset rep78

Panel variable: rep78 (unbalanced)

. xtreg price c.mpg##c.trunk, fe

Fixed-effects (within) regression               Number of obs     =         69
Group variable: rep78                           Number of groups  =          5

R-squared:                                      Obs per group:
     Within  = 0.2570                                         min =          2
     Between = 0.0653                                         avg =       13.8
     Overall = 0.2237                                         max =         30

                                                F(3, 61)          =       7.03
corr(u_i, Xb) = -0.4133                         Prob > F          =     0.0004

------------------------------------------------------------------------------
       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |  -98.12003   226.8708    -0.43   0.667    -551.7763    355.5362
       trunk |   295.0544   343.3934     0.86   0.394    -391.6032     981.712
             |
       c.mpg#|
     c.trunk |  -12.23318   15.94713    -0.77   0.446    -44.12143    19.65506
             |
       _cons |    7574.85   5321.325     1.42   0.160    -3065.797     18215.5
-------------+----------------------------------------------------------------
     sigma_u |   992.2156
     sigma_e |  2631.2869
         rho |  .12449059   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(4, 61) = 0.86                       Prob > F = 0.4948

. margins , dydx(mpg)

Average marginal effects                                    Number of obs = 69
Model VCE: Conventional

Expression: Linear prediction, predict()
dy/dx wrt:  mpg

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |  -268.4981   74.12513    -3.62   0.000    -413.7807   -123.2156
------------------------------------------------------------------------------

. reg price c.mpg##c.trunk i.rep78

      Source |       SS           df       MS      Number of obs   =        69
-------------+----------------------------------   F(7, 61)        =      3.19
       Model |   154453046         7  22064720.8   Prob > F        =    0.0061
    Residual |   422343913        61  6923670.71   R-squared       =    0.2678
-------------+----------------------------------   Adj R-squared   =    0.1838
       Total |   576796959        68  8482308.22   Root MSE        =    2631.3

------------------------------------------------------------------------------
       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |  -98.12003   226.8708    -0.43   0.667    -551.7763    355.5362
       trunk |   295.0544   343.3934     0.86   0.394    -391.6032     981.712
             |
       c.mpg#|
     c.trunk |  -12.23318   15.94713    -0.77   0.446    -44.12143    19.65506
             |
       rep78 |
          2  |   438.0002   2161.922     0.20   0.840    -3885.031    4761.031
          3  |   987.1363   2022.606     0.49   0.627    -3057.315    5031.587
          4  |   1240.944   2046.417     0.61   0.547     -2851.12    5333.008
          5  |    2605.83   2161.837     1.21   0.233    -1717.031    6928.691
             |
       _cons |   6355.731   5209.899     1.22   0.227    -4062.105    16773.57
------------------------------------------------------------------------------

. margins , dydx(mpg)

Average marginal effects                                    Number of obs = 69
Model VCE: OLS

Expression: Linear prediction, predict()
dy/dx wrt:  mpg

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |  -268.4981   74.12513    -3.62   0.001    -416.7205   -120.2758
------------------------------------------------------------------------------

. 

All is fine.

5.2 Marginal effects in a non-linear model

In a nonlinear model, we need to be more careful:

clear
set seed 42
set obs 250
gen id = ceil(_n/5)
bys id: gen t = _n
gen mpg = rnormal(20,4)
gen trunk = rnormal(15,3)
gen alpha = rnormal(0,1)
bys id: replace alpha = alpha[1]
gen lambda = exp(0.5 + 0.05*mpg - 0.03*trunk + alpha)
gen accidents = rpoisson(lambda)
xtset id
xtpoisson accidents mpg trunk, fe
margins , dydx(mpg)
margins , dydx(mpg) predict(nu0)
poisson accidents mpg trunk i.id
margins , dydx(mpg)

. clear

. set seed 42

. set obs 250
Number of observations (_N) was 0, now 250.

. gen id = ceil(_n/5)

. bys id: gen t = _n

. gen mpg = rnormal(20,4)

. gen trunk = rnormal(15,3)

. gen alpha = rnormal(0,1)

. bys id: replace alpha = alpha[1]
(200 real changes made)

. gen lambda = exp(0.5 + 0.05*mpg - 0.03*trunk + alpha)

. gen accidents = rpoisson(lambda)

. xtset id

Panel variable: id (balanced)

. xtpoisson accidents mpg trunk, fe
note: 1 group (5 obs) dropped because of all zero outcomes

Iteration 0:  Log likelihood = -358.34297  
Iteration 1:  Log likelihood = -340.16596  
Iteration 2:  Log likelihood = -340.16413  
Iteration 3:  Log likelihood = -340.16413  

Conditional fixed-effects Poisson regression         Number of obs    =    245
Group variable: id                                   Number of groups =     49

                                                     Obs per group:
                                                                  min =      5
                                                                  avg =    5.0
                                                                  max =      5

                                                     Wald chi2(2)     =  35.90
Log likelihood = -340.16413                          Prob > chi2      = 0.0000

------------------------------------------------------------------------------
   accidents | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |   .0487946   .0090524     5.39   0.000     .0310521    .0665371
       trunk |  -.0316066   .0108931    -2.90   0.004    -.0529566   -.0102566
------------------------------------------------------------------------------

. margins , dydx(mpg)

Average marginal effects                                   Number of obs = 245
Model VCE: OIM

Expression: Linear prediction, predict()
dy/dx wrt:  mpg

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |   .0487946   .0090524     5.39   0.000     .0310521    .0665371
------------------------------------------------------------------------------

. margins , dydx(mpg) predict(nu0)

Average marginal effects                                   Number of obs = 245
Model VCE: OIM

Expression: Predicted number of events (assuming u_i=0), predict(nu0)
dy/dx wrt:  mpg

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |   .0850287   .0340547     2.50   0.013     .0182826    .1517747
------------------------------------------------------------------------------

. poisson accidents mpg trunk i.id

Iteration 0:  Log likelihood = -485.81671  
Iteration 1:  Log likelihood = -453.45562  
Iteration 2:  Log likelihood = -453.02481  
Iteration 3:  Log likelihood = -452.93985  
Iteration 4:  Log likelihood = -452.92127  
Iteration 5:  Log likelihood = -452.91701  
Iteration 6:  Log likelihood =   -452.916  
Iteration 7:  Log likelihood = -452.91579  
Iteration 8:  Log likelihood = -452.91575  

Poisson regression                                      Number of obs =    250
                                                        LR chi2(51)   = 934.93
                                                        Prob > chi2   = 0.0000
Log likelihood = -452.91575                             Pseudo R2     = 0.5079

------------------------------------------------------------------------------
   accidents | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |   .0487946   .0090525     5.39   0.000      .031052    .0665371
       trunk |  -.0316067   .0108931    -2.90   0.004    -.0529568   -.0102565
             |
          id |
          2  |   .3464221   .5592835     0.62   0.536    -.7497533    1.442598
          3  |   1.123341   .5131078     2.19   0.029     .1176679    2.129014
          4  |   .1098041   .6057603     0.18   0.856    -1.077464    1.297073
          5  |   .4069152   .5702496     0.71   0.475    -.7107536    1.524584
          6  |   1.791221   .4776303     3.75   0.000     .8550829    2.727359
          7  |  -14.00991   475.5637    -0.03   0.976    -946.0976    918.0778
          8  |   .9632705   .5216941     1.85   0.065    -.0592311    1.985772
          9  |   .1039008   .6061094     0.17   0.864    -1.084052    1.291853
         10  |   .7748186   .5273812     1.47   0.142    -.2588296    1.808467
         11  |   .7331684   .5395719     1.36   0.174    -.3243732     1.79071
         12  |   1.114943   .5007908     2.23   0.026      .133411    2.096475
         13  |  -.2054342   .6343396    -0.32   0.746    -1.448717    1.037848
         14  |   1.248116   .4945786     2.52   0.012     .2787602    2.217472
         15  |   1.395876   .4914484     2.84   0.005     .4326552    2.359098
         16  |   1.689054   .4845687     3.49   0.000     .7393167    2.638791
         17  |   .9941942   .5171224     1.92   0.055    -.0193471    2.007735
         18  |   1.742704   .4844911     3.60   0.000      .793119    2.692289
         19  |   .9912213   .5168563     1.92   0.055    -.0217985    2.004241
         20  |  -1.134862   .8375742    -1.35   0.175    -2.776477    .5067532
         21  |   1.484731   .4879013     3.04   0.002     .5284622       2.441
         22  |   2.442088   .4643803     5.26   0.000     1.531919    3.352256
         23  |   .9732572   .5265941     1.85   0.065    -.0588483    2.005363
         24  |    .280425   .5859507     0.48   0.632    -.8680172    1.428867
         25  |   2.472329   .4641555     5.33   0.000     1.562601    3.382057
         26  |   .5602592   .5485592     1.02   0.307    -.5148971    1.635416
         27  |   1.537242   .4861325     3.16   0.002     .5844401    2.490044
         28  |    1.34391   .5026286     2.67   0.008     .3587763    2.329044
         29  |   2.370956   .4660218     5.09   0.000      1.45757    3.284342
         30  |   2.453329   .4642702     5.28   0.000     1.543377    3.363282
         31  |   .9383342   .5171502     1.81   0.070    -.0752616     1.95193
         32  |   .1850002   .5864331     0.32   0.752    -.9643876    1.334388
         33  |  -.2686233   .6711829    -0.40   0.689    -1.584118    1.046871
         34  |   1.547864    .485423     3.19   0.001     .5964525    2.499276
         35  |   1.557431   .4878123     3.19   0.001     .6013366    2.513526
         36  |   1.295147   .4965837     2.61   0.009     .3218611    2.268433
         37  |   2.668556   .4616456     5.78   0.000     1.763748    3.573365
         38  |   .9287953   .5326269     1.74   0.081    -.1151343    1.972725
         39  |  -.9247308   .8366882    -1.11   0.269     -2.56461     .715148
         40  |   1.358213   .4979867     2.73   0.006     .3821769    2.334249
         41  |  -.0472585   .6070096    -0.08   0.938    -1.236975    1.142458
         42  |   1.394504   .4958685     2.81   0.005     .4226201    2.366389
         43  |  -.5414444   .7303533    -0.74   0.458    -1.972911    .8900219
         44  |   .9881123    .513744     1.92   0.054    -.0188073    1.995032
         45  |   1.171458   .5165503     2.27   0.023     .1590378    2.183878
         46  |    .817007   .5269245     1.55   0.121    -.2157461     1.84976
         47  |   2.800173   .4596719     6.09   0.000     1.899233    3.701113
         48  |    .365911   .5704157     0.64   0.521    -.7520831    1.483905
         49  |   2.445633   .4649624     5.26   0.000     1.534324    3.356943
         50  |   .7177863   .5279692     1.36   0.174    -.3170143    1.752587
             |
       _cons |  -.4227135   .5046249    -0.84   0.402     -1.41176     .566333
------------------------------------------------------------------------------

. margins , dydx(mpg)

Average marginal effects                                   Number of obs = 250
Model VCE: OIM

Expression: Predicted number of events, predict()
dy/dx wrt:  mpg

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         mpg |   .2238673   .0420551     5.32   0.000     .1414408    .3062937
------------------------------------------------------------------------------

. 

(We use a simulated count outcome accidents and a proper panel with 50 units of 5 periods each, rather than auto’s continuous price and its 5-category rep78 — a true panel needs many units, and a fixed-effect count model needs an actual count dependent variable.)

In this example, “xtpoisson, fe” and “poisson i.rep78” returns the same results. Fixed effect Poisson model (sometimes called conditional fixed effect Poisson) is the same models as a Poisson model with dummies, just like a linear model (OLS with dummies is the same as fixed effect OLS). Poisson model and OLS are unique in this sense that there is no “incidental parameter” problem.

We see in this example, margins commands do not return the same marginal effects, even though the models are the same. The reason behind this is that in a conditional fixed effect Poisson, the fixed effects are not estimated (they are not in the final likelihood function that gets estimated). Therefore, we’ll have to make a decision what values to use as the values of the fixed effects. “margins, predict(nu0)” simply set all fixed effects to zero. On the other hand, margins after Poisson model with dummies does not do that. The fixed effect in that case gets estimated. Therefore the marginal effects in that case make more sense.

So our advice for a conditional Poisson model is that we should not use margins to calculate marginal effects afterwards; instead, we should simply stick with the original coefficient estimates.

The same logic applies to the conditional logit model. Fixed effects are not estimated in that model; simply setting them to zero does not make too much sense. In addition, conditional logit model is not the same model as a logit model with dummies, since there is the “incidental parameter” problem. Again, we should just focus on the coefficient estimates as the effect on the logged odds.

In other words, for fixed effect (conditional) logit model, the situation is worse: you cannot do logit with dummies, unless you have a deep panel. That is, when you have, say, more than 20 observations per group, the “incidental parameter” bias becomes negligible. If you stay with conditional logit model, the fixed effects are not estimated. Unfortunately the predicted probability depends on the fixed effects. Stata’s margins command after clogit (or xtlogit, fe) comes with a few options, but none is reasonable for the fixed effects. For example, the pu0 option is to assume all fixed effects being 0.

In a fixed effect logit model,

\[ log(P(y=1)/(1-P(y=1))) = \alpha_i + \beta_1 x_1 + \beta_2 x_2 + \beta_{12} x_1*x_2 \]

Here \(\alpha_i\) is fixed effect for each firm. Therefore,

\[ P(y=1) = F(\alpha_i + \beta_1 x_1 + \beta_2 x_2 + \beta_{12} x_1*x_2) \]

\(F\) is the logistic CDF (logit); for probit it would be the normal CDF. Therefore, without estimating \(\alpha_i\), there is no way to predict \(P\) in a reasonable way (assuming \(\alpha=0\) is not reasonable to me).

However, if we stick with logged odds (\(LO=log(P(y=1)/(1-P(y=1)))\)), then \(LO\) is a linear function of \(\alpha_i\) and other covariates. In that case, the marginal effects of \(x_1\) or \(x_2\) on \(Y\) has nothing to do with \(\alpha_i\).

Therefore, we can use margins command to calculate effects on the logged odds, which will be “predict(xb)” option. Note that xb here is \(X_{it}\beta\) only — the individual fixed effect \(\alpha_i\) is not estimated in conditional logit and so is not part of the prediction. The logged-odds effect of a covariate (\(\partial(X\beta)/\partial x\)) therefore matches the original coefficient exactly and does not depend on \(\alpha_i\), but xb itself is a log-odds relative to each individual’s (unidentified) baseline, not the individual’s absolute log-odds. This lets you make linear extrapolations of relative log-odds across values of the covariates, but not of absolute predicted probabilities, since those would require \(\alpha_i\).

clear
webuse union
clogit union c.age##i.south not_smsa grade, group(idcode)
margins, at( age=(15 20 25 30 35 40) south=(0 1)) predict(xb)
marginsplot

. clear

. webuse union
(NLS Women 14-24 in 1968)

. clogit union c.age##i.south not_smsa grade, group(idcode)
note: multiple positive outcomes within groups encountered.
note: 2,744 groups (14,165 obs) omitted because of all positive or
      all negative outcomes.

Iteration 0:  Log likelihood = -4518.8815  
Iteration 1:  Log likelihood = -4512.8224  
Iteration 2:  Log likelihood = -4512.8192  
Iteration 3:  Log likelihood = -4512.8192  

Conditional (fixed-effects) logistic regression         Number of obs = 12,035
                                                        LR chi2(5)    =  74.73
                                                        Prob > chi2   = 0.0000
Log likelihood = -4512.8192                             Pseudo R2     = 0.0082

------------------------------------------------------------------------------
       union | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         age |   .0096842   .0050265     1.93   0.054    -.0001676     .019536
     1.south |  -1.382178    .276966    -4.99   0.000    -1.925022   -.8393346
             |
 south#c.age |
          1  |   .0208997   .0081247     2.57   0.010     .0049756    .0368238
             |
    not_smsa |   .0195233   .1131292     0.17   0.863    -.2022058    .2412523
       grade |   .0822276   .0419062     1.96   0.050      .000093    .1643622
------------------------------------------------------------------------------

. margins, at( age=(15 20 25 30 35 40) south=(0 1)) predict(xb)

Predictive margins                                      Number of obs = 12,035
Model VCE: OIM

Expression: Linear prediction, predict(xb)
1._at:  age   = 15
        south =  0
2._at:  age   = 15
        south =  1
3._at:  age   = 20
        south =  0
4._at:  age   = 20
        south =  1
5._at:  age   = 25
        south =  0
6._at:  age   = 25
        south =  1
7._at:  age   = 30
        south =  0
8._at:  age   = 30
        south =  1
9._at:  age   = 35
        south =  0
10._at: age   = 35
        south =  1
11._at: age   = 40
        south =  0
12._at: age   = 40
        south =  1

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         _at |
          1  |   1.202147   .5190753     2.32   0.021      .184778    2.219516
          2  |    .133464   .5599015     0.24   0.812    -.9639228    1.230851
          3  |   1.250568   .5153257     2.43   0.015      .240548    2.260588
          4  |   .2863834   .5465398     0.52   0.600    -.7848148    1.357582
          5  |   1.298989   .5127819     2.53   0.011     .2939548    2.304023
          6  |   .4393029   .5349589     0.82   0.412    -.6091973    1.487803
          7  |    1.34741   .5114619     2.63   0.008     .3449629    2.349857
          8  |   .5922223   .5252767     1.13   0.260    -.4373011    1.621746
          9  |   1.395831   .5113752     2.73   0.006     .3935538    2.398108
         10  |   .7451418   .5175997     1.44   0.150    -.2693351    1.759619
         11  |   1.444252   .5125224     2.82   0.005     .4397264    2.448777
         12  |   .8980612   .5120182     1.75   0.079    -.1054761    1.901598
------------------------------------------------------------------------------

. marginsplot

Variables that uniquely identify margins: age south

In this example, we have a fixed effect logit on union status, with age and south interaction, age as continuous variable. Suppose we’d like to see the predicted logged odds of union status for different age and south/north, then we can still use margins to predict logged odds. But we cannot use margins to predict probability, since the fixed effects are not estimated.