Hello,
I am struggling with understanding what margins does after linear and logistic mixed regression (Stata 17).
The code below runs a linear mixed model using meglm. Using predict(mu), predict(mu fixedonly), predict(eta), and predict(xb) yield exactly the same result.I am surprised that predict(mu) and predict(mu fixedonly) give the same result, as I would have expected that predict(mu) also included both the fixed and random effects.
The code below runs a logistc mixed model using melogit, and here predict(mu) and predict(mu fixedonly) do not give the same result. It seems predict(mu) includes both a fixed and random part.
In addition, predict(eta), and predict(xb) seem to always give the same result (fixed part of the model), so I wondered if there is any difference ?
May main question is whether I can predict margins including both fixed and random effects for a linear mixed model, a for a logistic mixed model.
Thank you
I am struggling with understanding what margins does after linear and logistic mixed regression (Stata 17).
The code below runs a linear mixed model using meglm. Using predict(mu), predict(mu fixedonly), predict(eta), and predict(xb) yield exactly the same result.I am surprised that predict(mu) and predict(mu fixedonly) give the same result, as I would have expected that predict(mu) also included both the fixed and random effects.
Code:
. use https://www.stata-press.com/data/r17/pig, clear (Longitudinal analysis of pig weights) . . /* linear mixed model */ . meglm weight week || id:week Fitting fixed-effects model: ..... Iteration 8: log likelihood = -869.03825 Iteration 9: log likelihood = -869.03825 Mixed-effects GLM Number of obs = 432 Family: Gaussian Link: Identity Group variable: id Number of groups = 48 Obs per group: min = 9 avg = 9.0 max = 9 Integration method: mvaghermite Integration pts. = 7 Wald chi2(1) = 4689.52 Log likelihood = -869.03825 Prob > chi2 = 0.0000 ------------------------------------------------------------------------------- weight | Coefficient Std. err. z P>|z| [95% conf. interval] --------------+---------------------------------------------------------------- week | 6.209896 .0906818 68.48 0.000 6.032163 6.387629 _cons | 19.35561 .3979157 48.64 0.000 18.57571 20.13551 --------------+---------------------------------------------------------------- id | var(week)| .3680664 .080118 .2402386 .5639095 var(_cons)| 6.756356 1.543501 4.317717 10.57233 --------------+---------------------------------------------------------------- var(e.weight)| 1.598812 .1233989 1.374359 1.859921 ------------------------------------------------------------------------------- LR test vs. linear model: chi2(2) = 764.42 Prob > chi2 = 0.0000 Note: LR test is conservative and provided only for reference. . . margins , predict(mu) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Marginal predicted mean, predict(mu) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | 31.77541 .4244135 74.87 0.000 30.94357 32.60724 2 | 44.1952 .5173996 85.42 0.000 43.18111 45.20928 3 | 56.61499 .6488939 87.25 0.000 55.34318 57.8868 ------------------------------------------------------------------------------ . margins , predict(mu fixedonly) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Predicted mean, fixed portion only, predict(mu fixedonly) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | 31.77541 .4244135 74.87 0.000 30.94357 32.60724 2 | 44.1952 .5173996 85.42 0.000 43.18111 45.20928 3 | 56.61499 .6488939 87.25 0.000 55.34318 57.8868 ------------------------------------------------------------------------------ . margins , predict(eta) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Marginal linear prediction, predict(eta) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | 31.77541 .4244135 74.87 0.000 30.94357 32.60724 2 | 44.1952 .5173996 85.42 0.000 43.18111 45.20928 3 | 56.61499 .6488939 87.25 0.000 55.34318 57.8868 ------------------------------------------------------------------------------ . margins , predict(xb) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Linear prediction, fixed portion only, predict(xb) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | 31.77541 .4244135 74.87 0.000 30.94357 32.60724 2 | 44.1952 .5173996 85.42 0.000 43.18111 45.20928 3 | 56.61499 .6488939 87.25 0.000 55.34318 57.8868 ------------------------------------------------------------------------------
Code:
. generate weight_bin = 0 if weight < 55 (182 missing values generated) . replace weight_bin = 1 if weight >= 55 (182 real changes made) . . melogit weight_bin week || id:week , intmethod(ghermite) Fitting fixed-effects model: .... Iteration 7: log likelihood = -52.990773 Iteration 8: log likelihood = -52.990636 Iteration 9: log likelihood = -52.990635 Mixed-effects logistic regression Number of obs = 432 Group variable: id Number of groups = 48 Obs per group: min = 9 avg = 9.0 max = 9 Integration method: ghermite Integration pts. = 7 Wald chi2(1) = 0.38 Log likelihood = -52.990635 Prob > chi2 = 0.5361 ------------------------------------------------------------------------------ weight_bin | Coefficient Std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- week | 9.753026 15.76414 0.62 0.536 -21.14412 40.65017 _cons | -55.09313 88.20145 -0.62 0.532 -227.9648 117.7785 -------------+---------------------------------------------------------------- id | var(week)| .8057716 2.741197 .0010244 633.8107 var(_cons)| 13.05758 64.00653 .0008778 194241.1 ------------------------------------------------------------------------------ LR test vs. logistic model: chi2(2) = 11.71 Prob > chi2 = 0.0029 Note: LR test is conservative and provided only for reference. . . margins , predict(mu) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Marginal predicted mean, predict(mu) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | 1.05e-12 2.94e-11 0.04 0.972 -5.65e-11 5.86e-11 2 | .0014933 .0042185 0.35 0.723 -.0067748 .0097613 3 | .6913 .0670735 10.31 0.000 .5598382 .8227617 ------------------------------------------------------------------------------ . margins , predict(mu fixedonly) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Predicted mean, fixed portion only, predict(mu fixedonly) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | 3.51e-16 2.02e-09 0.00 1.000 -3.97e-09 3.97e-09 2 | 1.04e-07 2.61e-06 0.04 0.968 -5.01e-06 5.22e-06 3 | .9684777 .1979105 4.89 0.000 .5805802 1.356375 ------------------------------------------------------------------------------ . margins , predict(eta) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Marginal linear prediction, predict(eta) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | -35.58707 56.67693 -0.63 0.530 -146.6718 75.49767 2 | -16.08102 25.16184 -0.64 0.523 -65.39732 33.23528 3 | 3.425029 6.482773 0.53 0.597 -9.280973 16.13103 ------------------------------------------------------------------------------ . margins , predict(xb) at(week = (2(2)6)) Adjusted predictions Number of obs = 432 Model VCE: OIM Expression: Linear prediction, fixed portion only, predict(xb) 1._at: week = 2 2._at: week = 4 3._at: week = 6 ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- _at | 1 | -35.58707 56.67693 -0.63 0.530 -146.6718 75.49767 2 | -16.08102 25.16184 -0.64 0.523 -65.39732 33.23528 3 | 3.425029 6.482773 0.53 0.597 -9.280973 16.13103 ------------------------------------------------------------------------------ . . display invlogit(-35.58707) 3.505e-16 . display invlogit(-16.08102) 1.038e-07 . display invlogit(3.425029) .96847766 . display invlogit(22.93108) 1
May main question is whether I can predict margins including both fixed and random effects for a linear mixed model, a for a logistic mixed model.
Thank you