class: center, middle, inverse, title-slide # EDUC 640 ## Within Subjects ANOVA ### Chris Ives --- # Contents * Restructuring Data * Within-Subjects ANOVA * Pairwise Comparisons * Appendix + Contrasts --- ## Restructuring Data We need to restructure the data into a "long" format for ANOVA in R. I start by renaming the variables and then use `pivot_longer` to pivot those variables into two columns. The names of the columns become `inst` and the values become `vocab`. ```r lb6 <- lb6 %>% rename(phys = vocab1, social = vocab2, hist = vocab3) %>% pivot_longer(cols = c(phys, social, hist), values_to = "vocab", names_to = "inst") ``` --- Then we can just make a normal boxplot. ```r ggplot(lb6, aes(x = inst, y = vocab)) + geom_boxplot() ``` <!-- --> --- ## Within Subjects ANOVA `aov_car` will allow us to run `emmeans` on the object and uses the same method for pairwise comparisons as SPSS. We just need to specify our ID variable and our IVs within `Error()`. Mauchly's test is automatically included in the output. ```r needs(afex) m1 <- afex::aov_car(vocab ~ inst + Error(idnum/inst), data=lb6, include_aov = F) ``` --- ```r summary(m1) ``` ``` ## ## Univariate Type III Repeated-Measures ANOVA Assuming Sphericity ## ## Sum Sq num Df Error SS den Df F value Pr(>F) ## (Intercept) 40401 1 3531.7 11 125.836 2.319e-07 *** ## inst 1194 2 1067.3 22 12.305 0.000259 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## ## Mauchly Tests for Sphericity ## ## Test statistic p-value ## inst 0.41524 0.012345 ## ## ## Greenhouse-Geisser and Huynh-Feldt Corrections ## for Departure from Sphericity ## ## GG eps Pr(>F[GG]) ## inst 0.63101 0.00225 ** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## HF eps Pr(>F[HF]) ## inst 0.6748954 0.00173583 ``` --- Don't worry about within-subjects contrasts. We can ignore it and don't have to worry about it. Gina says so :) However, a brief demonstration is included in the Appendix. --- ## Pairwise Comparisons Pairwise comparsions are similar to how we did contrasts last week. We run `emmeans` on our model using our formula (`~inst`) and then run pairs on that object. ```r em <- emmeans(m1, ~inst) ``` ``` ## Substituting multivariate/lm model, as aov object missing. ``` ```r pairs(em) ``` ``` ## contrast estimate SE df t.ratio p.value ## phys - social 14.0 3.01 11 4.651 0.0019 ## phys - hist 5.5 1.52 11 3.618 0.0104 ## social - hist -8.5 3.59 11 -2.368 0.0875 ## ## P value adjustment: tukey method for comparing a family of 3 estimates ``` --- `pairs` does not automatically generate confidence intervals. If you needed them, you can wrap it with `confint` or run it on your pairs object. ```r confint(pairs(em)) ``` ``` ## contrast estimate SE df lower.CL upper.CL ## phys - social 14.0 3.01 11 5.87 22.13 ## phys - hist 5.5 1.52 11 1.39 9.61 ## social - hist -8.5 3.59 11 -18.20 1.20 ## ## Confidence level used: 0.95 ## Conf-level adjustment: tukey method for comparing a family of 3 estimates ``` --- class: inverse-blue middle # Appendix --- ## Contrasts Contrasts require just a different step to our pairwise comparions. We just create our contrasts in a list and run `contrast` on our emmeans object. If it was a 2-way Within-Subjects ANOVA, we would have to run it on our pairs object. ```r contrasts <- list( hyp1 = c(-.5, 1, -.5), hyp2 = c(0, 1, -1) ) ``` --- ```r contrast(em, contrasts) ``` ``` ## contrast estimate SE df t.ratio p.value ## hyp1 -11.2 3.22 11 -3.489 0.0051 ## hyp2 -8.5 3.59 11 -2.368 0.0373 ```