introduction to mixed models in r - school of statistics...

48
Introduction to Mixed Models in R Galin Jones School of Statistics University of Minnesota http://www.stat.umn.edu/galin March 2011

Upload: lyque

Post on 28-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Introduction to Mixed Models in R

Galin Jones

School of StatisticsUniversity of Minnesota

http://www.stat.umn.edu/∼galin

March 2011

Page 2: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Second in a Series

• Sponsored by Quantitative Methods Collaborative.

• Previous workshop “An Introduction to R” was given on 3/7by Professor Sanford Weisberg.

• Upcoming talk on 4/11 by Chris Winship of HarvardUniversity on causal inference.

Page 3: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Goals

• Brief review of first workshop.

• Definition of mixed models and why they may be useful.

• Using mixed models in R through two simple case studies.

Page 4: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Review

• R is “free” software and can be downloaded athttp://cran.r-project.org

• A package is a collection of functions designed for specifictasks. For example the package lme4 fits many mixed models.

• When you install R on your computer you get the “base”distribution. This includes many useful packages but others,such as lme4, you need to install separately.

• The R search engine is extremely usefulhttp://www.rseek.org

• Getting data into R (or any other software package) can bechallenging.

• Many R packages include built-in data sets and we will usetwo of these today.

Page 5: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Mixed Models

Mixed models are a large and complex topic, we will only justbarely get started with them today.

There are many varieties of mixed models:

• Linear mixed models (LMM)

• Nonlinear mixed models (NLM)

• Generalized linear mixed models (GLMM)

Our focus will be on linear mixed models. Much more discussion ofthis material can be found in the following books.

Extending the Linear Model with R by Julian Faraway

Mixed-Effects Models in S and S-PLUS by Jose Pinheiro andDouglas Bates

Page 6: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Factors, Effects and Treatments

Suppose apple slices are treated with five preservative compounds(A, B, C, D, E) with the goal of extending shelf life.

Response: Shelf life

Factor: Preservative

Treatments = Levels of a factor: A, B, C, D, E

Effect: Impact of compound on shelf life

µ=population mean shelf lifeµA= population mean shelf life with treatment A

effect of A = µA − µ

Page 7: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Fixed or Random?

Factor effects are either fixed or random.

• Fixed: The levels in the study represent all levels of interest

• Random: The levels in the study represent only a sample ofthe levels of interest.

Mixed models have both fixed and random effects.

In our example, preservative is fixed since A, B, C, D, E are theonly levels of interest.

Page 8: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Blocking

Block:

• A group of units formed so that units within the group are ashomogeneous as possible.

• Reduces the effects of variation among experimental units.

• Treatmets are randomly assigned to blocks.

Lets add to the example: Suppose 10 individual fruit are randomlychosen from a population of fruit and the 5 preservatives arerandomly assigned to 5 portions of fruit.

• Preservative is a fixed effect.

• Fruit is a (random) block effect.

Page 9: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Mixed models

Mixed models contain both fixed and random effects This hasseveral ramifications:

• Using random effects broadens the scope of inference. That is,inferences can be made on a statistical basis to the populationfrom which the levels of the random factor have been drawn.

• Naturally incorporates dependence in the model. Observationsthat share the same level of the random effects are beingmodeled as correlated.

• Using random factors often gives more accurate estimates.

• Sophisticated estimation and fitting methods must be used.

Page 10: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

The “Rail” data is a built-in R data set. It can be found in theMEMSS and the NLME packages.

The commands

> library(nlme)

> data(Rail)

> ?Rail

will make the data available to us and produce a description.

Page 11: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

Evaluation of Stress in Railway Rails

Description

The Rail data frame has 18 rows and 2 columns.

Format

This data frame contains the following columns:

Rail

an ordered factor identifying the rail on which

the measurement was made.

travel

a numeric vector giving the travel time for ultrasonic

head-waves in the rail (nanoseconds). The value given

is the original travel time minus 36,100 nanoseconds.

Page 12: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

Summary

• Six rails were chosen from a group of rails.

• Each rail was tested 3 times.

• Measured the time it takes an ultrasonic wave to travel thelength of a rail.

• Rail is a factor (fixed or random?) and travel is the response.

Page 13: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> Rail

Grouped Data: travel ~ 1 | Rail

Rail travel

1 1 55

2 1 53

3 1 54

4 2 26

5 2 37

.

.

.

15 5 50

16 6 80

17 6 85

18 6 83

Page 14: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> summary(Rail)

Rail travel

2:3 Min. : 26.00

5:3 1st Qu.: 50.25

