lme4 mixed model error
Asked Answered
A

2

5

What is wrong with the following model:

 # simulated data yr = 2; vg = 4, fm = 5, gen = 5
    mbb <- data.frame( trait1 = rnorm(200, 15, 4),yr = c(rep (1:2, each = 100)), 
   vg = c(rep(rep(1:4, each =25), 2)), fm = rep(rep(1:5, each = 5), 8), 
    gen = sample(c(1:5), 200, replace = T))
    require(lme4) 
    lmer(trait1 ~ (yr + vg + gen)^3 + (yr + vg + gen|fm:vg), data= mbb)# full model 

I am getting following error:

Error in validObject(.Object) : 
  invalid class "mer" object: Slot Zt must by dims['q']  by dims['n']*dims['s']
In addition: Warning messages:
1: In fm:vg : numerical expression has 200 elements: only the first used
2: In fm:vg : numerical expression has 200 elements: only the first used
Apathy answered 25/10, 2011 at 16:59 Comment(1)
It doesn't seem to like the interaction bit of your random effect. Change gen|fm:vg to gen|fm and it runs. Not yet sure of the meaning of this.Boettcher
S
7

The problem is precisely that fm and vg are stored as numeric, not as factors, and so lmer tries to interpret fm:vg as a sequence operator (see ?seq) rather than an interaction operator (see ?interaction). You can:

  • convert fm and vg to factors within the data frame (mbb <- transform(mbb,vg=factor(vg),fm=factor(fm))) [it's not clear from your setup whether you want vg and fm to be factors or continuous predictors ... that distinction would be very important, of course ... if you want them as continuous predictors, then it's a bit weird to treat them as factors for the purposes of grouping ... ]

  • write the interaction explicitly as interaction(fm,vg) on the fly

  • convert to factors on the fly ((yr+vg+gen|factor(fm):factor(vg)))

  • use Jim M.'s solution

    I think these will all work, although I have to admit that I haven't tested them.

Seagraves answered 25/10, 2011 at 18:14 Comment(0)
V
3

One possible solution to model the interaction as a random effect is to add the interaction term as an additional column in the mbb data frame.

mbb$fmvg <- with(mbb, interaction(fm,vg, sep=":"))

The model then becomes

lmer(trait1 ~ (yr + vg + gen)^3 + (yr + vg + gen|fmvg), data= mbb)
Vale answered 25/10, 2011 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.