------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\7.5.smcl log type: smcl opened on: 27 Mar 2020, 18:22:08 . ***************************************************************************** > *** . * Module 7: Multilevel Models for Binary Responses Stata Practicals . * . * P7.5: Two-Level Random Slope Model . * . * 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.5.dta", clear . . . . * P7.5.1 Allowing the effect of wealth to vary across communities . . runmlwin antemed cons magec magecsq meduc2 meduc3 wealthc, /// > level2(comm: cons wealthc, residuals(u)) /// > 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.12 Number of iterations = 8 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.4530626 .0826529 -5.48 0.000 -.6150592 -.2910659 magec | -.0002175 .0065621 -0.03 0.974 -.0130789 .0126439 magecsq | -.0010518 .000685 -1.54 0.125 -.0023943 .0002907 meduc2 | .5454563 .0851055 6.41 0.000 .3786525 .71226 meduc3 | 1.305341 .097297 13.42 0.000 1.114642 1.49604 wealthc | .4040866 .0303629 13.31 0.000 .3445764 .4635967 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .8342092 .0942575 .6494678 1.018951 cov(cons,wealthc) | -.1079019 .0300187 -.1667375 -.0490663 var(wealthc) | .015467 .020289 -.0242988 .0552328 ------------------------------------------------------------------------------ . . . . * P7.5.2 Interpretation of a random slope model . . runmlwin, correlations 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.12 Number of iterations = 8 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.4530626 .0826529 -5.48 0.000 -.6150592 -.2910659 magec | -.0002175 .0065621 -0.03 0.974 -.0130789 .0126439 magecsq | -.0010518 .000685 -1.54 0.125 -.0023943 .0002907 meduc2 | .5454563 .0851055 6.41 0.000 .3786525 .71226 meduc3 | 1.305341 .097297 13.42 0.000 1.114642 1.49604 wealthc | .4040866 .0303629 13.31 0.000 .3445764 .4635967 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .8342092 .0942575 .6494678 1.018951 corr(cons,wealthc) | -.9499232 .5853116 -2.097113 .1972664 var(wealthc) | .015467 .020289 -.0242988 .0552328 ------------------------------------------------------------------------------ . . egen pickone = tag(comm) . . scatter u1 u0 if pickone==1, yline(0) xline(0) . . preserve . . replace magec = 0 (5,366 real changes made) . . replace magecsq = 0 (5,366 real changes made) . . replace meduc2 = 0 (1,649 real changes made) . . replace meduc3 = 0 (1,851 real changes made) . . predict predxb . . generate predprob = invlogit(predxb) . . generate predlogit = logit(predprob) . . replace predlogit = predlogit + u0 + u1*wealthc (5,366 real changes made) . . drop pickone . . egen pickone = tag(comm wealthc) . . line predlogit wealth if pickone==1, connect(ascending) . . generate multiplewealth = pickone . . bysort comm (wealth): replace multiplewealth = 0 if wealth[_N]==wealt > h[1] (16 real changes made) . . line predlogit wealth if multiplewealth==1, connect(ascending) . . restore . . twoway function 0.865 + -0.232*x + 0.025*x^2, range(1 5) . . . . * P7.5.3 Fitting random coefficients to categorical wealth . . runmlwin antemed cons magec magecsq meduc2 meduc3 /// > wealth2 wealth3 wealth4 wealth5, /// > level2(comm: cons) /// > 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) = 0.87 Number of iterations = 5 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.246443 .1019835 -12.22 0.000 -1.446327 -1.046559 magec | -.0013438 .0065576 -0.20 0.838 -.0141965 .0115089 magecsq | -.0009947 .0006825 -1.46 0.145 -.0023325 .000343 meduc2 | .5506561 .0846341 6.51 0.000 .3847763 .7165359 meduc3 | 1.297188 .0977686 13.27 0.000 1.105565 1.488811 wealth2 | .4662711 .106225 4.39 0.000 .258074 .6744682 wealth3 | .6831606 .1083912 6.30 0.000 .4707178 .8956035 wealth4 | 1.053151 .1144715 9.20 0.000 .8287907 1.277511 wealth5 | 1.765905 .1336265 13.22 0.000 1.504002 2.027808 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .8161432 .0909669 .6378514 .9944349 ------------------------------------------------------------------------------ . . estimates store model1 . . preserve . . 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) . . predict predxb . . generate predprob = invlogit(predxb) . . graph bar (mean) predprob, over(wealth) . . restore . . . runmlwin antemed cons magec magecsq meduc2 meduc3 /// > wealth2 wealth3 wealth4 wealth5, /// > level2(comm: cons 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.59 Number of iterations = 9 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.257304 .1043335 -12.05 0.000 -1.461794 -1.052814 magec | -.0008802 .0065867 -0.13 0.894 -.0137899 .0120295 magecsq | -.0010227 .0006857 -1.49 0.136 -.0023666 .0003212 meduc2 | .5501968 .0851959 6.46 0.000 .3832159 .7171777 meduc3 | 1.306005 .0980422 13.32 0.000 1.113846 1.498164 wealth2 | .4659508 .107177 4.35 0.000 .2558878 .6760138 wealth3 | .6812646 .1093809 6.23 0.000 .4668819 .8956472 wealth4 | 1.05555 .115844 9.11 0.000 .8285002 1.2826 wealth5 | 1.841238 .1346624 13.67 0.000 1.577304 2.105171 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | .9172326 .1065707 .7083579 1.126107 cov(cons,wealth5) | -.4171388 .144411 -.7001791 -.1340985 var(wealth5) | .3993384 .2527427 -.0960282 .894705 ------------------------------------------------------------------------------ . . estimates store model2 . . test ([RP2]var(wealth5) = 0) ([RP2]cov(cons\wealth5) = 0) ( 1) [RP2]var(wealth5) = 0 ( 2) [RP2]cov(cons\wealth5) = 0 chi2( 2) = 9.01 Prob > chi2 = 0.0110 . . runmlwin antemed cons magec magecsq meduc2 meduc3 /// > wealth2 wealth3 wealth4 wealth5, /// > 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.68 Number of iterations = 10 ------------------------------------------------------------------------------ antemed | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.252853 .1073933 -11.67 0.000 -1.46334 -1.042366 magec | -.0005063 .0065815 -0.08 0.939 -.0134058 .0123933 magecsq | -.0010569 .0006866 -1.54 0.124 -.0024026 .0002887 meduc2 | .554214 .0853377 6.49 0.000 .3869551 .7214728 meduc3 | 1.307445 .0975796 13.40 0.000 1.116193 1.498698 wealth2 | .4750537 .1084755 4.38 0.000 .2624457 .6876617 wealth3 | .6883574 .1108383 6.21 0.000 .4711184 .9055964 wealth4 | 1.002694 .115254 8.70 0.000 .7768007 1.228588 wealth5 | 1.829137 .1348131 13.57 0.000 1.564908 2.093366 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: comm | var(cons) | 1.063131 .1315968 .8052059 1.321056 cov(cons,wealth4) | -.309197 .1251315 -.5544503 -.0639437 var(wealth4) | .0726574 .1759846 -.2722661 .4175809 cov(cons,wealth5) | -.4462959 .1699103 -.7793139 -.1132778 cov(wealth4,wealth5) | -.0439122 .1857181 -.4079131 .3200886 var(wealth5) | .289267 .283376 -.2661398 .8446738 ------------------------------------------------------------------------------ . . estimates store model3 . . test ([RP2]var(wealth4) = 0) ([RP2]cov(cons\wealth4) = 0) ([RP2]cov(wealth4\w > ealth5) = 0) ( 1) [RP2]var(wealth4) = 0 ( 2) [RP2]cov(cons\wealth4) = 0 ( 3) [RP2]cov(wealth4\wealth5) = 0 chi2( 3) = 11.23 Prob > chi2 = 0.0106 . end of do-file