------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\4_Other_Fea > tures_of_Variance_Components_Models.smcl log type: smcl opened on: 27 Mar 2020, 17:44:37 . **************************************************************************** . * MLwiN MCMC Manual . * . * 4 Other Features of Variance Components Models . . . . . . . . . . . .45 . * . * Browne, W. J. (2009). MCMC Estimation in 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/ . **************************************************************************** . . * 4.1 Metropolis Hastings (MH) sampling for the variance components model 46 . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear . . runmlwin normexam cons standlrt, /// > 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 = 4 Log likelihood = -4678.6211 Deviance = 9357.2423 ------------------------------------------------------------------------------ normexam | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | .0023908 .0400224 0.06 0.952 -.0760516 .0808332 standlrt | .5633712 .0124654 45.19 0.000 .5389395 .5878029 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .0921275 .0181475 .0565591 .127696 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .565731 .0126585 .5409208 .5905412 ------------------------------------------------------------------------------ . . estimates store igls . . matrix b = e(b) . . matrix V = e(V) . . quietly runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > mcmc(on) initsb(b) initsv(V) nopause . . estimates store gibbs . . . . * 4.2 Metropolis-Hastings settings . . . . . . . . . . . . . . . . . . . .47 . . . . * 4.3 Running the variance components with Metropolis Hastings . . . . . .48 . . quietly runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > mcmc(femethod(univariatemh) remethod(univariatemh)) /// > initsb(b) initsv(V) nopause . . estimates store mh_adaptive . . mcmcsum [FP1]standlrt, detail [FP1]standlrt ------------------------------------------------------------------------------ Percentiles Mean .563338 0.5% .5321006 Thinned Chain Length 5000 MCSE of Mean .0003827 2.5% .5387213 Effective Sample Size 1055 Std. Dev. .0124799 5% .5428947 Raftery Lewis (2.5%) 12654 Mode .5637112 25% .5551378 Raftery Lewis (97.5%) 13584 P(mean) 0.000 Brooks Draper (mean) 113 P(mode) 0.000 50% .563556 P(median) 0.000 75% .5715874 95% .5842525 97.5% .5881385 99.5% .5962517 ------------------------------------------------------------------------------ . . mcmcsum [FP1]standlrt, fiveway . . quietly runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > mcmc(femethod(univariatemh) remethod(univariatemh) noadapt) /// > initsb(b) initsv(V) nopause . . estimates store mh_scale . . estimates table igls gibbs mh_scale mh_adaptive, b(%4.3f) ------------------------------------------------------ Variable | igls gibbs mh_sc~e mh_ad~e -------------+---------------------------------------- FP1 | cons | 0.002 0.001 -0.005 0.001 standlrt | 0.563 0.563 0.563 0.563 -------------+---------------------------------------- RP2 | var(cons) | 0.092 0.097 0.096 0.097 -------------+---------------------------------------- RP1 | var(cons) | 0.566 0.566 0.566 0.566 ------------------------------------------------------ . . . . . * 4.4 MH cycles per Gibbs iteration . . . . . . . . . . . . . . . . . . . 49 . . . . * 4.5 Block updating MH sampling . . . . . . . . . . . . . . . . . . . . .49 . . runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > mcmc(femethod(multivariatemh) remethod(univariatemh) acceptance(0.4)) > /// > initsb(b) initsv(V) nopause MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ school | 65 2 62.4 198 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 2.36 Deviance (dbar) = 9209.16 Deviance (thetabar) = 9149.36 Effective no. of pars (pd) = 59.80 Bayesian DIC = 9268.97 ------------------------------------------------------------------------------ normexam | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | .0037085 .0417094 36 0.449 -.0860144 .0817368 standlrt | .5636631 .0127263 219 0.000 .5386203 .5877184 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .0966746 .0202706 2303 .0644674 .1428891 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .5662289 .0125336 4759 .5425205 .5914839 ------------------------------------------------------------------------------ . . mcmcsum in 4951/5000, trajectories . . . . * 4.6 Residuals in MCMC . . . . . . . . . . . . . . . . . . . . . . . . . 51 . . quietly runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > nopause . . runmlwin normexam cons standlrt, /// > level2(school: cons, residuals(u, savechains("u.dta", replace))) /// > level1(student: cons) /// > mcmc(chain(5001)) /// > initsprevious nopause MLwiN 3.05 multilevel model Number of obs = 4059 Normal response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ school | 65 2 62.4 198 ----------------------------------------------------------- Burnin = 500 Chain = 5001 Thinning = 1 Run time (seconds) = 10.3 Deviance (dbar) = 9208.82 Deviance (thetabar) = 9148.97 Effective no. of pars (pd) = 59.85 Bayesian DIC = 9268.67 ------------------------------------------------------------------------------ normexam | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | .0013397 .042154 231 0.498 -.0776169 .0851899 standlrt | .563306 .0125423 3918 0.000 .5388907 .587993 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .0973239 .0204918 2832 .0644136 .1445378 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .5663482 .0126933 4924 .5417525 .5920476 ------------------------------------------------------------------------------ . . use "u.dta", clear . . keep iteration school value . . rename value u0_ . . reshape wide u0, i(iteration) j(school) (note: j = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 > 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5 > 2 53 54 55 56 57 58 59 60 61 62 63 64 65) Data long -> wide ----------------------------------------------------------------------------- Number of obs. 325065 -> 5001 Number of variables 3 -> 66 j variable (65 values) school -> (dropped) xij variables: u0_ -> u0_1 u0_2 ... u0_65 ----------------------------------------------------------------------------- . . mcmcsum u0_1, variables detail u0_1 ------------------------------------------------------------------------------ Percentiles Mean .3735389 0.5% .1302519 Thinned Chain Length 5001 MCSE of Mean .0015708 2.5% .1894161 Effective Sample Size 1750 Std. Dev. .0929649 5% .2210402 Raftery Lewis (2.5%) 3955 Mode .3740578 25% .3133567 Raftery Lewis (97.5%) 3865 P(mean) 0.000 Brooks Draper (mean) 1897 P(mode) 0.000 50% .3747569 P(median) 0.000 75% .4358414 95% .5227967 97.5% .5567949 99.5% .6368156 ------------------------------------------------------------------------------ . . mcmcsum u0_1, variables fiveway . . . . * 4.7 Comparing two schools . . . . . . . . . . . . . . . . . . . . . . . 54 . . gen diff = u0_1 - u0_2 . . mcmcsum diff, variables detail diff ------------------------------------------------------------------------------ Percentiles Mean -.1293446 0.5% -.460066 Thinned Chain Length 5001 MCSE of Mean .0018406 2.5% -.3776349 Effective Sample Size 5326 Std. Dev. .1271746 5% -.3365295 Raftery Lewis (2.5%) 3766 Mode -.1274151 25% -.2159065 Raftery Lewis (97.5%) 3679 P(mean) 0.153 Brooks Draper (mean) 2604 P(mode) 0.153 50% -.1286535 P(median) 0.153 75% -.0445563 95% .0782901 97.5% .1189055 99.5% .2111304 ------------------------------------------------------------------------------ . . mcmcsum diff, variables fiveway . . gen prop = (diff>0) . . sum prop Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- prop | 5,001 .1529694 .359994 0 1 . . . . * 4.8 Calculating ranks of schools . . . . . . . . . . . . . . . . . . . .55 . . use "u.dta", clear . . keep iteration school value . . rename value u0 . . bysort iteration: egen u0rank = rank(u0), unique . . drop u0 . . bysort school: egen u0rankmn = mean(u0rank) . . bysort school: egen u0ranklo = pctile(u0rank), p(2.5) . . bysort school: egen u0rankmd = pctile(u0rank), p(50) . . bysort school: egen u0rankhi = pctile(u0rank), p(97.5) . . keep school u0rankmn u0ranklo u0rankmd u0rankhi . . duplicates drop Duplicates in terms of all variables (325,000 observations deleted) . . twoway (scatter u0rankmd school) (rcap u0rankhi u0ranklo school), /// > ytitle("Rank") xtitle("School") legend(off) . . egen rank = rank(u0rankmn), unique . . twoway (scatter u0rankmn rank) (rcap u0rankhi u0ranklo rank), /// > ytitle("Rank") xtitle("School") legend(off) . . . . * 4.9 Estimating a function of parameters . . . . . . . . . . . . . . . . 58 . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear . . quietly runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > nopause . . quietly runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > mcmc(chain(5001)) /// > initsprevious nopause . . mcmcsum, getchains . . gen isc = RP2_var_cons_/( RP2_var_cons_ + RP1_var_cons_) . . mcmcsum isc, variables detail isc ------------------------------------------------------------------------------ Percentiles Mean .1459128 0.5% .0915792 Thinned Chain Length 5001 MCSE of Mean .0004803 2.5% .1015487 Effective Sample Size 2799 Std. Dev. .0260634 5% .1081655 Raftery Lewis (2.5%) 4434 Mode .1398498 25% .1272494 Raftery Lewis (97.5%) 4128 P(mean) 0.000 Brooks Draper (mean) 178 P(mode) 0.000 50% .1434288 P(median) 0.000 75% .1621036 95% .1924947 97.5% .2036141 99.5% .2264668 ------------------------------------------------------------------------------ . . mcmcsum isc, variables fiveway . . . . * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . . . 60 . . . . . . **************************************************************************** . exit end of do-file