------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\2_Introduct > ion_to_Multilevel_Modelling.smcl log type: smcl opened on: 27 Mar 2020, 17:41:27 . **************************************************************************** . * MLwiN User Manual . * . * 2 Introduction to Multilevel Modelling 9 . * . * Rasbash, J., Steele, F., Browne, W. J. and Goldstein, H. (2012). . * A User’s Guide to MLwiN, v2.26. Centre for Multilevel Modelling, . * University of Bristol. . **************************************************************************** . * Stata do-file to replicate all analyses using runmlwin . * . * George Leckie and Chris Charlton, . * Centre for Multilevel Modelling, 2012 . * http://www.bristol.ac.uk/cmm/software/runmlwin/ . **************************************************************************** . . * 2.1 The tutorial data set . . . . . . . . . . . . . . . . . . . . . . . .9 . . * 2.2 Opening the worksheet and looking at the data . . . . . . . . . . . 10 . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear . . describe Contains data from http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta obs: 4,059 vars: 10 21 Oct 2011 12:19 ------------------------------------------------------------------------------- storage display value variable name type format label variable label ------------------------------------------------------------------------------- school byte %9.0g School ID student int %9.0g Student ID normexam float %9.0g Age 16 exam score (normalised) cons byte %9.0g Constant standlrt float %9.0g Age 11 exam score (standardised) girl byte %9.0g Girl schgend byte %9.0g schgend School gender avslrt float %9.0g School average LRT score schav byte %9.0g schav School average LRT score (3 categories) vrband byte %9.0g vrband Age 11 verbal reasoning level ------------------------------------------------------------------------------- Sorted by: . . list school student normexam cons standlrt girl schgend in 1/10 +-------------------------------------------------------------------+ | school student normexam cons standlrt girl schgend | |-------------------------------------------------------------------| 1. | 1 1 .2613245 1 .6190593 1 mixedsch | 2. | 1 2 .1340668 1 .205802 1 mixedsch | 3. | 1 3 -1.723882 1 -1.364576 0 mixedsch | 4. | 1 4 .967586 1 .205802 1 mixedsch | 5. | 1 5 .5443409 1 .3711049 1 mixedsch | |-------------------------------------------------------------------| 6. | 1 6 1.734899 1 2.189437 0 mixedsch | 7. | 1 7 1.039608 1 -1.116621 0 mixedsch | 8. | 1 8 -.1290847 1 -1.03397 0 mixedsch | 9. | 1 9 -.9393777 1 -.5380612 1 mixedsch | 10. | 1 10 -1.219486 1 -1.447227 0 mixedsch | +-------------------------------------------------------------------+ . . . . * 2.3 Comparing two groups . . . . . . . . . . . . . . . . . . . . . . . .13 . . tabstat normexam, statistics(count mean sd) by(girl) columns(statistics) Summary for variables: normexam by categories of: girl (Girl) girl | N mean sd ---------+------------------------------ 0 | 1623 -.1403503 1.025713 1 | 2436 .0933195 .9697191 ---------+------------------------------ Total | 4059 -.0001139 .9989439 ---------------------------------------- . . ttest normexam, by(girl) Two-sample t test with equal variances ------------------------------------------------------------------------------ Group | Obs Mean Std. Err. Std. Dev. [95% Conf. Interval] ---------+-------------------------------------------------------------------- 0 | 1,623 -.1403503 .0254605 1.025713 -.1902892 -.0904115 1 | 2,436 .0933195 .0196475 .9697191 .0547919 .131847 ---------+-------------------------------------------------------------------- combined | 4,059 -.0001139 .0156795 .9989439 -.0308543 .0306265 ---------+-------------------------------------------------------------------- diff | -.2336698 .0318006 -.2960165 -.1713232 ------------------------------------------------------------------------------ diff = mean(0) - mean(1) t = -7.3480 Ho: diff = 0 degrees of freedom = 4057 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000 . . runmlwin normexam cons girl, level1(student: cons) nopause MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: IGLS Run time (seconds) = 0.89 Number of iterations = 2 Log likelihood = -5727.8509 Deviance = 11455.702 ------------------------------------------------------------------------------ normexam | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.1403503 .0246296 -5.70 0.000 -.1886235 -.0920772 girl | .2336698 .0317928 7.35 0.000 .1713571 .2959825 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .9845402 .0218544 .9417064 1.027374 ------------------------------------------------------------------------------ . . . . . * 2.4 Comparing more than two groups: Fixed effects models . . . . . . . .20 . . bysort school: egen mean_normexam = mean(normexam) . . egen pickone = tag(school) . . histogram mean_normexam if pickone==1, frequency (bin=8, start=-1.049087, width=.25657941) . . quietly tabulate school, generate(s) . . runmlwin normexam cons s1-s64, level1(student: cons) nopause MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: IGLS Run time (seconds) = 1.78 Number of iterations = 2 Log likelihood = -5391.4608 Deviance = 10782.922 ------------------------------------------------------------------------------ normexam | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.3086872 .1021126 -3.02 0.003 -.5088243 -.1085501 s1 | .8098967 .1478304 5.48 0.000 .5201543 1.099639 s2 | 1.091789 .1599798 6.82 0.000 .7782347 1.405344 s3 | 1.164132 .1626915 7.16 0.000 .8452622 1.483001 s4 | .3823156 .1448654 2.64 0.008 .0983847 .6662465 s5 | .7122957 .1850949 3.85 0.000 .3495164 1.075075 s6 | 1.253267 .1444091 8.68 0.000 .9702303 1.536304 s7 | .7001871 .1410889 4.96 0.000 .4236579 .9767163 s8 | .2604946 .1364002 1.91 0.056 -.006845 .5278341 s9 | -.126995 .1869789 -0.68 0.497 -.4934669 .2394768 s10 | .0392965 .1646517 0.24 0.811 -.2834149 .3620079 s11 | .8659096 .1545354 5.60 0.000 .5630258 1.168793 s12 | .2362085 .1678543 1.41 0.159 -.0927799 .5651969 s13 | .0629375 .153169 0.41 0.681 -.2372682 .3631431 s14 | .3242566 .1209955 2.68 0.007 .0871097 .5614034 s15 | .2680841 .1399771 1.92 0.055 -.0062659 .5424342 s16 | .0545691 .1410889 0.39 0.699 -.2219601 .3310982 s17 | .0632617 .1305653 0.48 0.628 -.1926417 .319165 s18 | .3025362 .1318269 2.29 0.022 .0441603 .5609121 s19 | .5149721 .1599798 3.22 0.001 .2014176 .8285267 s20 | .8077777 .1783695 4.53 0.000 .4581798 1.157376 s21 | .689012 .1478304 4.66 0.000 .3992697 .9787543 s22 | -.1895142 .1403404 -1.35 0.177 -.4645763 .085548 s23 | -.4289454 .2005453 -2.14 0.032 -.8220069 -.0358839 s24 | .3213285 .1815815 1.77 0.077 -.0345648 .6772217 s25 | -.3044192 .1478304 -2.06 0.039 -.5941615 -.0146769 s26 | -.083331 .1467962 -0.57 0.570 -.3710463 .2043842 s27 | -.0202094 .1783695 -0.11 0.910 -.3698073 .3293885 s28 | -.5640231 .1583078 -3.56 0.000 -.8743008 -.2537455 s29 | .3721952 .1448654 2.57 0.010 .0882643 .6561261 s30 | .6441434 .1740343 3.70 0.000 .3030425 .9852443 s31 | .072008 .1656824 0.43 0.664 -.2527235 .3967395 s32 | -.0626444 .1740343 -0.36 0.719 -.4037453 .2784565 s33 | .385799 .1458089 2.65 0.008 .1000188 .6715792 s34 | -.0622075 .2061796 -0.30 0.763 -.4663122 .3418972 s35 | .3926389 .1799404 2.18 0.029 .0399621 .7453156 s36 | .0594832 .1494776 0.40 0.691 -.2334876 .3524539 s37 | -.3566608 .2198712 -1.62 0.105 -.7876004 .0742787 s38 | .021142 .1608552 0.13 0.895 -.2941284 .3364124 s39 | .3470126 .1667493 2.08 0.037 .02019 .6738351 s40 | .0490109 .1489151 0.33 0.742 -.2428574 .3408792 s41 | .3816094 .1559796 2.45 0.014 .0758949 .6873239 s42 | .3313716 .1575089 2.10 0.035 .0226598 .6400834 s43 | .4562818 .1552473 2.94 0.003 .1520026 .760561 s44 | -.1250405 .1979674 -0.63 0.528 -.5130496 .2629686 s45 | .0831819 .1617586 0.51 0.607 -.2338592 .400223 s46 | -.1487162 .1430982 -1.04 0.299 -.4291836 .1317512 s47 | .1864295 .1435258 1.30 0.194 -.094876 .467735 s48 | -.1056077 .65384 -0.16 0.872 -1.38711 1.175895 s49 | .3555799 .1334501 2.66 0.008 .0940224 .6171373 s50 | -.0130929 .1478304 -0.09 0.929 -.3028352 .2766494 s51 | .0306169 .1575089 0.19 0.846 -.2780948 .3393287 s52 | .8420539 .1552473 5.42 0.000 .5377747 1.146333 s53 | 1.312235 .1494776 8.78 0.000 1.019265 1.605206 s54 | -.3249417 .3386693 -0.96 0.337 -.9887214 .338838 s55 | 1.025811 .1636553 6.27 0.000 .7050522 1.346569 s56 | .274407 .1799404 1.52 0.127 -.0782698 .6270837 s57 | .3097683 .1538428 2.01 0.044 .0082419 .6112947 s58 | .5905748 .1815815 3.25 0.001 .2346816 .946468 s59 | -.7403999 .1678543 -4.41 0.000 -1.069388 -.4114115 s60 | .5041154 .1444091 3.49 0.000 .2210788 .787152 s61 | .256353 .153169 1.67 0.094 -.0438526 .5565587 s62 | .3466774 .1489151 2.33 0.020 .0548091 .6385457 s63 | 1.044365 .1955308 5.34 0.000 .6611317 1.427598 s64 | .6524808 .1567332 4.16 0.000 .3452894 .9596721 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .8341594 .0185163 .7978681 .8704507 ------------------------------------------------------------------------------ . . estimates store model2 . . anova normexam school Number of obs = 4,059 R-squared = 0.1639 Root MSE = .920725 Adj R-squared = 0.1505 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 663.57984 64 10.368435 12.23 0.0000 | school | 663.57984 64 10.368435 12.23 0.0000 | Residual | 3385.8531 3,994 .84773488 -----------+---------------------------------------------------- Total | 4049.433 4,058 .99788885 . . quietly runmlwin normexam cons, level1(student: cons) nopause . . estimates store model1 . . lrtest model1 model2 Likelihood-ratio test LR chi2(64) = 726.44 (Assumption: model1 nested in model2) Prob > chi2 = 0.0000 . . generate boysch = (schgend==2) . . generate girlsch = (schgend==3) . . runmlwin normexam cons s1-s64 boysch girlsch, level1(student: cons) nopause note: boysch omitted because of collinearity note: girlsch omitted because of collinearity MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: IGLS Run time (seconds) = 1.57 Number of iterations = 2 Log likelihood = -5391.4608 Deviance = 10782.922 ------------------------------------------------------------------------------ normexam | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.3086872 .1021126 -3.02 0.003 -.5088243 -.1085501 s1 | .8098967 .1478304 5.48 0.000 .5201543 1.099639 s2 | 1.091789 .1599798 6.82 0.000 .7782347 1.405344 s3 | 1.164132 .1626915 7.16 0.000 .8452622 1.483001 s4 | .3823156 .1448654 2.64 0.008 .0983847 .6662465 s5 | .7122957 .1850949 3.85 0.000 .3495164 1.075075 s6 | 1.253267 .1444091 8.68 0.000 .9702303 1.536304 s7 | .7001871 .1410889 4.96 0.000 .4236579 .9767163 s8 | .2604946 .1364002 1.91 0.056 -.006845 .5278341 s9 | -.126995 .1869789 -0.68 0.497 -.4934669 .2394768 s10 | .0392965 .1646517 0.24 0.811 -.2834149 .3620079 s11 | .8659096 .1545354 5.60 0.000 .5630258 1.168793 s12 | .2362085 .1678543 1.41 0.159 -.0927799 .5651969 s13 | .0629375 .153169 0.41 0.681 -.2372682 .3631431 s14 | .3242566 .1209955 2.68 0.007 .0871097 .5614034 s15 | .2680841 .1399771 1.92 0.055 -.0062659 .5424342 s16 | .0545691 .1410889 0.39 0.699 -.2219601 .3310982 s17 | .0632617 .1305653 0.48 0.628 -.1926417 .319165 s18 | .3025362 .1318269 2.29 0.022 .0441603 .5609121 s19 | .5149721 .1599798 3.22 0.001 .2014176 .8285267 s20 | .8077777 .1783695 4.53 0.000 .4581798 1.157376 s21 | .689012 .1478304 4.66 0.000 .3992697 .9787543 s22 | -.1895142 .1403404 -1.35 0.177 -.4645763 .085548 s23 | -.4289454 .2005453 -2.14 0.032 -.8220069 -.0358839 s24 | .3213285 .1815815 1.77 0.077 -.0345648 .6772217 s25 | -.3044192 .1478304 -2.06 0.039 -.5941615 -.0146769 s26 | -.083331 .1467962 -0.57 0.570 -.3710463 .2043842 s27 | -.0202094 .1783695 -0.11 0.910 -.3698073 .3293885 s28 | -.5640231 .1583078 -3.56 0.000 -.8743008 -.2537455 s29 | .3721952 .1448654 2.57 0.010 .0882643 .6561261 s30 | .6441434 .1740343 3.70 0.000 .3030425 .9852443 s31 | .072008 .1656824 0.43 0.664 -.2527235 .3967395 s32 | -.0626444 .1740343 -0.36 0.719 -.4037453 .2784565 s33 | .385799 .1458089 2.65 0.008 .1000188 .6715792 s34 | -.0622075 .2061796 -0.30 0.763 -.4663122 .3418972 s35 | .3926389 .1799404 2.18 0.029 .0399621 .7453156 s36 | .0594832 .1494776 0.40 0.691 -.2334876 .3524539 s37 | -.3566608 .2198712 -1.62 0.105 -.7876004 .0742787 s38 | .021142 .1608552 0.13 0.895 -.2941284 .3364124 s39 | .3470126 .1667493 2.08 0.037 .02019 .6738351 s40 | .0490109 .1489151 0.33 0.742 -.2428574 .3408792 s41 | .3816094 .1559796 2.45 0.014 .0758949 .6873239 s42 | .3313716 .1575089 2.10 0.035 .0226598 .6400834 s43 | .4562818 .1552473 2.94 0.003 .1520026 .760561 s44 | -.1250405 .1979674 -0.63 0.528 -.5130496 .2629686 s45 | .0831819 .1617586 0.51 0.607 -.2338592 .400223 s46 | -.1487162 .1430982 -1.04 0.299 -.4291836 .1317512 s47 | .1864295 .1435258 1.30 0.194 -.094876 .467735 s48 | -.1056077 .65384 -0.16 0.872 -1.38711 1.175895 s49 | .3555799 .1334501 2.66 0.008 .0940224 .6171373 s50 | -.0130929 .1478304 -0.09 0.929 -.3028352 .2766494 s51 | .0306169 .1575089 0.19 0.846 -.2780948 .3393287 s52 | .8420539 .1552473 5.42 0.000 .5377747 1.146333 s53 | 1.312235 .1494776 8.78 0.000 1.019265 1.605206 s54 | -.3249417 .3386693 -0.96 0.337 -.9887214 .338838 s55 | 1.025811 .1636553 6.27 0.000 .7050522 1.346569 s56 | .274407 .1799404 1.52 0.127 -.0782698 .6270837 s57 | .3097683 .1538428 2.01 0.044 .0082419 .6112947 s58 | .5905748 .1815815 3.25 0.001 .2346816 .946468 s59 | -.7403999 .1678543 -4.41 0.000 -1.069388 -.4114115 s60 | .5041154 .1444091 3.49 0.000 .2210788 .787152 s61 | .256353 .153169 1.67 0.094 -.0438526 .5565587 s62 | .3466774 .1489151 2.33 0.020 .0548091 .6385457 s63 | 1.044365 .1955308 5.34 0.000 .6611317 1.427598 s64 | .6524808 .1567332 4.16 0.000 .3452894 .9596721 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .8341594 .0185163 .7978681 .8704507 ------------------------------------------------------------------------------ . . . . * 2.5 Comparing means: Random effects or multilevel model . . . . . . . .28 . . runmlwin normexam cons, level2(school: cons) level1(student: cons) nopause MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ school | 65 2 62.4 198 ----------------------------------------------------------- Run time (seconds) = 0.57 Number of iterations = 3 Log likelihood = -5505.324 Deviance = 11010.648 ------------------------------------------------------------------------------ normexam | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.0131668 .0536254 -0.25 0.806 -.1182706 .091937 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .168625 .0324466 .1050308 .2322193 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .8477613 .0189712 .8105785 .8849441 ------------------------------------------------------------------------------ . . runmlwin normexam cons boysch girlsch, /// > level2(school: cons) level1(student: cons) nopause MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: IGLS ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ school | 65 2 62.4 198 ----------------------------------------------------------- Run time (seconds) = 0.56 Number of iterations = 3 Log likelihood = -5502.966 Deviance = 11005.932 ------------------------------------------------------------------------------ normexam | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -.1013988 .0702043 -1.44 0.149 -.2389967 .0361992 boysch | .0643543 .1494051 0.43 0.667 -.2284743 .357183 girlsch | .2576005 .1168154 2.21 0.027 .0286465 .4865544 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .1551328 .0300495 .0962369 .2140288 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .8477943 .018972 .8106098 .8849788 ------------------------------------------------------------------------------ . . save "tutorial2.dta", replace file tutorial2.dta saved . . . . * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . 35 . . . . **************************************************************************** . exit end of do-file