Docconcoct Docconcoct - 2 months ago 12
R Question

Difference between aov() and ezANOVA outputs

I'm running a repeated measures ANOVA on this dataset:

ANOVA1_data<-structure(list(response_time = c(852.3760155, 1126.8859, 771.37418925,
515.039921, 704.730038, 498.255039538462, 414.674203166667, 1002.95192083333,
550.903181277778, 967.5577028, 1200.10554377778, 688.62227325,
568.7093463, 599.644536444444, 599.450236533333, 477.63492625,
852.6842127, 487.885839, 671.5945855, 1406.254308, 630.157431,
544.0455392, 952.309923166667, 555.052128285714, 534.940400142857,
958.7624941, 484.318274461538, 796.669692909091, 1094.1511547,
644.2667865, 513.026395333333, 578.4711744, 468.296029636364,
490.760670125, 993.210234181818, 485.081406666667, 784.688966692308,
994.1911471, 683.231708285714, 489.100872272727, 529.933097166667,
500.0458643, 502.0769296, 782.131561, 503.106378076923, 873.463863545455,
954.098663181818, 687.636025714286, 508.866894555556, 609.3654196875,
569.5037471, 485.838012375, 677.4758566, 490.894644727273, 950.1887905,
1273.720742, 677.823247, 590.922434416667, 587.083661727273,
519.520321, 497.9753709, 777.282322444444, 477.434508636364,
694.656964333333, 894.189973111111, 668.8171375, 492.210069352941,
633.660594166667, 511.815477066667, 385.2507655, 754.016341833333,
448.738492071429, 780.0980990625, 1350.88667075, 615.3092625,
516.4133455, 599.23199475, 528.664191823529, 465.009907142857,
645.526472, 506.692067461538, 741.007966333333, 1156.59821475,
739.794313666667, 579.644074272727, 711.2937442, 527.265395333333,
407.0503579, 679.8124972, 616.782603916667, 790.288266272727,
1225.061001, 659.54558775, 539.471006666667, 548.871068846154,
541.776038733333, 593.5888702, 917.187675222222, 634.578, 642.405313916667,
826.0807324, 557.183927375, 526.966490888889, 602.756101866667,
476.4600226875, 459.10276, 883.192454111111, 574.848643909091,
875.407049941176, 1167.006157875, 585.656621714286, 554.222852777778,
735.8162707, 499.69293775, 556.543906142857, 875.035436571429,
524.174892722222, 647.455149125, 1087.53515485714, 686.796519,
506.3197505625, 584.608293222222, 599.938951714286, 548.7216914,
716.97108425, 485.173967583333, 594.6478585, 1169.579207, 635.1226654,
576.322667125, 611.906749625, 534.87835075, 542.002233833333,
832.87268, 551.311072857143, 735.185274142857, 1019.00502225,
644.710864, 487.586029818182, 588.046562545455, 538.638263, 467.2331068,
992.040390333333, 447.6888445, 582.810538285714, 770.830189888889,
545.3166901, 555.4240726, 805.41041225, 485.093444294118, 549.1646476,
617.863255857143, 545.728531833333, 668.0134345, 988.09201925,
554.773951428571, 523.983008, 673.102314666667, 512.198864222222,
525.437858857143, 831.596884111111, 704.2265038, 675.469981090909,
929.600156857143, 562.595987090909, 528.187944909091, 667.77096975,
587.682777333333, 739.0132942, 784.1174622, 593.48687, 733.008363,
967.308045714286, 502.277713666667, 543.780318307692, 741.213556857143,
485.901219571429, 500.773469166667, 646.677269666667, 579.922640307692,
670.514432333333, 594.596857055556, 565.1742616, 519.029296666667,
826.665948, 517.6210813125, 638.896322142857, 842.662615666667,
608.046112833333, 695.388263090909, 731.689308222222, 614.044796375,
562.0161665, 676.51305425, 518.815232230769, 505.696347166667,
811.1939501, 620.4278604, 629.5713655, 886.479992428571, 540.152837090909,
519.513340125, 741.0639604, 514.0700816, 510.0282582, 652.348294,
578.9893216, 853.700193571429, 649.196684428571, 506.72766425,
530.0589578, 1191.7668945, 497.964943, 487.768835, 659.172599142857,
519.883308375, 862.009397333333, 795.90972425, 630.326232727273,
592.5958208, 1059.42960416667, 547.5455905, 545.340032125, 668.149378714286,
536.7357422, 727.936624538462, 826.002796875, 614.638075, 543.432615875,
1072.87504425, 534.1879206, 558.45495425, 800.5423618, 529.956305125,
744.011926571429, 717.220530111111, 618.982837166667, 561.909433333333,
735.876713666667, 528.781302636364, 578.9483135, 831.885292333333,
526.534026125, 620.34445925, 644.317982714286, 626.278062470588,
505.169543857143, 558.7303128, 482.935910466667, 520.953098333333,
776.282798272727, 558.65795325, 618.6108095, 775.767045818182,
509.158378916667, 519.430281333333, 700.8157985, 533.102873933333,
526.225282666667, 735.458925444444, 561.294387625, 669.371324125,
709.557364769231, 530.476736285714, 542.761542333333, 803.686653375,
547.784239181818, 585.437835125, 700.554143363636, 590.141187153846,
782.5425745, 612.78272575, 542.874586, 498.931897625, 797.0490638,
532.9786162, 580.352599555556, 896.370314538462, 529.1135348,
669.924511461538, 1049.25964625, 489.548872583333, 536.993126142857,
808.74337, 465.948712125, 476.2053386, 582.2254407, 648.819782777778
), subject_nr = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 0L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 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
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("RRRR", "ARRR", "RARR", "AARR", "RRAR",
"ARAR", "RAAR", "AAAR", "RRRA", "ARRA", "RARA", "AARA", "RRAA",
"ARAA", "RAAA", "AAAA"), class = "factor"), Visual1 = c("one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "one",
"one", "one", "one", "one", "one", "one", "one", "one", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two", "two",
"two", "two", "two", "two", "two", "two", "two", "two")), .Names = c("response_time",
"subject_nr", "Pattern", "Visual1"), row.names = c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 30L, 31L, 32L, 33L,
34L, 35L, 36L, 37L, 38L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 60L, 61L, 62L,
63L, 64L, 65L, 66L, 67L, 68L, 70L, 71L, 72L, 73L, 74L, 75L, 76L,
77L, 78L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L,
91L, 92L, 93L, 94L, 95L, 96L, 97L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 109L, 110L, 111L, 112L, 113L, 114L, 115L,
116L, 117L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 139L, 140L,
141L, 142L, 143L, 144L, 145L, 146L, 147L, 149L, 150L, 151L, 152L,
153L, 154L, 155L, 156L, 157L, 159L, 160L, 161L, 162L, 163L, 164L,
165L, 166L, 167L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L,
177L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 189L,
190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 199L, 200L, 201L,
202L, 203L, 204L, 205L, 206L, 207L, 209L, 210L, 211L, 212L, 213L,
214L, 215L, 216L, 217L, 219L, 220L, 221L, 222L, 223L, 224L, 225L,
226L, 227L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L,
239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 249L, 250L,
251L, 252L, 253L, 254L, 255L, 256L, 257L, 259L, 260L, 261L, 262L,
263L, 264L, 265L, 266L, 267L, 269L, 270L, 271L, 272L, 273L, 274L,
275L, 276L, 277L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L,
287L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 299L,
300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 309L, 310L, 311L,
312L, 313L, 314L, 315L, 316L, 317L, 319L), class = "data.frame")


