Skip to content

lmer code for multiple random slopes

3 messages · Peter R Law, Phillip Alday, Ben Bolker

#
I am trying to fit a model with two covariates, x and z say, for response y, with a random factor g and want each of x and y to have a random slope. I expected

lmer(y ~ x + z + (x+z|g),...)

to fit a model with 6 random variance components, the intercept, two slopes and three correlations. But I got an error message saying there were 74 random variance components and my data was insufficient to fit the model. Yet

lmer(y ~ x + z + (x+z||g),...)

returned what I expected, a model with the random intercept and two slopes but no correlations. How is lmer interpreting the first line of code above and how I would code for what I want. I have not been able to find any examples in the literature or online that help me but I may have easily missed something so if anyone knows of a useful link that'd be great. The only examples of multiple random slopes I've seen take the form

lmer(y~x + z +(x|g) + (z|g),...)

specifically excluding correlations between the random slopes and intercept of the two predictors. Even if the latter is a more sensible approach I'd like to understand the coding issue.

Thanks.

Peter

Sent with [ProtonMail](https://protonmail.com) Secure Email.
Message-ID: <34JKCz24u8awawkhTLmboO_cBRWW-m0XAb0frTUb9YqspQe1VeBBWUvfqKd3-QIMLeKpiDUyzhN0X6aqRlEqwX1LmBfg3gn9EB2XCa5v62s=@protonmail.com>
#
I suspect we'll need to know a bit more about your data to answer this
question. Can you share it in any form (e.g. variables renamed and
levels of factors changed to something opaque) ?

Best,
Phillip
On 16/2/21 4:02 am, Peter R Law via R-sig-mixed-models wrote:
#
I second Phillip's point.  The example below works as expected (gets 
a singular fit, but there are 6 covariance parameters as expected). 
Based on what you've told us so far, the most plausible explanation is 
that one or both of your covariates (x and/or z) are factors 
(categorical) rather than numeric.

   Ben Bolker



===========
set.seed(101)
dd <- data.frame(x=rnorm(500),z=rnorm(500),
                  g=factor(sample(1:6,size=500,replace=TRUE)))
form <- y ~ x + z + (x+z|g)
dd$y <- simulate(form[-2],
                  newdata=dd,
                  newparams=list(beta=rep(0,3),
                                 theta=rep(1,6),
                                 sigma=1))[[1]]


library(lme4)
m1 <- lmer(form, data=dd)
VarCorr(m1)
On 2/16/21 8:18 AM, Phillip Alday wrote: