------------------------------------------------------------------------------- name: log: Q:\C-modelling\runmlwin\website\logfiles\2020-03-27\16\16_An_Intro > duction_to_Simulation_Methods_of_Estimation.smcl log type: smcl opened on: 27 Mar 2020, 17:43:16 . **************************************************************************** . * MLwiN User Manual . * . * 16 An Introduction to Simulation Methods of Estimation 241 . * . * 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/ . **************************************************************************** . . * 16.1 An illustration of parameter estimation with Normally distributed . . . * . . .data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/height.dta", clear . . summarize height Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- height | 100 175.35 10.0024 154 204 . . histogram height (bin=10, start=154, width=5) . . quietly summarize height . . display 1 - normal((200 - r(mean))/r(sd)) .00686197 . . capture program drop heightsim1 . . program define heightsim1, rclass 1. . drop _all 2. . set obs 100 3. . generate heightsim = 175.35 + 10.002*invnorm(uniform()) 4. . summarize heightsim 5. . return scalar pmean = r(mean) 6. . return scalar pvar = r(Var) 7. end . . simulate, reps(1000) seed(12345): heightsim1 command: heightsim1 pvar: r(pvar) pmean: r(pmean) Simulations (1000) ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 .................................................. 50 .................................................. 100 .................................................. 150 .................................................. 200 .................................................. 250 .................................................. 300 .................................................. 350 .................................................. 400 .................................................. 450 .................................................. 500 .................................................. 550 .................................................. 600 .................................................. 650 .................................................. 700 .................................................. 750 .................................................. 800 .................................................. 850 .................................................. 900 .................................................. 950 .................................................. 1000 . // Note: To obtain estimates as close as possible to the manual, ncrease the . // number of reps to 10000. . . generate iteration = _n . . line pmean iteration . . kdensity pmean . . centile pmean, centile(2.5 97.5) -- Binom. Interp. -- Variable | Obs Percentile Centile [95% Conf. Interval] -------------+------------------------------------------------------------- pmean | 1,000 2.5 173.2063 173.0833 173.4998 | 97.5 177.2738 177.1252 177.4803 . . line pvar iteration . . kdensity pvar . . centile pvar, centile(2.5 97.5) -- Binom. Interp. -- Variable | Obs Percentile Centile [95% Conf. Interval] -------------+------------------------------------------------------------- pvar | 1,000 2.5 72.8153 70.54017 74.12077 | 97.5 128.0812 126.7303 132.4153 . . . . use "http://www.bristol.ac.uk/cmm/media/runmlwin/height.dta", clear . . save "height.dta", replace (note: file height.dta not found) file height.dta saved . . capture program drop heightsim2 . . program define heightsim2, rclass 1. . use "height.dta", clear 2. . bsample 3. . summarize height 4. . return scalar npmean = r(mean) 5. . return scalar npvar = r(Var) 6. end . . simulate, reps(1000) seed(12345): heightsim2 command: heightsim2 npvar: r(npvar) npmean: r(npmean) Simulations (1000) ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 .................................................. 50 .................................................. 100 .................................................. 150 .................................................. 200 .................................................. 250 .................................................. 300 .................................................. 350 .................................................. 400 .................................................. 450 .................................................. 500 .................................................. 550 .................................................. 600 .................................................. 650 .................................................. 700 .................................................. 750 .................................................. 800 .................................................. 850 .................................................. 900 .................................................. 950 .................................................. 1000 . // Note: To obtain estimates as close as possible to the manual, increase the . // number of reps to 10000. . . erase "height.dta" . . generate iteration = _n . . line npmean iteration . . kdensity npmean . . centile npmean, centile(2.5 97.5) -- Binom. Interp. -- Variable | Obs Percentile Centile [95% Conf. Interval] -------------+------------------------------------------------------------- npmean | 1,000 2.5 173.391 173.2132 173.59 | 97.5 177.29 177.1536 177.4074 . . line npvar iteration . . kdensity npvar . . centile npvar, centile(2.5 97.5) -- Binom. Interp. -- Variable | Obs Percentile Centile [95% Conf. Interval] -------------+------------------------------------------------------------- npvar | 1,000 2.5 73.62636 70.95973 76.35469 | 97.5 127.5858 124.643 129.8879 . . . . * 16.2 Generating random numbers in MLwiN . . . . . . . . . . . . . . . .249 . . clear . . set obs 100 number of observations (_N) was 0, now 100 . . generate female = (uniform()<=0.6) . . generate height2 = (1 - female)*(175 + 10*invnorm(uniform())) + female*(160 + > 8*invnorm(uniform())) . . histogram height2, by(female) . . . . * Chapter learning outcomes . . . . . . . . . . . . . . . . . . . . . . .253 . . . . **************************************************************************** . exit end of do-file