3

I am interested in reproducing average marginal effects from a random effects logit model (run in Stata using xtlogit). I understand how to reproduce the average marginal effects from a logit model using the Delta method. For instance, in the code below, I successfully reproduce the average marginal effect for age reported in margins.

*** Stata code
* download data
webuse union, clear

* calculate delta and copy variable of interest - age
sum age
gen xdelta = r(sd)/1000
clonevar age_ = age

* run logit model and calculate average marginal effect using margins
logit union age_
margins,dydx(age_)

* calculate average marginal effect by hand - mean of xme equals result from margins above
predict p1
replace age_ = age_+xdelta
predict p2
gen xme = (p2 - p1) / xdelta
sum xme

* calculate average marginal effect at fixed value of age using margins
margins,at(age=(16))

* calculate average marginal effect by hand - mean of p3 equals result from margins above
replace age_ = 16
predict p3
sum p3

Where I'm struggling is reproducing the average marginal effects for a xtlogit model.

*** Stata code
* download data and designate panel variables
webuse union, clear
xtset idcode year

* run xtlogit model
xtlogit union age

* calculate average marginal effects - can't figure out how to reproduce these estimates :(
margins, dydx(*)
margins, at(age=(16))

Any help for figuring out how to reproduce the xtlogit marginal effects is greatly appreciated. Thank you!

----- edited to make clearer that I'm interested in reproducing estimates reported by margins

Eunice
  • 139
  • 4
  • When you ask about how to calculate "by hand" it's not clear. There isn't much done "by hand" in your successful example. If you're interested in how margins works after `xtlogit, re` [Cameron & Trivedi 2010](https://www.stata.com/bookstore/microeconometrics-stata/) page 630 has good starting point and recommends an estimation approach. – Arthur Morris Nov 03 '20 at 04:41
  • Thanks a lot for your comment. I guess "by hand" was not the correct phrase - I meant that I would like to reproduce or replicate the marginal effects reported by ```margins```. I have modified my question. Also, I will check out the reference you passed along! – Eunice Nov 03 '20 at 05:21

1 Answers1

3

There are a couple of ways to do this, but essentially the problem boils down to the fact

$$\Pr(y_{it}=1 \vert x_{it})=\int\Lambda(u_i + x_{it}'\beta)\cdot \varphi(0,\sigma_u^2) du_i$$

where $\varphi()$ is the normal density. In your code, you are effectively setting the random effect $u_i$ to zero (which is what predict(pu0) does). This sets the RE to its average, which may not be what you had it mind. Of course, $u_i$ is not observed or even estimated by xtlogit, re, so if you want to replicate what predict(pr) does, you need to integrate the random effect out to get the unconditional probability using the estimated variance.

One way to do this in Stata is to use the user-written integrate command to do one dimensional numerical integration like this:

webuse union, clear
xtset idcode year
xtlogit union age, nolog
margins, at(age=(16)) predict(pr)
margins, dydx(*) at(age=16) predict(pr)
capture ssc install integrate
/* phat at age 16 */
integrate, f(invlogit(x - 3.0079682 + .01929225*16)*normalden(x,0,2.477537654945496)) l(-10) u(10) vectorise quadpts(1000)
/* ME at age 16 */
integrate, f(invlogit(x - 3.0079682 + .01929225*16)*(1-invlogit(x - 3.0079682 + .01929225*16))*(.01929225)*normalden(x,0,2.477537654945496)) l(-10) u(10) vectorise quadpts(1000)

You can probably get even better precision if you use the actual coefficients (like _b[_cons] and e(sigma_u)) instead of pasting the values in.

There might be a more efficient way to do this using Mata, Python, or even with a simulation approach, but I will leave that up to you to work out. You can also get posterior modal estimates of the REs using xtmelogit.

Dimitriy V. Masterov
  • 8,406
  • 2
  • 21
  • 43