October 6, 2009 Session 6Slide 1 PSC 5940: Running Basic Multi- Level Models in R Session 6 Fall, 2009
October 6, 2009 Session 6Slide 2 Running Multilevel Models in R Using lmer: “linear mixed-effects in R” Identify a grouping variable: “state” levels(state) # will show the categories: > levels(state) [1] "AK" "AL" "AR" "AZ" "CA" "CO" "CT" "DC" "DE" "FL" "GA" [12] "HI" "IA" "ID" "IL" "IN" "KS" "KY" "LA" "MA" "MD" "ME" [23] "MI" "MN" "MO" "MS" "MT" "NC" "ND" "NE" "NH" "NJ" "NM" [34] "NV" "NY" "OH" "OK" "OR" "PA" "RI" "SC" "SD" "TN" "TX" [45] "UT" "VA" "VT" "WA" "WI" "WV" "WY” Texas is element #44; Oklahoma is element #37; etc.
October 6, 2009 Session 6Slide 3 Running Multilevel Models in R Re-name some variables for analysis income<-e130e_co educ<-e2b_edu Run a simple linear model for comparison: OLS1<-lm(income ~ educ) lm(formula = income ~ educ) Residuals: Min 1Q Median 3Q Max Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) e-13 *** educ < 2e-16 *** --- Signif. codes: 0 ‘***’ ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: on 1506 degrees of freedom (190 observations deleted due to missingness) Multiple R-squared: ,Adjusted R-squared: F-statistic: on 1 and 1506 DF, p-value: < 2.2e-16
October 6, 2009 Session 6Slide 4 Running Multilevel Models in R For a simple-minded intercept-varying model (with no slope coefficients): ML1<-lmer(income ~ 1 + (1 | state)) Formula: income ~ 1 + (1 | state) AIC BIC logLik deviance REMLdev Random effects: Groups Name Variance Std.Dev. state (Intercept) Residual Number of obs: 1513, groups: state, 51 Fixed effects: Estimate Std. Error t value (Intercept)
October 6, 2009 Session 6Slide 5 Running Multilevel Models in R To see the fixed effect: fixef(ML1) Returns the average intercept: ranef(ML1) Returns the variation for each state around the mean intercept: $state (Intercept) AK AL AR AZ CA CO (etc.)
October 6, 2009 Session 6Slide 6 Running Multilevel Models in R A somewhat more interesting ML model: ML2<-lmer(income ~ educ + (1 | state)) Returns a model with a fixed slope and varying intercepts. Summary gets you this: Formula: income ~ educ + (1 | state) AIC BIC logLik deviance REMLdev Random effects: Groups Name Variance Std.Dev. state (Intercept) Residual Number of obs: 1508, groups: state, 51 Fixed effects: Estimate Std. Error t value (Intercept) educ
October 6, 2009 Session 6Slide 7 Running Multilevel Models in R To observe the model estimates: fixef(ML2): (Intercept) educ ranef(ML2): Calculation of the intercept for Texas (46 th state): coef(ML2)$state[46,1], returns: [1] $state (Intercept) AK e-02 AL e-01 AR e-01 AZ e-01 CA e-01 CO e-02 CT e-01
October 6, 2009 Session 6Slide 8 Running Multilevel Models in R To calculate the 95% confidence interval for Texas: coef(ML2)$state[46,1]+c(-2,2)*se.ranef(ML2)$state[46][1] The 95% confidence interval for the model slope is: fixef(ML2)["educ"]+c(-2,2)*se.fixef(ML2)["educ"] which returns: [1]
October 6, 2009 Session 6Slide 9 Running Multilevel Models in R A still more interesting ML model: ML2<-lmer(income ~ educ + (1 + educ | state)) Returns a model with both a varying slope and intercept for each state. Summary gets you this: Formula: income ~ educ + (1 + educ | state) AIC BIC logLik deviance REMLdev Random effects: Groups Name Variance Std.Dev. Corr state (Intercept) educ Residual Number of obs: 1508, groups: state, 51 Fixed effects: Estimate Std. Error t value (Intercept) educ
October 6, 2009 Session 6Slide 10 Running Multilevel Models in R To observe the model estimates: fixef(ML3): (Intercept) educ ranef(ML3): Calculation of the intercept and slopes for Texas: coef(ML3)$state[46,1], returns: [1] coef(ML3)$state[46,2], returns: [2] $state (Intercept) educ AK AL AR AZ CA CO CT
October 6, 2009 Session 6Slide 11 Workshop 1: Build ML Model using Ideology to Predict GHG Risk Use the state variable as the group level How much is the model residual reduced by allowing states to vary? Present it to me in 20 min.
October 6, 2009 Session 6Slide 12 BREAK
October 6, 2009 Session 6Slide 13 Workshop 2: Data presentations Sources, characteristics Preliminary group-level models?
October 6, 2009 Session 6Slide 14 For Next Week Read Gelman & Hill Ch. 13 Build plots: Figure out how to replicate Figure 12.4 (p. 257) code is shown on p Present your initial group-level models