------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2024-10-11\18\4_Other_Fea > tures_of_Variance_Components_Models.smcl log type: smcl opened on: 11 Oct 2024, 16:49:39 . **************************************************************************** . * 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 . * https://www.bristol.ac.uk/cmm/software/runmlwin/ . **************************************************************************** . . * 4.1 Metropolis Hastings (MH) sampling for the variance components model 46 . . use "https://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear . . runmlwin normexam cons standlrt, /// > level2(school: cons) /// > level1(student: cons) /// > nopause MLwiN 3.13 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) = 2.68 Number of iterations = 4 Log likelihood = -4678.6211 Deviance = 9357.2422 ------------------------------------------------------------------------------ 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.003 -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.13 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) = 8.9 Deviance (dbar) = 9208.98 Deviance (thetabar) = 9148.81 Effective no. of pars (pd) = 60.18 Bayesian DIC = 9269.16 ------------------------------------------------------------------------------ normexam | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -.0003505 .0421778 34 0.489 -.0829767 .0854788 standlrt | .5637709 .0123897 236 0.000 .5386738 .586561 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .0979511 .0201498 2230 .0652301 .1433059 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .565848 .01254 4578 .5418827 .5907224 ------------------------------------------------------------------------------ . . 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.13 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) = 12.8 Deviance (dbar) = 9209.08 Deviance (thetabar) = 9149.07 Effective no. of pars (pd) = 60.01 Bayesian DIC = 9269.09 ------------------------------------------------------------------------------ normexam | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | .0031558 .0442179 222 0.473 -.083354 .0918911 standlrt | .5632158 .0125783 3766 0.000 .5384442 .5878019 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: school | var(cons) | .0974233 .0198761 3004 .0647167 .1414378 -----------------------------+------------------------------------------------ Level 1: student | var(cons) | .566177 .0124851 5061 .5421247 .5909016 ------------------------------------------------------------------------------ . . use "u.dta", clear . . keep iteration school value . . rename value u0_ . . reshape wide u0, i(iteration) j(school) (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 52 53 5 > 4 55 56 57 58 59 60 61 62 63 64 65) Data Long -> Wide ----------------------------------------------------------------------------- Number of observations 325,065 -> 5,001 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 .3724692 0.5% .1284774 Thinned Chain Length 5001 MCSE of Mean .0015882 2.5% .187386 Effective Sample Size 1539 Std. Dev. .094254 5% .2202634 Raftery Lewis (2.5%) 4222 Mode .3711377 25% .3090198 Raftery Lewis (97.5%) 3929 P(mean) 0.000 Brooks Draper (mean) 1939 P(mode) 0.000 50% .3713689 P(median) 0.000 75% .4345235 95% .5297607 97.5% .5618089 99.5% .6154772 ------------------------------------------------------------------------------ . . mcmcsum u0_1, variables fiveway . . . . * 4.7 Comparing two schools . . . . . . . . . . . . . . . . . . . . . . . 54 . . gen diff = u0_1 - u0_2 . . mcmcsum diff, variables detail diff ------------------------------------------------------------------------------ Percentiles Mean -.1290953 0.5% -.4568056 Thinned Chain Length 5001 MCSE of Mean .0018255 2.5% -.3800029 Effective Sample Size 4719 Std. Dev. .1280991 5% -.3413402 Raftery Lewis (2.5%) 3646 Mode -.1329369 25% -.2177141 Raftery Lewis (97.5%) 3679 P(mean) 0.159 Brooks Draper (mean) 2561 P(mode) 0.159 50% -.1299073 P(median) 0.159 75% -.042855 95% .0804123 97.5% .1225501 99.5% .2007739 ------------------------------------------------------------------------------ . . mcmcsum diff, variables fiveway . . gen prop = (diff>0) . . sum prop Variable | Obs Mean Std. dev. Min Max -------------+--------------------------------------------------------- prop | 5,001 .1587682 .3654964 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 "https://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 .146123 0.5% .0916379 Thinned Chain Length 5001 MCSE of Mean .000455 2.5% .102413 Effective Sample Size 2953 Std. Dev. .0253529 5% .1082295 Raftery Lewis (2.5%) 4434 Mode .1420195 25% .1285175 Raftery Lewis (97.5%) 3865 P(mean) 0.000 Brooks Draper (mean) 160 P(mode) 0.000 50% .1442902 P(median) 0.000 75% .1614671 95% .1906697 97.5% .2002375 99.5% .2210128 ------------------------------------------------------------------------------ . . mcmcsum isc, variables fiveway . . . . * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . . . 60 . . . . . . **************************************************************************** . exit end of do-file