------------------------------------------------------------------------------- name: <unnamed> log: Q:\C-modelling\runmlwin\website\logfiles\2024-10-11\18\10_Modellin > g_Binary_Responses.smcl log type: smcl opened on: 11 Oct 2024, 17:09:23 . **************************************************************************** . * MLwiN MCMC Manual . * . * 10 Modelling Binary Responses . . . . . . . . . . . . . . . . . . . . 129 . * . * 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/ . **************************************************************************** . . use "https://www.bristol.ac.uk/cmm/media/runmlwin/bang1.dta", clear . . describe Contains data from https://www.bristol.ac.uk/cmm/media/runmlwin/bang1.dta Observations: 1,934 Variables: 13 21 Oct 2011 12:19 ------------------------------------------------------------------------------- Variable Storage Display Value name type format label Variable label ------------------------------------------------------------------------------- woman int %9.0g district byte %9.0g use byte %9.0g lc byte %9.0g lc age float %9.0g urban byte %9.0g educ byte %9.0g hindu byte %9.0g d_illit float %9.0g d_pray float %9.0g cons byte %9.0g bcons byte %9.0g denomb byte %9.0g ------------------------------------------------------------------------------- Sorted by: . . . . * 10.1 Simple logistic regression model . . . . . . . . . . . . . . . . .130 . . quietly runmlwin use cons age, /// > level2(district:) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) nopause . . runmlwin use cons age, /// > level2(district:) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(on) initsprevious nopause level(90) MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 11.8 Deviance (dbar) = 2591.29 Deviance (thetabar) = 2589.29 Effective no. of pars (pd) = 2.00 Bayesian DIC = 2593.29 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [90% Cred. Interval] -------------+---------------------------------------------------------------- cons | -.4385755 .047307 1198 0.000 -.515495 -.3615458 age | .0068138 .0050855 1235 0.089 -.0014546 .0150039 ------------------------------------------------------------------------------ . . mcmcsum [FP1]age, detail [FP1]age ------------------------------------------------------------------------------ Percentiles Mean .0068138 0.5% -.0068774 Thinned Chain Length 5000 MCSE of Mean .0001503 2.5% -.0033339 Effective Sample Size 1235 Std. Dev. .0050855 5% -.0014546 Raftery Lewis (2.5%) 13099 Mode .0066046 25% .0034059 Raftery Lewis (97.5%) 13496 P(mean) 0.089 Brooks Draper (mean) 173547 P(mode) 0.089 50% .0067932 P(median) 0.089 75% .0104303 95% .0150039 97.5% .0164863 99.5% .01952 ------------------------------------------------------------------------------ . . mcmcsum [FP1]age, fiveway . . runmlwin use cons age, /// > level2(district:) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(chain(15000)) initsprevious nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 15000 Thinning = 1 Run time (seconds) = 27 Deviance (dbar) = 2591.29 Deviance (thetabar) = 2589.29 Effective no. of pars (pd) = 2.00 Bayesian DIC = 2593.29 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -.4394264 .045736 3686 0.000 -.5284889 -.350504 age | .006372 .0052533 3663 0.113 -.0040397 .0165157 ------------------------------------------------------------------------------ . // Note: Here runmlwin is fitting 15000 iterations from scratch. This . // contrasts the manual where we are fitting 10000 iterations in addition . // to the original 5000 giving a total of 15000.. . . gen onekid = (lc==1) . . gen twokids = (lc==2) . . gen threepluskids = (lc==3) . . quietly runmlwin use cons age onekid twokids threepluskids, /// > level2(district:) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) nopause . . runmlwin use cons age onekid twokids threepluskids, /// > level2(district:) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(on) initsprevious nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 16.5 Deviance (dbar) = 2519.98 Deviance (thetabar) = 2515.10 Effective no. of pars (pd) = 4.88 Bayesian DIC = 2524.87 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.257306 .1172414 74 0.000 -1.50365 -1.044943 age | -.0216689 .0072982 154 0.000 -.0368835 -.0075772 onekid | 1.023307 .1556602 145 0.000 .7301328 1.345265 twokids | 1.178552 .162634 120 0.000 .8654791 1.49371 threeplusk~s | 1.104576 .1711481 84 0.000 .7886095 1.455924 ------------------------------------------------------------------------------ . . . . * 10.2 Random effects logistic regression model . . . . . . . . . . . . .136 . . runmlwin use cons age onekid twokids threepluskids, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: IGLS, MQL1 ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Run time (seconds) = 3.02 Number of iterations = 5 ------------------------------------------------------------------------------ use | Coef. Std. Err. z P>|z| [95% Conf. Interval] -------------+---------------------------------------------------------------- cons | -1.368827 .1387225 -9.87 0.000 -1.640718 -1.096935 age | -.0235103 .007618 -3.09 0.002 -.0384414 -.0085793 onekid | 1.020336 .1534657 6.65 0.000 .7195486 1.321123 twokids | 1.218038 .1687095 7.22 0.000 .8873739 1.548703 threeplusk~s | 1.194597 .1734567 6.89 0.000 .8546283 1.534566 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .2463276 .0747691 .0997829 .3928723 ------------------------------------------------------------------------------ . . runmlwin use cons age onekid twokids threepluskids, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(on) initsprevious nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 20.3 Deviance (dbar) = 2396.32 Deviance (thetabar) = 2355.12 Effective no. of pars (pd) = 41.20 Bayesian DIC = 2437.53 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.489344 .1376958 59 0.000 -1.767119 -1.228622 age | -.0260354 .0077301 216 0.000 -.0411373 -.011358 onekid | 1.110088 .1524513 259 0.000 .813606 1.417751 twokids | 1.329594 .1689007 149 0.000 1.006129 1.666637 threeplusk~s | 1.305332 .1687296 80 0.000 .9843593 1.623929 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .3022512 .0979855 474 .1503189 .5334598 ------------------------------------------------------------------------------ . . mcmcsum [RP2]var(cons), detail [RP2]var(cons) ------------------------------------------------------------------------------ Percentiles Mean .3022512 0.5% .1230625 Thinned Chain Length 5000 MCSE of Mean .0029601 2.5% .1503189 Effective Sample Size 474 Std. Dev. .0979855 5% .1665779 Raftery Lewis (2.5%) 12836 Mode .2711853 25% .2307661 Raftery Lewis (97.5%) 10430 P(mean) 0.000 Brooks Draper (mean) 6732 P(mode) 0.000 50% .2893552 P(median) 0.000 75% .3595042 95% .4803195 97.5% .5334598 99.5% .6343197 ------------------------------------------------------------------------------ . . mcmcsum [RP2]var(cons), fiveway . . . . * 10.3 Random coefficients for area type . . . . . . . . . . . . . . . . 139 . . quietly runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) nopause . . runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(on) initsprevious nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 21.9 Deviance (dbar) = 2370.45 Deviance (thetabar) = 2331.27 Effective no. of pars (pd) = 39.18 Bayesian DIC = 2409.63 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.722633 .1564096 85 0.000 -2.040373 -1.434551 age | -.0273354 .0084955 199 0.000 -.0450483 -.0112923 onekid | 1.128914 .1646927 173 0.000 .8040234 1.44924 twokids | 1.386754 .1807106 135 0.000 1.058083 1.759067 threeplusk~s | 1.374087 .1998872 95 0.000 1.008914 1.789485 urban | .7392214 .1167651 502 0.000 .5070931 .9664549 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .2344319 .0851539 292 .0985325 .4319376 ------------------------------------------------------------------------------ . . quietly runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons urban) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) nopause . . runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons urban) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(on) initsprevious nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 25.4 Deviance (dbar) = 2328.81 Deviance (thetabar) = 2271.29 Effective no. of pars (pd) = 57.53 Bayesian DIC = 2386.34 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.724727 .1608096 78 0.000 -2.057061 -1.436916 age | -.0269958 .0077822 213 0.000 -.0424352 -.0116732 onekid | 1.140499 .1610437 246 0.000 .8443593 1.464803 twokids | 1.371608 .1713673 187 0.000 1.037695 1.698188 threeplusk~s | 1.366217 .1767875 118 0.000 1.023669 1.720137 urban | .8306543 .1920122 96 0.000 .4591159 1.215496 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .4339787 .149603 185 .2080162 .7832098 cov(cons,urban) | -.4587883 .1958078 104 -.9070693 -.1595674 var(urban) | .7720941 .3588608 98 .2745705 1.659193 ------------------------------------------------------------------------------ . . . . * 10.4 Probit regression . . . . . . . . . . . . . . . . . . . . . . . . 141 . . * 10.5 Running a probit regression in MLwiN . . . . . . . . . . . . . . .142 . . quietly runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons urban) /// > level1(woman:) /// > discrete(distribution(binomial) link(probit) denom(cons)) nopause . . matrix b = e(b) . . matrix V = e(V) . . runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons urban) /// > level1(woman:) /// > discrete(distribution(binomial) link(probit) denom(cons)) /// > mcmc(on) initsb(b) initsv(V) nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial probit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 33.7 Deviance (dbar) = 2327.26 Deviance (thetabar) = 2270.55 Effective no. of pars (pd) = 56.72 Bayesian DIC = 2383.98 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.055831 .0928036 84 0.000 -1.253128 -.8847331 age | -.0161719 .0049991 178 0.001 -.0262923 -.0068061 onekid | .6917918 .0970108 137 0.000 .4978677 .8724751 twokids | .8289826 .1063514 139 0.000 .5996459 1.030021 threeplusk~s | .826828 .1137056 56 0.000 .5961093 1.062812 urban | .5145897 .1073405 130 0.000 .3057807 .7200971 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .1596531 .0525276 234 .0811271 .281848 cov(cons,urban) | -.1699949 .0693228 132 -.3381742 -.0668061 var(urban) | .286465 .1180859 134 .1208212 .5769592 ------------------------------------------------------------------------------ . . estimates store mh . . matrix mh_ess = e(ess) . . runmlwin use cons age onekid twokids threepluskids urban, /// > level2(district: cons urban) /// > level1(woman:) /// > discrete(distribution(binomial) link(probit) denom(cons)) /// > mcmc(femethod(gibbs) remethod(gibbs)) /// > initsb(b) initsv(V) nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial probit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 17.5 Deviance (dbar) = 2327.59 Deviance (thetabar) = 2271.08 Effective no. of pars (pd) = 56.51 Bayesian DIC = 2384.10 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.04158 .0977987 782 0.000 -1.232331 -.8516307 age | -.0162636 .0048217 1899 0.001 -.0259902 -.0067695 onekid | .6877396 .0969737 1766 0.000 .4947631 .8745657 twokids | .8237693 .1063055 1788 0.000 .6172041 1.033748 threeplusk~s | .8264746 .1084864 1691 0.000 .6152342 1.041743 urban | .4956297 .1056626 711 0.000 .2884581 .6974879 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .1580596 .0504436 875 .0792161 .2720135 cov(cons,urban) | -.1641594 .0673555 597 -.31634 -.0552599 var(urban) | .2726501 .1165671 504 .1019579 .5562358 ------------------------------------------------------------------------------ . . estimates store gibbs . . matrix gibbs_ess = e(ess) . . estimates table gibbs mh, b(%3.2f) ---------------------------------- Variable | gibbs mh -------------+-------------------- FP1 | cons | -1.04 -1.06 age | -0.02 -0.02 onekid | 0.69 0.69 twokids | 0.82 0.83 threeplusk~s | 0.83 0.83 urban | 0.50 0.51 -------------+-------------------- RP2 | var(cons) | 0.16 0.16 cov(cons\u~) | -0.16 -0.17 var(urban) | 0.27 0.29 -------------+-------------------- OD | bcons_1 | 1.00 1.00 ---------------------------------- . . matrix ess = (gibbs_ess', mh_ess') . . matrix list ess ess[10,2] r1 r1 FP1:cons 782 84 FP1:age 1899 178 FP1:onekid 1766 137 FP1:twokids 1788 139 FP1:threeplusk~s 1691 56 FP1:urban 711 130 RP2:var(cons) 875 234 RP2:cov(cons\u~) 597 132 RP2:var(urban) 504 134 OD:bcons_1 0 0 . . . . * 10.6 Comparison with WinBUGS . . . . . . . . . . . . . . . . . . . . . 144 . . quietly runmlwin use cons age, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) nopause . . matrix b = e(b) . . matrix V = e(V) . . quietly runmlwin use cons age, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(savewinbugs( /// > model("bang_model.txt", replace) /// > inits("bang_inits.txt", replace) /// > data("bang_data.txt", replace) /// > nofit)) /// > initsb(b) initsv(V) nopause . . view "bang_model.txt" . . /* There is a known MLwiN bug here which will be fixed in version 2.29 > wbscript , /// > model("`c(pwd)'\bang_model.txt") /// > data("`c(pwd)'\bang_data.txt") /// > inits("`c(pwd)'\bang_inits.txt") /// > coda("`c(pwd)'\out") /// > set(beta sigma2.u2) /// > burn(4000) update(5000) /// > saving("`c(pwd)'\script.txt", replace) /// > quit > > wbrun, script("`c(pwd)'\script.txt") /// > winbugs("C:\WinBUGS14\winbugs14.exe") > > wbcoda, root("`c(pwd)'\out") clear > > mcmcsum beta_1, variables > > mcmcsum beta_1, variables fiveway > */ . . use "https://www.bristol.ac.uk/cmm/media/runmlwin/bang1.dta", clear . . runmlwin use cons age, /// > level2(district: cons) /// > level1(woman:) /// > discrete(distribution(binomial) link(logit) denom(cons)) /// > mcmc(on) initsb(b) initsv(V) nopause MLwiN 3.13 multilevel model Number of obs = 1934 Binomial logit response model (hierarchical) Estimation algorithm: MCMC ----------------------------------------------------------- | No. of Observations per Group Level Variable | Groups Minimum Average Maximum ----------------+------------------------------------------ district | 60 2 32.2 118 ----------------------------------------------------------- Burnin = 500 Chain = 5000 Thinning = 1 Run time (seconds) = 17.1 Deviance (dbar) = 2477.18 Deviance (thetabar) = 2440.20 Effective no. of pars (pd) = 36.98 Bayesian DIC = 2514.16 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -.5368289 .0871881 221 0.000 -.7229493 -.3775872 age | .0085463 .0052379 1183 0.050 -.0012621 .0189108 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .2638906 .0892931 404 .131411 .4693553 ------------------------------------------------------------------------------ . . mcmcsum [FP1]cons, detail [FP1]cons ------------------------------------------------------------------------------ Percentiles Mean -.5368289 0.5% -.7872517 Thinned Chain Length 5000 MCSE of Mean .004917 2.5% -.7229493 Effective Sample Size 221 Std. Dev. .0871881 5% -.6841772 Raftery Lewis (2.5%) 25734 Mode -.539376 25% -.590112 Raftery Lewis (97.5%) 19686 P(mean) 0.000 Brooks Draper (mean) 18575 P(mode) 0.000 50% -.5356112 P(median) 0.000 75% -.4778811 95% -.3994635 97.5% -.3775872 99.5% -.3221758 ------------------------------------------------------------------------------ . . mcmcsum [FP1]cons, fiveway . . . . * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . . .151 . . . . . . **************************************************************************** . exit end of do-file