1:3 Median : 66.50

6:3 Mean : 66.50

3:3 3rd Qu.: 85.00

4:3 Max. :100.00

> with(Rail, tapply(travel, Rail, mean))

2 5 1 6 3 4

31.66667 50.00000 54.00000 82.66667 84.66667 96.00000

> pdf(file="RailPlot1.pdf")

> with(Rail, plot(travel, Rail, xlab="Travel time"))

> dev.off()

Page 15: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

●●●

● ●●

● ●●

● ●●

● ●●

● ●●

40 60 80 100

12

34

56

Travel time

Rail

Page 16: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

• Between-rail variability is greater than within-rail variability.

• Within-rail variability is not constant.

• Mean travel time appears different for some rails2 5 1 6 3 4

We clearly need to account for the classification factor (Rail) in theanalysis.

Page 17: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail DataRail as a fixed effect

yij = βi + eij i = 1, . . . , 6 j = 1, 2, 3

where

• yij is the observed travel time for observation j on rail i .

• βi is the population mean travel time of rail i

• eij are independent and identically normally distributed withmean 0 and variance σ2 or

eijiid∼ N(0, σ2) .

This asumption means yijind∼ N(βi , σ

2)

> #Rail as a fixed effect

> r1.lm<-lm(travel ~ Rail - 1, data=Rail)

Page 18: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> r1.lm

Call:

lm(formula = travel ~ Rail - 1, data = Rail)

Coefficients:

Rail2 Rail5 Rail1 Rail6 Rail3 Rail4

31.67 50.00 54.00 82.67 84.67 96.00

This means that the oredered estimates of the βi are

β2 = 31.67 , . . . , β4 = 96.00

Page 19: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> summary(r1.lm)

Coefficients:

Estimate Std. Error t value Pr(>|t|)

Rail2 31.667 2.321 13.64 1.15e-08 ***

.

.

Rail4 96.000 2.321 41.35 2.59e-14 ***

Residual standard error: 4.021 on 12 degrees of freedom

Multiple R-squared: 0.9978,Adjusted R-squared: 0.9967

F-statistic: 916.6 on 6 and 12 DF, p-value: 2.971e-15

Note that the F-statistic and p-value are testing for any differencesbetween the rail effects. Also, the estimate of σ2 is 4.021 on 12degrees of freedom.

Page 20: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> with(Rail, bwplot(Rail ~ residuals(r1.lm)))

residuals(r1.lm)

2

5

1

6

3

4

−5 0 5

Page 21: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

The fixed effects model gives a good summary of the data but themain interest is in the population of rails. Also, we don’t believethe model is accurate since the 3 observations on each rail areclearly not independent.

Rail as a random effect

yij = β + bi + eij

where

• yij is the observed travel time for observation j on rail i .

• β is the population mean travel time

• bi is the deviation from β for the ith rail

Page 22: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

• eij is the deviation for observation j on rail i from the meantravel time for rail i

biiid∼ N(0, σ2b) eij

iid∼ N(0, σ2) .

• Our assumptions imply that observations on the same rail arecorrelated, in fact,

corr =σ2b

σ2b + σ2.

Page 23: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> #Rail as random effect

> r2.lme<-lmer(travel ~ 1 + (1 | Rail),

REML=FALSE, data=Rail)

This notation takes some getting used to. Specifically,

(1 | Rail)

means that there is a single random factor which is constant withineach level and its levels are given by the grouping variable Rail.

Page 24: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> summary(r2.lme)

Linear mixed model fit by maximum likelihood

Formula: travel ~ 1 + (1 | Rail)

Random effects:

Groups Name Variance Std.Dev.

Rail (Intercept) 511.861 22.6243

Residual 16.167 4.0208

Number of obs: 18, groups: Rail, 6

Our best guess at σ2 and σ2b are

σ2b = 511.861 σ2 = 16.167

and the estimated correlation between observations on a rail is

corr =511.861

511.861 + 16.167= 0.969 .

Page 25: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

Fixed effects:

Estimate Std. Error t value

(Intercept) 66.500 9.285 7.162

The estimate of β is β = 66.5. For a new rail drawn for thepopulation this is our best guess at travel time while the predictedvalues for a new observation on each of the same 6 rails are

> fixef(r2.lme)+ranef(r2.lme)$Rail

(Intercept)

2 32.02957

5 50.17190

1 54.13023

6 82.49824

3 84.47740

4 95.69266

Page 26: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> qqnorm(resid(r2.lme), main="")

−2 −1 0 1 2

−6−4

−20

24

6

Theoretical Quantiles

Sam