I use library(ez) & aov():

ANOVAModel1 <- ezANOVA(data = ANOVA1_data, dv = .(response_time), wid = .(subject_nr), within = .(Pattern, Visual1), type = 1, detailed = TRUE)
ANOVAModel1

m.aov2 <- aov(response_time ~ (Pattern * Visual1) +
Error(subject_nr/(Pattern * Visual1)),
data = ANOVA1_data)

summary(m.aov2)


The outputs are different for each approach:

ANOVAModel1:

$ANOVA
Effect DFn DFd SSn SSd F p p<.05 ges
1 Pattern 15 120 188752.29 955689.9 1.5800295 0.08915725 0.05636435
2 Visual1 1 8 89415.79 1271189.0 0.5627222 0.47464681 0.02751719
3 Pattern:Visual1 15 120 131635.51 933157.5 1.1285170 0.33871622 0.03999047


m.aov2:

Error: subject_nr
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 1 1148803 1148803

Error: subject_nr:Pattern
Df Sum Sq Mean Sq
Pattern 15 99658 6644

Error: subject_nr:Visual1
Df Sum Sq Mean Sq
Visual1 1 4537 4537

Error: subject_nr:Pattern:Visual1
Df Sum Sq Mean Sq
Pattern:Visual1 15 92056 6137

Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
Pattern 15 266386 17759 0.549 0.910425
Visual1 1 404078 404078 12.488 0.000497 ***
Pattern:Visual1 15 144357 9624 0.297 0.995330
Residuals 224 7247992 32357
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


However, if I run:

m.aov3 <- aov(response_time ~ (Pattern * Visual1) +
Error(subject_nr),
data = ANOVA1_data)

summary(m.aov3)


I get the same Sum Square values as the ezANOVA output but still have different p-values:

Error: subject_nr
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 1 1148803 1148803

Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
Pattern 15 188752 12583 0.409 0.9759
Visual1 1 89416 89416 2.905 0.0895 .
Pattern:Visual1 15 131636 8776 0.285 0.9964
Residuals 255 7849261 30781
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


My understanding is that m.aov2 is the correct way to run a repeated measures ANOVA but perhaps I'm doing something wrong that accounts for the variation in results?

Any insight would be welcomed and appreciated.

Answer

As ezANOVA() warns, you forgot to change the class of subject_nr from integer to factor (and it would be better to change Visual1's class).

ANOVA1_data2 <- ANOVA1_data
ANOVA1_data2$subject_nr <- as.factor(ANOVA1_data2$subject_nr)
ANOVA1_data2$Visual1 <- as.factor(ANOVA1_data2$Visual1)

m.aov2.2 <- aov(response_time ~ (Pattern * Visual1) + 
                  Error(subject_nr/(Pattern * Visual1)), 
                data = ANOVA1_data2)            # the same model but the data modified
results
summary(m.aov2.2)

Error: subject_nr
          Df  Sum Sq Mean Sq F value Pr(>F)
Residuals  8 5838028  729753               

Error: subject_nr:Pattern
           Df Sum Sq Mean Sq F value Pr(>F)  
Pattern    15 188752   12583    1.58 0.0892 .
Residuals 120 955690    7964                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Error: subject_nr:Visual1
          Df  Sum Sq Mean Sq F value Pr(>F)
Visual1    1   89416   89416   0.563  0.475
Residuals  8 1271189  158899               

Error: subject_nr:Pattern:Visual1
                 Df Sum Sq Mean Sq F value Pr(>F)
Pattern:Visual1  15 131636    8776   1.129  0.339
Residuals       120 933157    7776     
details of p.value
p.val <- NA
for(i in 2:4) p.val[i-1] <- summary(m.aov2.2)[i][[1]][[1]][1,5]
  > p.val
   [1] 0.08915725 0.47464681 0.33871622

  > ANOVAModel1[[1]]$p
   [1] 0.08915725 0.47464681 0.33871622    # the same
Comments