Computer exercise: random effects

advertisement
Computer Exercise on Linear Mixed Models
Estrone data
The first task is to fit a linear mixed model to the Estrone example (p 261 in Pawitan 2001).
The most commonly used package is lme4 which you will try first. Thereafter you can try the
hglm package, which gives more information and is more flexible.
#Load the data
estrone <- as.matrix(read.table(file="http://users.du.se/~lrn/StatMod13/LKPACK/random.dat"))
y <- as.numeric(10*log10(estrone))
#see definition of y on page 260 in Pawitan (2001)
female <- factor(rep(1:5, each=16)) #Define the female classes
print(female)
Fit the linear mixed model using lme4
library(lme4) #The most commonly used package for random effect models
lmer1 <- lmer(y ~ 1 + (1|female) )
print(lmer1)
Check that you get the same results using hglm. Note also that you get the estimates of the
random effects
library(hglm) #This package gives estimates of the random effects too
hglm1 <- hglm(fixed = y~1, random = ~1|female)
print(hglm1)
Compare the random effect estimates to Figure 9.7 (first panel) on page 262 in Pawitan
(2001).
Using the summary function we get standard errors for all the estimates
summary(hglm1)
What is the standard error for the intercept term? Can we get a 95% confidence interval for
the variance of the random effect?
What happens if we ignore that we have repeated observations?
#Compare the standard errors for the intercept term if we would ignore the random female effect
lm1 <- lm(y~1)
summary(lm1)
Simulated data
Here we look at how one can simulate data with random effects.
#Simulation part
set.seed(1234)
n.cluster <- 5 #Number of individauls
n.per.cluster <- 4 #Number of observations per individual
n <- n.cluster * n.per.cluster
sigma2_a <- 0.2
sigma2_e <- 1
mu <- 0
X <- matrix(1, n, 1)
id <- factor(rep(1:n.cluster, each=n.per.cluster))
Z <- model.matrix(~0 + id)
print(Z)
a <- rnorm(n.cluster, 0, sqrt(sigma2_u))
e <- rnorm(n, 0, sqrt(sigma2_e))
y <- mu + Z %*% a + e
We can fit these data using hglm
#Estimation part
simul1 <- hglm(fixed=y~1, random=~1|id)
summary(simul1)
A nice feature of the hglm function is that you can specify the model using the design
matrices
#Same model specifying the design matrices as input
simul1_matrix <- hglm(y = y, X = X, Z = Z)
summary(simul1_matrix)
You can also fit data for any distribution in the GLM family
################
#Simulate Poisson data
eta <- exp(mu + Z %*% a)
y <- rpois(length(eta), eta)
simul.pois <- hglm(y = y, X = X, Z = Z, family = poisson(link = log), fix.disp=1)
summary(simul.pois)
If you have finished by now, have a look at the R Journal paper from 2010 at:
http://journal.r-project.org/archive/2010-2/RJournal_2010-2_Roennegaard~et~al.pdf
There you find more examples.
If you want to find out more, look at the hglm documentation in R:
http://cran.r-project.org/web/packages/hglm/index.html
or the YouTube tutorials at:
http://www.youtube.com/playlist?list=PLn1OmZECD-n15vnYzvJDy5GxjNpVV5Jr8
Download