How do I specify random factors in R ?
If I have a factor x1
which is supposed to be random , can I try something like this ?
lm(y ~ x1, data = p)
How do I specify random factors in R ?
If I have a factor x1
which is supposed to be random , can I try something like this ?
lm(y ~ x1, data = p)
Most simply (using the older nlme
rather than the new lme4
),
library(nlme)
lme(fixed = y ~ 1, random = ~1|x1, data = p)
The equivalent in lme4
is:
library(lme4)
lmer(y~1+(1|x1), data = p)
Are you by chance looking for the lme4 package which is focussed on linear mixed-effects (ie. fixed versus random) modelling?
Zuur et al 2011, Mixed Effects Models and Extensions in Ecology with R, has an excellent walk through of random effects using the nlme package. They explain the differences between fixed, random, and mixed models and how to specify random intercepts and random slopes.