ple Q

uant

iles

Page 27: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Rail Data

> plot(fitted(r2.lme), resid(r2.lme), xlab="Fitted",

ylab="Residuals")

30 40 50 60 70 80 90

−6−4

−20

24

6

Fitted

Resid

uals

Page 28: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Multilevel Models

Multilevel models

• are special cases of mixed models,

• are useful for data with a hierarchical structure, and

• can be implemented in a variety of R packages.

Page 29: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

> library(faraway)

> data(jsp)

> ?jsp

Description

Example Dataset from "Practical Regression and Anova"

Format

See for yourself

Source

See Reference

References

Reference details may be found in "Practical Regression

and Anova" by Julian Faraway

Page 30: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project> str(jsp)

’data.frame’: 3236 obs. of 9 variables:

$ school : Factor w/ 49 levels "1","2","3","4",..: 1 1

1 1 1 1 1 1 1 1 ...

$ class : Factor w/ 4 levels "1","2","3","4": 1 1 1 1

1 1 1 1 1 1 ...

$ gender : Factor w/ 2 levels "boy","girl": 2 2 2 1 1

1 1 1 1 1 ...

$ social : Factor w/ 9 levels "1","2","3","4",..: 9 9

9 2 2 2 2 2 9 9 ...

$ raven : num 23 23 23 15 15 22 22 22 14 14 ...

$ id : Factor w/ 1192 levels "1","2","3","4",..: 1

1 1 2 2 3 3 3 4 4 ...

$ english: num 72 80 39 7 17 88 89 83 12 25 ...

$ math : num 23 24 23 14 11 36 32 39 24 26 ...

$ year : num 0 1 2 0 1 0 1 2 0 1 ...

Page 31: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

> summary(jsp)

school class gender social raven

48 : 206 1:1949 boy :1551 4 :1225 Min. : 4.00

33 : 131 2: 987 girl:1685 9 : 484 1st Qu.:21.00

4 : 131 3: 169 2 : 424 Median :25.00

31 : 107 4: 131 5 : 288 Mean :25.13

47 : 102 3 : 270 3rd Qu.:29.00

50 : 101 6 : 221 Max. :36.00

(Other):2458 (Other): 324

id english math year

1 : 3 Min. : 0.00 Min. : 1.00 Min. :0.0000

3 : 3 1st Qu.:31.00 1st Qu.:22.00 1st Qu.:0.0000

4 : 3 Median :54.00 Median :28.00 Median :1.0000

6 : 3 Mean :52.49 Mean :26.66 Mean :0.9379

7 : 3 3rd Qu.:75.00 3rd Qu.:33.00 3rd Qu.:2.0000

8 : 3 Max. :98.00 Max. :40.00 Max. :2.0000

(Other):3218

Page 32: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

#Subset data to focus on Year=2

> jsp.year2<-jsp[jsp$year==2,]

> plot(jitter(math) ~ jitter(raven), xlab="Raven Score",

ylab="Math Score", data=jsp.year2)

> boxplot(math ~ social, xlab="Social Class",

ylab="Math Score", data=jsp.year2)

> boxplot(math ~ gender, xlab="Gender", ylab="Math Score",

data=jsp.year2)

Page 33: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●

●● ●

●●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

● ●

●●

● ●

●●

●●

● ●

● ●

●●

● ●●

●●

●●

●●

●●

●●

●●

● ● ●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ● ●

●●

●●

●●

● ●

● ●●

● ●

●●●

●●●

● ●●

● ●

●●

●●

●●

●●

●●

5 10 15 20 25 30 35

510

1520

2530

3540

Raven Score

Mat

h Sc

ore

There is clearly correlation between math score and raven score.

Page 34: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●●

1 2 3 4 5 6 7 8 9

510

1520

2530

3540

Social Class

Mat

h Sc

ore

There are differences in math scores between the levels of socialclass.

Page 35: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●●

boy girl

510

1520

2530

3540

Gender

Mat

h Sc

ore

Math scores are not different between the levels of gender.

Page 36: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

Center raven since we would otherwise be comparing to zero.

> jsp.y2$ctrraven<-jsp.y2$raven-mean(jsp.y2$raven)

> jsp1.lme<-lmer(math ~ ctrraven*social*gender +

(1 | school) + (1 | school:class), data=jsp.y2)

> qqnorm(resid(jsp1.lme),main="")

> plot(fitted(jsp1.lme) ~ resid(jsp1.lme), xlab="Fitted",

ylab="Residuals")

Page 37: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●

−3 −2 −1 0 1 2 3

−20

−15

−10

−50

