# Solved – In lme4, how to specify a one-way ANOVA within or between subject

Using the lme4 package for mixed effect models in R, I am trying to figure out what is the difference in modelling a one-way ANOVA within subject and a one-way ANOVA between subject.

Suppose first, that each subject see all three treatments (A, B and C). In each treatment, each subject gives me one measure (DV). I can model this within-subject design as follows:

``lmer(DV ~ treatment + (1|subject), data = My_Data) ``

Now, suppose each subject sees only one treatment. What is the corresponding model? Would it be the same? If yes, how will lmer() know that it is a between subject design?

Contents

The model formula will be the same in both cases. lmer knows whether the factor is within or between.

We can see this with a simple simulation. First, simulate a within-subject design:

``> set.seed(15) > dt.se <- expand.grid(subject = 1:10, treatmentWithin = as.factor(c("A", "B", "C"))) > > dt.se$$DV <- dt.se$$subject * as.numeric(dt.se\$treatmentWithin) + rnorm(nrow(dt.se), 0, 1) > > xtabs(~ treatmentWithin + subject, dt.se)                  subject treatmentWithin 1 2 3 4 5 6 7 8 9 10               A 1 1 1 1 1 1 1 1 1  1               B 1 1 1 1 1 1 1 1 1  1               C 1 1 1 1 1 1 1 1 1  1 ``

So we can see that each subject receives each treatment once. Then we fit the model:

``> summary(lmm1 <- lmer(DV ~ treatmentWithin + (1|subject), data = dt.se))   Linear mixed model fit by REML ['lmerMod'] Formula: DV ~ treatmentWithin + (1 | subject)    Data: dt.se  REML criterion at convergence: 167.3  Scaled residuals:      Min      1Q  Median      3Q     Max  -1.5772 -0.4341  0.0261  0.4912  1.9308   Random effects:  Groups   Name        Variance Std.Dev.  subject  (Intercept) 29.8     5.46      Residual             10.5     3.24     Number of obs: 30, groups:  subject, 10  Fixed effects:                  Estimate Std. Error t value (Intercept)          5.68       2.01    2.83 treatmentWithinB     5.24       1.45    3.61 treatmentWithinC    11.29       1.45    7.78 ``

Now we create a new treatment variable for a between-subject design:

``> dt.se\$treatmentBetween <- as.factor(rep(c(rep(c("A", "B", "C"), 3), "A"), 3)) > xtabs(~ treatmentBetween + subject, dt.se)                  subject treatmentBetween 1 2 3 4 5 6 7 8 9 10                A 3 0 0 3 0 0 3 0 0  3                B 0 3 0 0 3 0 0 3 0  0                C 0 0 3 0 0 3 0 0 3  0 ``

Now we see that each subject receives only one treatment – 3 times each. So now we can fit the model with the same formula:

``> summary(lmm2 <- lmer(DV ~ treatmentBetween + (1|subject), data = dt.se))   Linear mixed model fit by REML ['lmerMod'] Formula: DV ~ treatmentBetween + (1 | subject)    Data: dt.se  REML criterion at convergence: 191.9  Scaled residuals:      Min      1Q  Median      3Q     Max  -1.2516 -0.6507 -0.0686  0.3562  2.1658   Random effects:  Groups   Name        Variance Std.Dev.  subject  (Intercept) 28.8     5.36      Residual             41.4     6.43     Number of obs: 30, groups:  subject, 10  Fixed effects:                   Estimate Std. Error t value (Intercept)         11.146      3.262    3.42 treatmentBetweenB   -0.448      4.982   -0.09 treatmentBetweenC    0.590      4.982    0.12 ``

Rate this post