Random Effects in R Bret Larget Departments of Botany and of Statistics University of Wisconsin Madison March 15 2007 Statistics 572 Spring 2007 Random Effects in R The Big Picture March 17 2007 1 13 Random Effects and Balance The Big Picture The completely randomized design with a random effect assumes the following model yij ai eij j 1 ni i 1 k where ai iidN 0 a2 and eij iidN 0 e2 with ai independent of eij If the data is perfectly balanced with equal numbers of individuals in each group ni n for all i it is possible to make modifications to a fixed effects ANOVA analysis by computing a different expected mean square EMS for inference When the data is not balanced this approach fails sometimes approximations work sometimes not An alternative regression approach is appropriate even when the design is imbalanced but the scope of inference is then different I will first show the regression approach described in Chapter 10 of your book Statistics 572 Spring 2007 Random Effects in R March 17 2007 2 13 The Big Picture Random Effects and Balance Correlation One consequence of the random effects model is that observations can be correlated In the model we study here observations in different groups are independent just as in the fixed effect model because they do not share any random variables For example the first observations from groups one y11 depends a1 and e11 while y21 depends on a2 and e21 In contrast observations in the same group are correlated y11 and y12 each depend on a1 Statistics 572 Spring 2007 Random Effects in R The Big Picture March 17 2007 3 13 Random Effects and Balance Variance Recall from Statistics 571 the following facts about variances of independent random variables X and Y and constant c 1 2 V X Y V X Y V X V Y if independent V cX c 2 V X With this reminder we can recognize that a measures the size of a typical difference between a random effect and the grand mean A effects is ptypical difference p between two regression V ai aj V ai V aj 2 a Statistics 572 Spring 2007 Random Effects in R March 17 2007 4 13 Computation lmer Linear Mixed Effects Models using lmer The most recently developed R package for fitting linear models with random effects is in the library lme4 The function to use instead of lm is named lmer Mac users will need a recent operating system OS X 10 4 4 or later and R 2 4 1 or later to use this library Users of older Macs can use the older package nlme and the function lme lme cannot fit as rich a class of random effects models as lmer for example random effects cannot be nested and you cannot use generalized linear models but it will suffice for much of what we do in the course A model formula with a random effect in lmer differs from lm by including a term of the form a b where a is a model matrix often the intercept 1 for the scope of the random effect and b is the group to which the random effect applies Statistics 572 Spring 2007 Random Effects in R Computation March 17 2007 5 13 lmer Data Description We will consider the data set ant111b from the textbook This data set is a subset of a much larger data set on corn yields on different islands in the Carribean In the subset of data two possibly interesting response variables are harvest weight harvwt and the number of ears ears Both harvwt and ears are measured by plot but the units are unspecified There are four plots within each site The variable plot is only meaningful nested within a site We will consider the site as a random effect as there are many possible sites on each island In the subset of the data we have there is only a single treatment and all sites are on the same island Statistics 572 Spring 2007 Random Effects in R March 17 2007 6 13 Computation lmer Data library DAAG str ant111b data frame 32 obs of 9 variables site Factor w 8 levels DBAN LFAN 1 2 3 4 5 6 7 8 1 2 parcel Factor w 4 levels I II III 1 1 1 1 1 1 1 1 2 2 code num 58 58 58 58 58 58 58 58 58 58 island num 1 1 1 1 1 1 1 1 1 1 id num 3 40 186 256 220 plot num 3 4 5 5 4 5 3 5 5 7 7 15 5 15 trt num 111 111 111 111 111 111 111 111 111 111 ears num 43 5 40 5 20 42 5 31 5 32 5 43 5 50 46 46 5 harvwt num 5 16 2 93 1 73 6 79 3 25 Statistics 572 Spring 2007 Random Effects in R Computation March 17 2007 7 13 lmer Plot of Data 7 attach ant111b library lattice fig1 xyplot harvwt site pch 16 col blue print fig1 4 Variation within sites from plot to plot appears smaller 5 Notice that there is considerable variation among sites 6 harvwt 3 2 DBAN LFAN NSAN ORAN OVAN TEAN WEAN WLAN site Statistics 572 Spring 2007 Random Effects in R March 17 2007 8 13 Computation lmer Fitting a Random Effects Model The first 1 is the fixed effect library lme4 corn1 lmer lmer harvwt 1 1 site summary corn1 lmer The term 1 site means that there is a random effect for each site and this effect is nested within the intercept the whole model Linear mixed effects model fit by REML Formula harvwt 1 1 site AIC BIC logLik MLdeviance REMLdeviance 98 42 101 3 47 21 95 08 94 42 Random effects Groups Name Variance Std Dev site Intercept 2 36773 1 53874 Residual 0 57754 0 75996 number of obs 32 groups site 8 There are two sources of random variation one for site and one for parcel within site Fixed effects Estimate Std Error t value Intercept 4 2917 0 5604 7 659 The estimated variance components are a2 2 36773 and e2 0 57754 sig2site as vector VarCorr corn1 lmer site sigma attributes summary corn1 lmer sigma Statistics 572 Spring 2007 Random Effects in R Computation March 17 2007 9 13 lmer 1 0 1 5 Residual Plot 0 5 0 0 0 5 There are no bad patterns in the residual plot 1 0 plot fitted corn1 lmer residuals corn1 lmer abline h 0 residuals corn1 lmer 1 5 3 4 5 6 fitted corn1 lmer Statistics 572 Spring 2007 Random Effects in R March 17 2007 10 13 Computation lmer Fitted Values means sapply split harvwt site mean siteFit sapply split fitted corn1 lmer site mean data frame mean means fitted siteFit DBAN LFAN NSAN ORAN OVAN TEAN WEAN WLAN mean 4 88500 4 20750 2 09000 6 91500 4 83250 3 03625 5 52625 2 84125 Notice that the fitted values are not just the …
View Full Document