***************************************************************************** * Stata do-file to replicate results given in the following presentation: * * TITLE: Running MLwiN from within Stata: The runmlwin command * AUTHORS: George Leckie and Chris Charlton * CONFERENCE: 17th Stata Users’ Group Meeting * LOCATION: Cass Business School, London * DATE: 16-09-2011 * * George Leckie and Chris Charlton * Centre for Multilevel Modelling, 2011 ***************************************************************************** * To run this do-file in Stata you must be working with: * * (1) Stata 11.2 or higher * * (2) The latest version of MLwiN * * (3) The latest version of the runmlwin command * * runmlwin will inform you if you are working with out of date versions of * Stata or the MLwiN software package. * * You must also tell runmlwin where MLwiN is installed. One way you can do * this is by specifying the MLwiN path by defining a global macro called * MLwiN_path. For example: * * . global MLwiN_path C:\Program Files\MLwiN v2.23\mlwin.exe * * See the "Remarks on installation instructions" section of the runmlwin help * file for further information: * * . help runmlwin * ***************************************************************************** * Change this global macro to point to your MLwiN path global MLwiN_path D:\Program Files (x86)\MLwiN v2.23\mlwin.exe * Open the tutorial data set use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear * Fit a two-level (students within schools) variance components model to * a continuous educational response variable, normexam. Note, you will need * to click the "Resume Macro" button twice in MLwiN to return the model * results to the Stata output window. runmlwin normexam cons, /// level2(school: cons) /// level1(student: cons) * Generate a boy dummy variable generate boy = 1 - girl * Extend the previous model to include fixed part covariates, a random school * level slope and separate level 1 residuals for boys and girls. The runmlwin * command also requests that runmlwin extracts the predicted values for the * school level residuals from MLwiN and returns them to Stata. The nopause * option prevents MLwiN from pausing before and after model estimation and so * returns the model results automatically to Stata. runmlwin normexam cons standlrt girl, /// level2(school: cons standlrt, residuals(u)) /// level1(student: girl boy, diagonal) nopause * Perform a Wald test to compare the boy and girl residual variances test [RP1]var(girl) = [RP1]var(boy) * Preserve the data as we will shortly be collapsing the data to the school * level, but afterwards we will want to return to the original data preserve * Tag one child in each school egen pickone = tag(school) * Collapse the data to one row per school keep if pickone==1 * Generate the ranks of the school level intercept residuals egen u0rank = rank(u0) * Produce a caterpillar plot for school level intercept residuals serrbar u0 u0se u0rank, scale(1.96) yline(0) * Restore the data to its state before we collapsed to the school level restore * Generate a binary pass/fail version of the continuous response gen binexam = (normexam>0) runmlwin binexam cons standlrt girl, /// level2(school: cons standlrt) /// level1(student:) /// discrete(d(binomial) l(logit) de(cons) pql2) /// nopause * Specify a vector of initial values for the following model matrix a = (-0.037,1.359,0.201,0.474,0.063,0.075,1) * Fit a two-level random intercepts random slopes model to the binary * response with the same fixed part and school level random part as * before using a burnin of 500 iterations and a chain of 5000 iterations runmlwin binexam cons standlrt girl, /// level2(school: cons standlrt) /// level1(student:) /// discrete(d(binomial) l(logit) de(cons)) /// mcmc(burnin(500) chain(5000)) /// initsb(a) nopause * Produce the MCMC trajectories plot for each parameter mcmcsum, trajectories * Produce the MCMC kernel densities plot for each parameter mcmcsum, densities * Produce the MCMC fiveway diagnostic plot for school level slope variance * parameter mcmcsum [RP2]var(standlrt), fiveway * Produce MCMC summary diagnostics for the school level slope variance * parameter mcmcsum [RP2]var(standlrt) * Re-specify the previous model, but additionally write the WinBUGS model * syntax, initial values and data to three separate text files. Note, we use * the nofit option to prevent MLwiN from actually fitting this model as we * have already done this above. runmlwin binexam cons standlrt girl, /// level2(school: cons standlrt) /// level1(student:) /// discrete(d(binomial) l(logit) de(cons)) /// mcmc(burnin(500) chain(5000) savewinbugs( /// model("m.txt", replace) /// inits("i.txt", replace) /// data("d.txt", replace) /// nofit) /// ) /// initsprevious nopause * View the WinBUGS model file in the Stata Viewer view m.txt ***************************************************************************** exit