* ============================================================================================== * * Multilevel workshop: Stata examples * Benjamin Rosche - benrosche.com * Spring 2022 * Examples adapted from Germán Rodríguez (Princeton) * https://data.princeton.edu/pop510/ * * The dataset is freely available from: * https://www.stats.ox.ac.uk/~snijders/mlbook.htm * ============================================================================================== * * ============================================================================================== * * Create data and variables * ============================================================================================== * * Snijders & Bosker (1999) dataset use "https://data.princeton.edu/pop510/snijders", clear * Outcome egen gpa = std(langpost) * Explanatory variables regen ses = std(ses), replace regen schoolses = std(schoolses), replace * Save data for R saveold snijders.dta, v(12) replace * ============================================================================================== * * Varying-intercept model * ============================================================================================== * * Intercept-only model mixed gpa || schoolnr: * Model including covariates mixed gpa schoolses || schoolnr:, mle mixed gpa ses || schoolnr:, mle mixed gpa ses schoolses || schoolnr:, mle // in slides * Plot the varying intercepts sort schoolnr ses predict yhat1, fitted line yhat1 ses, connect(L) ytitle(gpa) graph export stata-varyingintercepts.png, width(500) replace * ============================================================================================== * * Varying intercept + slope model * ============================================================================================== * * Model with varying slope for ses mixed gpa c.ses c.schoolses || schoolnr: ses, mle covariance(unstructured) * Plot the varying slopes sort schoolnr ses predict yhat2, fitted line yhat2 ses, connect(L) graph export stata-varyingslopes.png, width(500) replace * ============================================================================================== * * Explaining varying intercept + slope model * ============================================================================================== * mixed gpa c.ses c.schoolses c.ses#c.schoolses || schoolnr: ses, mle covariance(unstructured) * eof