Solved – How to enter a continuous variable as a random effect in a linear mixed effects model

mixed model

I collected data on the growth of juvenile fish from 4 different types of crosses using multiple distinct family blocks and I am trying to see if cross type has an effect on growth using linear mixed effects models. I have one fixed factor (Length), 3 categorical random factors (sire, dam and sire/dam interaction) and a continuous random factor (density per tank). As I am relatively new to R and new to mixed effects models, I was wondering if I would have to code the continuous random factor in R differently from the categorical random factors using lme4?

So far I have

model5=lmer(Length~(1|Dam)+(1|Sire)+(1|Sire:Dam)+(1|Density))
model6=lmer(Length~Cross+(1|Dam)+(1|Sire)+(1|Sire:Dam)+(1|Density))

is density coded in properly, or would I have to alter it due to the fact that it is a continuous variable?

Best Answer

I'll elaborate on what I think Sergio meant in his comment.

A random effect is always associated with a categorical variable. This categorical variable will most often divide the observations into different observational units (this could for instance be Dam in your data set as it seems reasonable to assume that observations from the same dam are more alike than from different dams. Using something like (1|Dam) will give you a random intercept on that variable.

Using a continuous predictor like Density you can get a random slope on the predictor. Then you'll have to use (Density|Dam) in your model formulae. This will give you a (random) slope, i.e. effect of Density, for each level of Dam.

What you're doing in the above code is forcing Density to be used as a categorical predictor, i.e. making a random intercept for each level (value) of Density. This is probably not what you want.