I am trying to replicate manually the results of -margins- after estimating a model using -melogit-. Here are my data:
Let me tag the groups, then run the model (which converges in 5 iterations):
Then calculate the marginal probabilities of x, conditioned on the random effects:
So far so good. But then I wanted to demonstrate to someone how these were calculated, so I am trying to do it manually; first get the random effects for each group, then calculate the probabilities:
Why are these so different from the output of -margins-? Notably, if do the same thing using -margins x, predict(fixedonly)- and omit the random effects in my calculations, they agree.
thanks,
Jeph
Code:
. d y x group storage display value variable name type format label variable label -------------------------------------------------------------------------------------------- y double %12.0g x double %12.0g group str6 %6s
Code:
. egen tag=tag(group) . melogit y i.x || group: Fitting fixed-effects model: [...] Mixed-effects logistic regression Number of obs = 2,087 Group variable: group Number of groups = 328 Obs per group: min = 1 avg = 6.4 max = 54 Integration method: mvaghermite Integration pts. = 7 Wald chi2(1) = 4.50 Log likelihood = -335.56699 Prob > chi2 = 0.0340 ------------------------------------------------------------------------------ y | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- | 1.x | 1.154201 .5442717 2.12 0.034 .0874484 2.220954 _cons | -4.972222 .4338917 -11.46 0.000 -5.822634 -4.12181 -------------+---------------------------------------------------------------- group | var(_cons)| 5.033932 1.512757 2.793259 9.072011 ------------------------------------------------------------------------------ LR test vs. logistic model: chibar2(01) = 106.07 Prob >= chibar2 = 0.0000
Code:
. margins x, predict(mu) Adjusted predictions Number of obs = 2,087 Model VCE : OIM Expression : Marginal predicted mean, predict(mu) ------------------------------------------------------------------------------ | Delta-method | Margin Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- x | 0 | .0420558 .0080066 5.25 0.000 .0263632 .0577484 1 | .0902771 .0272681 3.31 0.001 .0368325 .1437217 ------------------------------------------------------------------------------
So far so good. But then I wanted to demonstrate to someone how these were calculated, so I am trying to do it manually; first get the random effects for each group, then calculate the probabilities:
Code:
. . predict re, reffects (calculating posterior means of random effects) (using 7 quadrature points) . gen prx0=invlogit(_b[_cons]+re) . gen prx1=invlogit(_b[_cons]+re+_b[1.x]) . sum prx* if tag Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- prx0 | 328 .0235069 .0703413 .0015256 .6677329 prx1 | 328 .0537861 .1228471 .0048224 .8643794 . sum prx* Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- prx0 | 2,087 .0274458 .0721269 .0015256 .6677329 prx1 | 2,087 .0630935 .1330303 .0048224 .8643794
thanks,
Jeph
Comment