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.
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).
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.
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.
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.
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.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With