/********************* * Code from Day 2 * *********************/ /** HOMEWORK 1 ANSWERS **/ /* question 1: long to wide */ /* generate mean score variable using egen */ egen meanScore = mean(Score), by(Country Party Dimension Scale) use PPMD_detail.dta, clear /* collapse on the mean Score */ use PPMD_detail.dta, clear collapse (mean) Score, by(Country Party Dimension Scale) /* reshape on Dimension */ use http://www.kenbenoit.net/courses/mlm/PPMD_summary_day1.dta, clear reshape wide Mean, i(Country Party Scale) j(Dimension) /* reshape on Scale */ use http://www.kenbenoit.net/courses/mlm/PPMD_summary_day1.dta, clear reshape wide Mean, i(Country Party Dimension) j(Scale) rename Mean1 Position rename Mean2 Importance graph twoway (qfitci Importance Position) (scatter Importance Position, msize(tiny)), xtitle(Position) ytitle(Importance) scheme(s1color) legend(off) /* question 2: wide to long */ use http://www.stata-press.com/data/mlmus2/pefr, clear reshape long wm wp, i(id) j(occasion) reshape long w, i(id occasion) j(instrument) string rename w score /** FROM CLASS **/ /** HSB dataset example **/ use http://www.stata-press.com/data/mlmus2/hsb.dta, clear summ tab sector label define sector 0 public 1 private label values sector sector reg mathach ses sector gen sesXsector = ses*sector reg mathach ses sector ses*sector reg mathach ses sector, vce(cluster schoolid) /** Wright peak flow meter example from R&S Chapter 2 **/ use http://www.stata-press.com/data/mlmus2/pefr, clear list if id<6, clean noobs reshape long wp wm, i(id) j(occasion) list if id<6, clean noobs /* using xtreg */ xtreg wm, i(id) mle xtreg wm, i(id) fe /* using xtmixed */ xtmixed wm || id:, mle /** HSB dataset example **/ use http://www.stata-press.com/data/mlmus2/hsb.dta, clear xtreg mathach, i(schoolid) mle nolog xtmixed mathach || schoolid:, mle nolog xtreg mathach, i(schoolid) fe