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.
(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.
\(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
(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.
---title: "Marginal effects in models with fixed effects"date: "2019-01-25"---## Marginal effects in a linear modelStata'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:```{r}#| label: stata-chunk#| engine: 'stata'#| engine.path: '/usr/local/bin/stata'#| cache: trueclearsysuse autoxtset rep78xtreg price c.mpg##c.trunk, femargins , dydx(mpg)reg price c.mpg##c.trunk i.rep78margins , dydx(mpg)```All is fine.## Marginal effects in a non-linear modelIn a nonlinear model, we need to be more careful:```{r}#| label: stata-chunk2#| engine: 'stata'#| engine.path: '/usr/local/bin/stata'#| cache: trueclearset seed 42set obs 250gen id =ceil(_n/5)bys id: gen t = _ngen 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 idxtpoisson accidents mpg trunk, femargins , dydx(mpg)margins , dydx(mpg) predict(nu0)poisson accidents mpg trunk i.idmargins , dydx(mpg)```(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 thesame results. Fixed effect Poisson model (sometimes calledconditional fixed effect Poisson) is the same models as a Poissonmodel with dummies, just like a linear model (OLS with dummies is thesame as fixed effect OLS). Poisson model and OLS are unique in thissense that there is no "incidental parameter" problem.We see in this example, margins commands do not return the samemarginal effects, even though the models are the same. The reasonbehind this is that in a conditional fixed effect Poisson, the fixedeffects are not estimated (they are not in the final likelihoodfunction that gets estimated). Therefore, we'll have to make adecision what values to use as the values of the fixed effects."margins, predict(nu0)" simply set all fixed effects to zero. On theother hand, margins after Poisson model with dummies does not do that.The fixed effect in that case gets estimated. Therefore the marginaleffects in that case make more sense.So our advice for a conditional Poisson model is that we should notuse margins to calculate marginal effects afterwards; instead, weshould simply stick with the original coefficient estimates.The same logic applies to the conditional logit model. Fixed effectsare not estimated in that model; simply setting them to zero does notmake too much sense. In addition, conditional logit model is not thesame model as a logit model with dummies, since there is the"incidental parameter" problem. Again, we should just focus on thecoefficient estimates as the effect on the logged odds.In other words, for fixed effect (conditional) logit model, thesituation is worse: you cannot do logit with dummies, unless you havea deep panel. That is, when you have, say, more than 20 observationsper group, the "incidental parameter" bias becomes negligible. If youstay with conditional logit model, the fixed effects are notestimated. Unfortunately the predicted probability depends on thefixed effects. Stata's margins command after clogit (or xtlogit, fe)comes with a few options, but none is reasonable for the fixedeffects. For example, the pu0 option is to assume all fixed effectsbeing 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. Inthat case, the marginal effects of $x_1$ or $x_2$ on $Y$ has nothingto do with $\alpha_i$.Therefore, we can use margins command to calculate effects on thelogged odds, which will be "predict(xb)" option. Note that `xb` hereis $X_{it}\beta$ only — the individual fixed effect $\alpha_i$ is notestimated 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 dependon $\alpha_i$, but `xb` itself is a log-odds relative to eachindividual's (unidentified) baseline, not the individual's absolutelog-odds. This lets you make linear extrapolations of relativelog-odds across values of the covariates, but not of absolutepredicted probabilities, since those would require $\alpha_i$.```{r}#| label: stata-chunk3#| engine: 'stata'#| engine.path: '/usr/local/bin/stata'#| cache: trueclearwebuse unionclogit union c.age##i.south not_smsa grade, group(idcode)margins, at( age=(152025303540) south=(01)) predict(xb)marginsplot```In this example, we have a fixed effect logit on union status, withage and south interaction, age as continuous variable. Suppose we'dlike to see the predicted logged odds of union status for differentage and south/north, then we can still use margins to predict loggedodds. But we cannot use margins to predict probability, since thefixed effects are not estimated.