***************************************************************************** * Stata do-file to replicate results given in the following presentation: * * TITLE: Running MLwiN from within Stata: The runmlwin command * AUTHORS: George Leckie * MEETING: Research Workshop in Multilevel Modelling using MLwiN, University * of Bristol * LOCATION: Bristol * DATE: 13-09-2013 * * George Leckie * Centre for Multilevel Modelling, 2013 ***************************************************************************** * To run this do-file in Stata you must be working with: * * (1) Stata 9.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.28\mlwin.exe * * See the "Remarks on installation instructions" section of the runmlwin help * file for further information: * * . help runmlwin * ***************************************************************************** **************************************************************************** * 1. Example analyses using the Hedonism in Europe data **************************************************************************** * Load the hedonism.dta dataset use http://www.bristol.ac.uk/cmm/media/runmlwin/hedonism, clear * Describe and summarize all variables in the data codebook, compact **************************************************************************** * Variance-components model **************************************************************************** * Reload the data use http://www.bristol.ac.uk/cmm/media/runmlwin/hedonism, clear * Fit the variance-components model by IGLS runmlwin hedonism cons, level2(country: cons) level1(individual: cons) * Refit the variance-components model by RIGLS and retrieve the level-2 * residuals runmlwin hedonism cons, /// level2(country: cons, residuals(u)) /// level1(individual: cons) /// rigls nogroup nopause * Calculate the VPC/ICC display 0.094/(0.094 + 0.885) * Calculate the VPC/ICC automatically and with full precision by referring * to the parameters' internal Stata names display [RP2]var(cons)/([RP2]var(cons) + [RP1]var(cons)) * Store the model results estimates store VC * Fit the single-level model runmlwin hedonism cons, /// level2(country:) /// level1(individual: cons) /// rigls noheader nopause * Store the model results estimates store SL * Perform an LR test of the single-level model vs. the multilevel model lrtest SL VC * Keep a subset of the variables: country, u0 and u0se keep country countrycode u0 u0se * Collapse the data down to one record per country duplicates drop * Assert that country uniquely identifies the records in the data isid country * Sort the country effects sort u0 * Rank the country effects generate u0rank = _n * Plot a caterpillar plot of the country effects serrbar u0 u0se u0rank, scale(1.96) yline(0) * Replot the caterpillar plot, adding country labels serrbar u0 u0se u0rank, scale(1.96) mvopts(mlabel(countrycode) /// mlabsize(*1) mlabposition(6) mlabgap(huge)) /// ytitle("Residual") yline(0) xtitle("Country (ranked)") **************************************************************************** * Random-intercept model **************************************************************************** * Reload the data use http://www.bristol.ac.uk/cmm/media/runmlwin/hedonism, clear * Centre age around 46 years of age generate age46 = age - 46 * Fit a random-intercept model where we enter age as a covariate runmlwin hedonism cons age46, /// level2(country: cons, residuals(u)) /// level1(individual: cons) /// rigls nogroup nopause * Store the model results estimates store RI display [FP1]cons - invnorm(0.975)*sqrt([RP2]var(cons)) display [FP1]cons + invnorm(0.975)*sqrt([RP2]var(cons)) * Predict the hedonism age relationship in the average country predict predxb * Add the country residuals onto the predictions for the average country line generate predxbu = predxb + u0 * Sort the data by country and then by age within each country sort country age46 * Plot the predicted country lines twoway (line predxbu age, connect(ascending)), /// ytitle("Predicted hedonism") xtitle("Age (in years)") xline(46) **************************************************************************** * Random-slope model **************************************************************************** * Reload the data use http://www.bristol.ac.uk/cmm/media/runmlwin/hedonism, clear * Centre age around 46 years of age generate age46 = age - 46 * Fit the random-slope model runmlwin hedonism cons age46, /// level2(country: cons age46, residuals(u)) /// level1(individual: cons) /// rigls noheader nopause * Store the model results estimates store RS * Perform a LR test of the random-intercept model vs. the random-slope model lrtest RI RS * Predict the hedonism age relationship in the average country predict predxb * Add the country residuals onto the predictions for the average country * line generate predxbu = predxb + u0 + u1*age46 * Sort the data by country and then by age within each country sort country age46 * Plot the predicted country lines twoway (line predxbu age, connect(ascending)), /// ytitle("Predicted hedonism") xtitle("Age (in years)") xline(46) * Display the intercept-slope correlation runmlwin, noheader correlations * Predict the level-2 variance function generate lev2var = [RP2]var(cons) /// + 2*[RP2]cov(cons\age46)*age46 + [RP2]var(age46)*age46^2 * Plot the level-2 variance function twoway (line lev2var age, sort), /// ytitle("Between-country variance") xline(46) * Displays a table of coefficients and statistics for all sets of estimation * results. estimates table SL VC RI RS, /// stats(deviance) b(%4.3f) stfmt(%6.0f) varwidth(15) **************************************************************************** exit