Reputation: 166
I have a model such as:
mymod = lmer(y ~ x1 + x2 + (x1 | id) , data = mydata)
I know I can get the model matrix from the fitted object using getME
but is there a way to obtain the model matrix for the fixed effects without first fitting the model:
Upvotes: 1
Views: 1017
Reputation: 6812
You can do this using the lformula
function in the lme4 package. This rerurns an object which has holds the transpose of this matrix, Zt
:
library(lme4)
# create some toy data
dt <- expand.grid(x1 = 1:4, x2 = 5:6, id = LETTERS[1:20], reps = 1:2)
# this is the model in the OP:
myFormula = "y ~ x1 + x2 + (x1 | id)"
# for lFormula to work we need y in the data frame
# so just put a vector of 1s since that will not affect the random effects model matrix:
dt$y <- 1
Then:
foo <- lFormula(eval(myFormula), dt)
Z <- t(as.matrix(foo$reTrms$Zt))
where Z
is the model matrix for the random effects that you requested.
Upvotes: 2