------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\10_Modellin > g_Binary_Responses.smcl log type: smcl opened on: 27 Mar 2020, 17:50:40 . **************************************************************************** . * 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 . * http://www.bristol.ac.uk/cmm/software/runmlwin/ . **************************************************************************** . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/bang1.dta", clear . . describe Contains data from http://www.bristol.ac.uk/cmm/media/runmlwin/bang1.dta obs: 1,934 vars: 13 21 Oct 2011 12:19 ------------------------------------------------------------------------------- storage display value variable 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.05 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) = 2.98 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.05 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) = 7 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.05 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) = 4.97 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.05 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) = 0.62 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.05 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) = 6.04 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.05 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) = 6.86 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.05 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) = 8.68 Deviance (dbar) = 2327.27 Deviance (thetabar) = 2269.60 Effective no. of pars (pd) = 57.67 Bayesian DIC = 2384.94 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.723641 .1645528 55 0.000 -2.081246 -1.398094 age | -.0259363 .0077362 262 0.001 -.0413501 -.0110153 onekid | 1.12629 .1525135 233 0.000 .8373096 1.439499 twokids | 1.346535 .1685376 213 0.000 1.013076 1.672413 threeplusk~s | 1.343007 .170597 117 0.000 1.009437 1.659421 urban | .8393166 .1987613 77 0.000 .4609401 1.254963 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .4368685 .1454697 165 .2212415 .7852281 cov(cons,urban) | -.4615861 .1921197 129 -.9212328 -.1584836 var(urban) | .7889688 .3325157 116 .2958012 1.587222 ------------------------------------------------------------------------------ . . . . * 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.05 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) = 10.8 Deviance (dbar) = 2325.94 Deviance (thetabar) = 2269.22 Effective no. of pars (pd) = 56.72 Bayesian DIC = 2382.66 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.038382 .0899663 93 0.000 -1.211031 -.8568175 age | -.0163648 .0049208 205 0.000 -.0260278 -.0067306 onekid | .6817023 .0926713 197 0.000 .5070102 .8698451 twokids | .8219158 .1049742 183 0.000 .6181701 1.027163 threeplusk~s | .8241316 .108331 120 0.000 .5879764 1.0359 urban | .5029798 .1003369 124 0.000 .3138003 .7044826 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .159016 .0525263 157 .0775392 .2818248 cov(cons,urban) | -.17532 .0724821 106 -.3485249 -.0684016 var(urban) | .2992793 .129325 106 .1124159 .6202447 ------------------------------------------------------------------------------ . . 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.05 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) = 5.58 Deviance (dbar) = 2328.65 Deviance (thetabar) = 2272.37 Effective no. of pars (pd) = 56.28 Bayesian DIC = 2384.94 ------------------------------------------------------------------------------ use | Mean Std. Dev. ESS P [95% Cred. Interval] -------------+---------------------------------------------------------------- cons | -1.048481 .0947266 793 0.000 -1.236711 -.8682711 age | -.0164792 .0048471 1979 0.000 -.0261705 -.0070635 onekid | .6859656 .0953973 1984 0.000 .502068 .8715076 twokids | .8293769 .104187 2054 0.000 .6323714 1.033312 threeplusk~s | .8266129 .1076423 1723 0.000 .6197145 1.042486 urban | .5088691 .0987777 737 0.000 .3197373 .7085021 ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Random-effects Parameters | Mean Std. Dev. ESS [95% Cred. Int] -----------------------------+------------------------------------------------ Level 2: district | var(cons) | .1543232 .0493151 805 .077873 .269261 cov(cons,urban) | -.1598077 .0656256 550 -.3076762 -.0579773 var(urban) | .2651197 .1164211 345 .0988208 .5383534 ------------------------------------------------------------------------------ . . estimates store gibbs . . matrix gibbs_ess = e(ess) . . estimates table gibbs mh, b(%3.2f) ---------------------------------- Variable | gibbs mh -------------+-------------------- FP1 | cons | -1.05 -1.04 age | -0.02 -0.02 onekid | 0.69 0.68 twokids | 0.83 0.82 threeplusk~s | 0.83 0.82 urban | 0.51 0.50 -------------+-------------------- RP2 | var(cons) | 0.15 0.16 cov(cons\u~) | -0.16 -0.18 var(urban) | 0.27 0.30 -------------+-------------------- OD | bcons_1 | 1.00 1.00 ---------------------------------- . . matrix ess = (gibbs_ess', mh_ess') . . matrix list ess ess[10,2] r1 r1 FP1:cons 793 93 FP1:age 1979 205 FP1:onekid 1984 197 FP1:twokids 2054 183 FP1:threeplusk~s 1723 120 FP1:urban 737 124 RP2:var(cons) 805 157 RP2:cov(cons\u~) 550 106 RP2:var(urban) 345 106 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 "http://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.05 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) = 4.84 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