No, model1
and model2
are not the same. model1
accounts for random variation within cluster1
, where as model2
accounts for random variation among cluster1
within fix1
. Here's another link to a different post on this site: Have I correctly specified my model in lmer?
But let's have quick example to illustrate that. Then you'll see that the estimated standard deviations for the random terms will be different. Also have a look at the line where it says Number of obs:
and groups:
. This will indicate that there are differences in the grouping structure as well.
set.seed(123)
df <- data.frame(FIXED=rep(c("A","B","C"), each=12),
RANDOM=rep(c("W","X","Y","Z"), each=3),
Y=runif(36,1,10))
head(df)
# FIXED RANDOM Y
#1 A W 3.588198
#2 A W 8.094746
#3 A W 4.680792
#4 A X 8.947157
#5 A X 9.464206
#6 A X 1.410008
require(lme4)
summary(lmer(Y ~ FIXED + (1|RANDOM), df))
# ...
# Random effects:
# Groups Name Variance Std.Dev.
# RANDOM (Intercept) 0.4556 0.675
# Residual 7.1980 2.683
#Number of obs: 36, groups: RANDOM, 4
#Fixed effects:
# Estimate Std. Error t value
#(Intercept) 6.3945 0.8448 7.569
#FIXEDB -0.1140 1.0953 -0.104
#FIXEDC -0.3000 1.0953 -0.274
# ...
summary(lmer(Y ~ FIXED + (1|FIXED:RANDOM), df))
# ...
# Random effects:
# Groups Name Variance Std.Dev.
# FIXED:RANDOM (Intercept) 7.887e-14 2.808e-07
# Residual 7.571e+00 2.752e+00
#Number of obs: 36, groups: FIXED:RANDOM, 12
#Fixed effects:
# Estimate Std. Error t value
#(Intercept) 6.3945 0.7943 8.051
#FIXEDB -0.1140 1.1233 -0.101
#FIXEDC -0.3000 1.1233 -0.267
# ...
Notice the difference in the standard deviation for the random effects but also the difference in the standard error for the fixed effects!
Now if I added another variable that combines RANDOM
and FIXED
, such as
df$RANDOM2 <- paste(df$FIXED, df$RANDOM, sep="_")
head(df)
# FIXED RANDOM Y RANDOM2
#1 A W 3.588198 A_W
#2 A W 8.094746 A_W
#3 A W 4.680792 A_W
#4 A X 8.947157 A_X
#5 A X 9.464206 A_X
#6 A X 1.410008 A_X
and then run
summary(lmer(Y ~ FIXED + (1|RANDOM2), df))
# ...
# Random effects:
# Groups Name Variance Std.Dev.
# FIXED:RANDOM (Intercept) 7.887e-14 2.808e-07
# Residual 7.571e+00 2.752e+00
#Number of obs: 36, groups: FIXED:RANDOM, 12
#Fixed effects:
# Estimate Std. Error t value
#(Intercept) 6.3945 0.7943 8.051
#FIXEDB -0.1140 1.1233 -0.101
#FIXEDC -0.3000 1.1233 -0.267
# ...
you can see that the output is the same as the output above using random term +(1|FIXED:RANDOM)
. Why is that? Because RANDOM
is implicitly nested in FIXED
whereas RANDOM2
is explicitly nested. So the syntax of your random factors depends on the way you setup your data table. I hope this example made the distinction between +(1|RANDOM)
and +(1|FIXED:RANDOM)
more clear.