------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\7.6.smcl log type: smcl opened on: 27 Mar 2020, 18:22:17 . **************************************************************************** . * Module 7: Multilevel Models for Binary Responses Stata Practicals . * . * P7.6: Adding Level 2 Explanatory Variables: Contextual Effects . * . * George Leckie . * Centre for Multilevel Modelling, 2010 . **************************************************************************** . * Stata do-file to replicate all analyses using runmlwin . * . * George Leckie . * Centre for Multilevel Modelling, 2013 . * http://www.bristol.ac.uk/cmm/software/runmlwin/ . **************************************************************************** . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/7.6.dta", clear . . . . * P7.6.1 Contextual effects . . runmlwin antemed cons magec magecsq meduc2 meduc3 /// > wealth2 wealth3 wealth4 wealth5 /// > urban, /// > level2(comm: cons wealth4 wealth5) /// > level1(womid:) /// > discrete(distribution(binomial) link(logit) denominator(cons) pql2) / > // > nopause MLwiN 3.05 multilevel model Number of obs = 5366 Binomial logit response model (hierarchical) Estimation algorithm: IGLS, PQL2 ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ comm | 361 3 14.9 25 ----------------------------------------------------------- Run time (seconds) = 1.77 Number of iterations = 10 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.51699 .110013 -13.79 0.000 -1.732612 -1.301369 magec | .0004328 .0065748 0.07 0.948 -.0124536 .0133192 magecsq | -.0010864 .000687 -1.58 0.114 -.0024329 .0002601 meduc2 | .5733115 .0854191 6.71 0.000 .4058931 .7407299 meduc3 | 1.364122 .0976801 13.97 0.000 1.172673 1.555572 wealth2 | .4569967 .1083704 4.22 0.000 .2445946 .6693989 wealth3 | .651333 .1106626 5.89 0.000 .4344382 .8682278 wealth4 | .9564006 .1150286 8.31 0.000 .7309486 1.181853 wealth5 | 1.511068 .1338976 11.29 0.000 1.248634 1.773503 urban | .970279 .1144983 8.47 0.000 .7458666 1.194692 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .9140853 .1196275 .6796197 1.148551 cov(cons,wealth4) | -.2800813 .119444 -.5141873 -.0459752 var(wealth4) | .0707389 .1750615 -.2723753 .413853 cov(cons,wealth5) | -.4712259 .1546921 -.7744169 -.1680349 cov(wealth4,wealth5) | -.0780272 .1722123 -.4155571 .2595026 var(wealth5) | .2157971 .2553595 -.2846982 .7162924 ------------------------------------------------------------------------------ . . tabulate wealth urban, row +----------------+ | Key | |----------------| | frequency | | row percentage | +----------------+ Wealth | Type of region of index (1 = | residence poorest) | 0 1 | Total -----------+----------------------+---------- 1 | 997 170 | 1,167 | 85.43 14.57 | 100.00 -----------+----------------------+---------- 2 | 833 184 | 1,017 | 81.91 18.09 | 100.00 -----------+----------------------+---------- 3 | 771 219 | 990 | 77.88 22.12 | 100.00 -----------+----------------------+---------- 4 | 722 267 | 989 | 73.00 27.00 | 100.00 -----------+----------------------+---------- 5 | 359 844 | 1,203 | 29.84 70.16 | 100.00 -----------+----------------------+---------- Total | 3,682 1,684 | 5,366 | 68.62 31.38 | 100.00 . . bysort comm: egen wealth5mean = mean(wealth5) . . runmlwin antemed cons magec magecsq meduc2 meduc3 /// > wealth2 wealth3 wealth4 wealth5 /// > urban wealth5mean, /// > level2(comm: cons wealth4 wealth5) /// > level1(womid:) /// > discrete(distribution(binomial) link(logit) denominator(cons) pql2) / > // > initsprevious nopause Model fitted using initial values specified as parameter estimates from previou > s model MLwiN 3.05 multilevel model Number of obs = 5366 Binomial logit response model (hierarchical) Estimation algorithm: IGLS, PQL2 ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ comm | 361 3 14.9 25 ----------------------------------------------------------- Run time (seconds) = 1.75 Number of iterations = 12 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.619681 .1101847 -14.70 0.000 -1.83564 -1.403723 magec | .0004012 .0065665 0.06 0.951 -.0124688 .0132713 magecsq | -.0011211 .0006876 -1.63 0.103 -.0024687 .0002264 meduc2 | .5814052 .0853972 6.81 0.000 .4140298 .7487806 meduc3 | 1.381887 .0977313 14.14 0.000 1.190337 1.573437 wealth2 | .4431603 .1080723 4.10 0.000 .2313425 .6549781 wealth3 | .6226894 .1104169 5.64 0.000 .4062763 .8391025 wealth4 | .9085185 .1150865 7.89 0.000 .682953 1.134084 wealth5 | 1.175604 .1417575 8.29 0.000 .8977645 1.453444 urban | .4896622 .1379808 3.55 0.000 .2192249 .7600996 wealth5mean | 1.456262 .2587892 5.63 0.000 .9490445 1.96348 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .8343974 .1129031 .6131113 1.055684 cov(cons,wealth4) | -.2718003 .116183 -.4995149 -.0440857 var(wealth4) | .0871118 .1749044 -.2556945 .4299182 cov(cons,wealth5) | -.4055714 .1421897 -.684258 -.1268847 cov(wealth4,wealth5) | -.09623 .1629641 -.4156338 .2231738 var(wealth5) | .1044863 .2310225 -.3483095 .5572822 ------------------------------------------------------------------------------ . . . . * P7.6.2 Cross-level interactions . . generate wealth2Xwealth5mean = wealth2*wealth5mean . . generate wealth3Xwealth5mean = wealth3*wealth5mean . . generate wealth4Xwealth5mean = wealth4*wealth5mean . . generate wealth5Xwealth5mean = wealth5*wealth5mean . . runmlwin antemed cons magec magecsq meduc2 meduc3 /// > wealth2 wealth3 wealth4 wealth5 /// > urban wealth5mean /// > wealth2Xwealth5mean wealth3Xwealth5mean /// > wealth4Xwealth5mean wealth5Xwealth5mean, /// > level2(comm: cons wealth4 wealth5) /// > level1(womid:) /// > discrete(distribution(binomial) link(logit) denominator(cons) pql2) / > // > initsprevious nopause Model fitted using initial values specified as parameter estimates from previou > s model MLwiN 3.05 multilevel model Number of obs = 5366 Binomial logit response model (hierarchical) Estimation algorithm: IGLS, PQL2 ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ comm | 361 3 14.9 25 ----------------------------------------------------------- Run time (seconds) = 1.72 Number of iterations = 10 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.758765 .1254934 -14.01 0.000 -2.004727 -1.512802 magec | -.0001371 .0065756 -0.02 0.983 -.0130251 .0127509 magecsq | -.0010854 .0006875 -1.58 0.114 -.0024328 .000262 meduc2 | .5798882 .0855504 6.78 0.000 .4122125 .7475639 meduc3 | 1.374489 .0977328 14.06 0.000 1.182936 1.566042 wealth2 | .5472127 .1313866 4.16 0.000 .2896997 .8047256 wealth3 | .6637484 .137328 4.83 0.000 .3945905 .9329062 wealth4 | 1.066193 .1452841 7.34 0.000 .7814418 1.350945 wealth5 | 1.489396 .1998384 7.45 0.000 1.097719 1.881072 urban | .4712439 .1368141 3.44 0.001 .2030932 .7393946 wealth5mean | 2.902358 .6725616 4.32 0.000 1.584162 4.220555 wealth2Xwe~n | -1.184766 .7670469 -1.54 0.122 -2.68815 .3186183 wealth3Xwe~n | -.6480948 .7552896 -0.86 0.391 -2.128435 .8322456 wealth4Xwe~n | -1.511143 .7142591 -2.12 0.034 -2.911065 -.1112208 wealth5Xwe~n | -1.734488 .7114722 -2.44 0.015 -3.128948 -.3400286 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .8341403 .1133717 .6119359 1.056345 cov(cons,wealth4) | -.2826286 .1167366 -.5114282 -.053829 var(wealth4) | .0937365 .1752357 -.2497191 .4371922 cov(cons,wealth5) | -.437436 .1440232 -.7197163 -.1551557 cov(wealth4,wealth5) | -.0618531 .1642137 -.383706 .2599997 var(wealth5) | .1394945 .2342798 -.3196855 .5986745 ------------------------------------------------------------------------------ . . replace magec = 0 (5,366 real changes made) . . replace magecsq = 0 (5,366 real changes made) . . summarize meduc2 Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- meduc2 | 5,366 .3073053 .46142 0 1 . . replace meduc2 = 0.307 variable meduc2 was byte now float (5,366 real changes made) . . summarize meduc3 Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- meduc3 | 5,366 .3449497 .4753962 0 1 . . replace meduc3 = 0.345 variable meduc3 was byte now float (5,366 real changes made) . . summarize urban Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- urban | 5,366 .3138278 .4640906 0 1 . . replace urban = 0.314 variable urban was byte now float (5,366 real changes made) . . recode wealth5mean (0/0.1 = 0) (0.1/0.3 = 0.2) (0.3/1.0 = 0.4) (wealth5mean: 3418 changes made) . . recode wealth2Xwealth5mean wealth3Xwealth5mean /// > wealth4Xwealth5mean wealth5Xwealth5mean /// > (0/0.1 = 0) (0.1/0.3 = 0.2) (0.3/1.0 = 0.4) (wealth2Xwealth5mean: 493 changes made) (wealth3Xwealth5mean: 591 changes made) (wealth4Xwealth5mean: 686 changes made) (wealth5Xwealth5mean: 1182 changes made) . . predict predxb . . generate predprob = invlogit(predxb) . . graph bar (mean) predprob, over(wealth5mean) over(wealth) . . graph bar (mean) predprob, over(wealth5mean) over(wealth) asyvars /// > ytitle(Predicted probability) /// > b1title(Household wealth index (quntiles)) /// > legend(rows(1) /// > subtitle(Community proportion in the top wealth quintile)) . end of do-file