If I fit an lmer()
model with correlated fixed effects, by default it gives me a fit warning fixed-effect model matrix is rank deficient
and returns the model with one or more parameters dropped.
For example:
set.seed(1030)
d = data.frame(y=rnorm(100), x1=c('a', 'b'), x2=c('c', 'd'), k=1:5)
lmer(y ~ x1 + x2 + (1 + x1 + x2 | k), d)
(plus convergence warnings etc)
This fits a model with one fixed-effect parameter dropped. Is it possible to also force it to drop the slopes that correspond to the dropped column(s)?
I found instructions on how to identify the bad columns manually, but my situation is this: I have an interaction of multilevel factors, where some combinations of factor levels never co-occur in the data. The factors are sum-coded, and I'd like to just fit the maximal model without hand-coding new columns for each contrast parameter, because I'm using lsmeans
to calculate predicted marginal means. If I can keep the original factor coding then I can get also get an easily-interpretable output from lsmeans
.
Edit: an example like my data
This is what I'm doing now:
set.seed(1030) # level 3 of 'a' only occurs with level 3 of 'b' x = expand.grid(a=1:3, b=1:3) x = subset(x, !(a == 3 & b != 3)) d = data.frame(y=rnorm(700), a=factor(x$a), b=factor(x$b), k=factor(1:5)) library(lme4) library(lsmeans) # tmp for speed lsm.options(disable.pbkrtest=TRUE) # this model includes random slopes for factor level combinations that don't exist m1 = lmer(y ~ a * b + (1 + a * b | k), data=d) lsmeans(m1, ~ a | b) # and then pairs() for comparisons
Is this (below) what you're suggesting, or is this not quite right?
# this model has a different random parameterization, but # doesn't include slopes for non-existent combinations m2 = lmer(y ~ a * b + (1 + droplevels(interaction(a, b)) | k), data=d) lsmeans(m2, ~ a | b) # produces the same results as above
lsmeans
is giving me NAs exactly where I expect it to for the non-estimable cells & contrasts. I just mean that I want to be able to uselsmeans(model, ~ factor)
, wherefactor
is a 3-level sum-coded factor that bothlmer
andlsmeans
know how to deal with, rather thanlsmeans(model, ~ factor_1 + factor_2)
, wherefactor_1
is a column coded as 1 for level 1, 0 for level 2, and -1 for level 3; andfactor_2
is a column coded as 0 for level 1, 1 for level 2, and -1 for level 3. $\endgroup$lsmeans
won't know which combinations of values correspond to which factor levels, and I'll have to interpret that myself. $\endgroup$interaction(factor1, factor2)
? Or am I missing something? $\endgroup$