510

15

Theoretical Quantiles

Sam

ple Q

uant

iles

There isn’t much of concern here.

Page 38: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

● ●

●●●

●●

●●

●●●

●●

● ●

●●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

● ●

●●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●

●●

● ●●

● ●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

−20 −15 −10 −5 0 5 10 15

1015

2025

3035

40

Fitted

Resid

uals

There is some evidence of non-constant variance. Transformation?

Page 39: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

> anova(jsp1.lme)

Analysis of Variance Table

Df Sum Sq Mean Sq F value

ctrraven 1 10218.0 10218.0 374.4037

social 8 615.7 77.0 2.8201

gender 1 21.6 21.6 0.7917

ctrraven:social 8 577.4 72.2 2.6445

ctrraven:gender 1 2.5 2.5 0.0905

social:gender 8 275.2 34.4 1.2605

ctrraven:social:gender 8 187.2 23.4 0.8573

Page 40: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

The p-values associated with the F -statistics are approximate andcan be too small when the number of cases is small. However, thisdata set is probably large enough to overcome this limitation. Theparametric bootstrap can be used if this is a concern.

> nrow(jsp.y2) - sum(anova(jsp1.lme)[,1]) - 1

[1] 917

> round(pf(anova(jsp1.lme)[,4], anova(jsp1.lme)[,1], 917,

lower.tail=FALSE),4)

[1] 0.0000 0.0043 0.3738 0.0072 0.7636 0.2607 0.5524

Page 41: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

> #Remove Gender

> jsp2.lme<-lmer(math ~ ctrraven*social + (1 | school) +

(1 | school:class), data=jsp.y2)

> qqnorm(resid(jsp2.lme),main="")

> plot(fitted(jsp2.lme) ~ resid(jsp2.lme), xlab="Fitted",

ylab="Residuals")

> qqnorm(ranef(jsp2.lme)$"school:class"[[1]],

main="School Effects")

> qqnorm(ranef(jsp2.lme)$"school:class"[[1]],

main="Class Effects")

Page 42: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

−3 −2 −1 0 1 2 3

−20

−15

−10

−50

510

15

Theoretical Quantiles

Sam

ple Q

uant

iles

Still not much of concern.

Page 43: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●

●●

●●

● ●

●●

● ●●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●

● ●

● ●

●●

●●

●●

● ●

●●

● ● ●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●●●

● ●

●●

●●

●●

● ●

●●●

● ●

●●

●●

● ●

●●

−20 −15 −10 −5 0 5 10 15

1520

2530

3540

Fitted

Resid

uals

Constant variance is still a problem.

Page 44: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●

●●

●●

−2 −1 0 1 2

−1.5

−1.0

−0.5

0.0

0.5

1.0

School Effects

Theoretical Quantiles

Sam

ple Q

uant

iles

Page 45: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

●●

●●

●●

−2 −1 0 1 2

−1.5

−1.0

−0.5

0.0

0.5

1.0

Class Effects

Theoretical Quantiles

Sam

ple Q

uant

iles

Page 46: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

> anova(jsp2.lme)

Analysis of Variance Table

Df Sum Sq Mean Sq F value

ctrraven 1 10159.7 10159.7 374.3263

social 8 609.5 76.2 2.8073

ctrraven:social 8 564.6 70.6 2.6005

> nrow(jsp.y2) - sum(anova(jsp2.lme)[,1]) - 1

[1] 935

> round(pf(anova(jsp2.lme)[,4], anova(jsp2.lme)[,1], 935,

lower.tail=FALSE),4)

[1] 0.0000 0.0045 0.0082

Page 47: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

> sch.effects<-ranef(jsp2.lme)$school[[1]]

> summary(sch.effects)

Min. 1st Qu. Median Mean 3rd Qu. Max.

-2.45200 -0.69480 0.01107 0.00000 0.71840 2.44900

> raw.sch.effects<-coef(lm(math ~ school-1,jsp.y2))

> raw.sch.effects<-raw.sch.effects-mean(raw.sch.effects)

> plot(raw.sch.effects,sch.effects)

> sint<-c(9,14,29)

> text(raw.sch.effects[sint],sch.effects[sint]+0.2,

c("9","15","30"))

Page 48: Introduction to Mixed Models in R - School of Statistics ...users.stat.umn.edu/~galin/IntroductionToMixedModelsInR.pdf · Goals Brief review of rst workshop. De nition of mixed models

Joint Schools Project

●●

● ●

−6 −4 −2 0 2 4

−2−1

01

2

raw.sch.effects

sch.

effec

ts

9

15

30