Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract the random effects design matrix in nlme

A linear mixed effects model is traditionally formulated in the following way. Ri = Xi × β + Zi × bi + εi where β represents estimated fixed effects and Z represents the random effects. X is thus the classical design matrix. Using R, I would like to be able to extract these two matrices after fitting a model using lme from the nlme package. For example, the dataset "Rails", also found in the nlme package, contains three separate measurements of ultrasonic travel time on 6 randomly selected railway rails. I can fit a simple model with an intercept fixed effect and a random effect for each rail with the following.

library(nlme)
lmemodel<-lme(travel ~ 1, random = ~ 1 | Rail, data=Rail)

The X design matrix is just an 18x1 matrix (6 rails * 3 measurements) of unity and is easily extracted in the following way:

 model.matrix(lmemodel, data=Rail)
   (Intercept)
1            1
2            1
3            1
4            1
5            1
6            1
7            1
8            1
9            1
10           1
11           1
12           1
13           1
14           1
15           1
16           1
17           1
18           1
attr(,"assign")
[1] 0

What I would like to do is extract the random effects design matrix Z. I realize if I fit the the same model using the lme4 package, this can be done in the following way:

library(lme4)
lmermodel<-lmer(travel ~ 1 + (1|Rail),data=Rail) 
t(lmermodel@Zt)  ##takes the transpose of lmermodel@Zt
lmermodel@X  ## extracts the X matrix

However, I am at a loss as to how to extract this matrix from an lme fitted model.

like image 696
iantist Avatar asked Oct 31 '13 16:10

iantist


People also ask

What is random effect in LMER?

The random effects: (1 + Time | Chick) which allows individual chicks to vary randomly in terms of their intercept (starting weight) and their effect of Time (weight change over time, also called a “random slope”, but I think that terminology can get confusing when fitting models with nonlinear predictors).

What is a random effect model in experimental design?

Random-effects models are statistical models in which some of the parameters (effects) that define systematic components of the model exhibit some form of random variation. Statistical models always describe variation in observed variables in terms of systematic and unsystematic components.

What does LMER do in R?

2.1. Like most model-fitting functions in R, lmer takes as its first two arguments a formula spec- ifying the model and the data with which to evaluate the formula. This second argument, data, is optional but recommended and is usually the name of an R data frame.

What does a random effects model show?

The Random Effects regression model is used to estimate the effect of individual-specific characteristics such as grit or acumen that are inherently unmeasurable. Such individual-specific effects are often encountered in panel data studies.


2 Answers

model.matrix(formula(lmemodel$modelStruct$reStr)[[1]],data=lmemodel$data)

the 1 is kinda specific to this example because there's only one random effect. When you have multiple random effects, you can do some more automatic programming to stack different Z_i's together.

like image 181
user3023981 Avatar answered Sep 30 '22 16:09

user3023981


As far as I can see the Z matrix isn't stored anywhere in an lme object. The best bet would be in the modelStruct$reStruct component (try names(modelfit); str(modelfit); sapply(modelfit,class) etc. to explore), but it isn't there as far as I can tell. In fact some poking into the guts of lme.default suggests that the Z matrix may never actually be explicitly constructed; internally lme seems to work with grouping structures instead. You can of course do

Z <- model.matrix(~Rail-1,data=Rail)

but that's probably not what you had in mind ...

like image 35
Ben Bolker Avatar answered Sep 30 '22 17:09

Ben Bolker