class: center, middle, inverse, title-slide # EDUC 640 ## Two-Way Mixed ANOVA ### Chris Ives --- # Contents * Restructuring Data * Two-Way Mixed ANOVA * Simple Effects * Custom Contrasts --- ## Restructuring Data (Review) Let's pivot the data into a long format and clean up the names. `janitor::clean_names()` by default changes everything to lowercase and snakecase (spaces are _). ```r lb8 <- lb8 %>% janitor::clean_names() %>% pivot_longer(cols = c(thirty, sixty, one_eighty), values_to = "total", names_to = "time_condition") %>% mutate(order = case_when(order == "Increasing time" ~ "increasing", order == "Decreasing time" ~ "decreasing")) ``` --- ## Plots ```r ggplot(lb8, aes(x = time_condition, y = total, fill = order)) + geom_boxplot() ``` <!-- --> --- ## Two-way Mixed ANOVA Since we now have both between and within variables, we have to remember to only put the within-subjects variable (time_condition only) in our `(Error(ID/...))`. ```r needs(afex) m1 <- afex::aov_car(total ~ time_condition + order + time_condition:order + Error(id/time_condition), data=lb8, include_aov = F) ``` ``` ## Converting to factor: order ``` ``` ## Contrasts set to contr.sum for the following variables: order ``` --- ```r summary(m1) ``` ``` ## ## Univariate Type III Repeated-Measures ANOVA Assuming Sphericity ## ## Sum Sq num Df Error SS den Df F value Pr(>F) ## (Intercept) 6912 1 127.33 14 759.958 1.338e-13 *** ## order 432 1 127.33 14 47.497 7.420e-06 *** ## time_condition 672 2 252.67 28 37.235 1.294e-08 *** ## order:time_condition 224 2 252.67 28 12.412 0.0001382 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## ## Mauchly Tests for Sphericity ## ## Test statistic p-value ## time_condition 0.34801 0.001048 ## order:time_condition 0.34801 0.001048 ## ## ## Greenhouse-Geisser and Huynh-Feldt Corrections ## for Departure from Sphericity ## ## GG eps Pr(>F[GG]) ## time_condition 0.60533 5.388e-06 *** ## order:time_condition 0.60533 0.001711 ** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## HF eps Pr(>F[HF]) ## time_condition 0.6317745 3.588465e-06 ## order:time_condition 0.6317745 1.443151e-03 ``` --- ## Simple Effects Our first set of simple effects will be examining `order` by `time_condition` ```r em <- emmeans(m1, "order", by = "time_condition") ``` ``` ## Substituting multivariate/lm model, as aov object missing. ``` ```r pairs(em) ``` ``` ## time_condition = thirty: ## contrast estimate SE df t.ratio p.value ## decreasing - increasing -4 1.134 14 -3.528 0.0033 ## ## time_condition = sixty: ## contrast estimate SE df t.ratio p.value ## decreasing - increasing -2 0.926 14 -2.160 0.0486 ## ## time_condition = one_eighty: ## contrast estimate SE df t.ratio p.value ## decreasing - increasing -12 2.155 14 -5.569 0.0001 ``` --- ## Second Set of Simple Effects Next we will compare `time_condition` by `order`. ```r means_v2 <- emmeans(m1, "time_condition", by = "order") ``` ``` ## Substituting multivariate/lm model, as aov object missing. ``` ```r pairs(means_v2) ``` ``` ## order = decreasing: ## contrast estimate SE df t.ratio p.value ## thirty - sixty -4 0.707 14 -5.657 0.0002 ## thirty - one_eighty -5 1.885 14 -2.652 0.0468 ## sixty - one_eighty -1 1.648 14 -0.607 0.8187 ## ## order = increasing: ## contrast estimate SE df t.ratio p.value ## thirty - sixty -2 0.707 14 -2.828 0.0337 ## thirty - one_eighty -13 1.885 14 -6.896 <.0001 ## sixty - one_eighty -11 1.648 14 -6.677 <.0001 ## ## P value adjustment: tukey method for comparing a family of 3 estimates ``` --- ## Profile Plots (Review) ```r means <- lb8 %>% group_by(order, time_condition) %>% summarise(total = mean(total)) %>% mutate(time_condition = factor(time_condition, c("thirty", "sixty", "one_eighty")), order = factor(order, c("increasing", "decreasing"))) ``` ``` ## `summarise()` has grouped output by 'order'. You can override using the `.groups` argument. ``` --- ```r ggplot(means, aes(x = time_condition, y = total, group = order)) + geom_point() + geom_line(aes(color = order)) ``` <!-- --> --- ```r ggplot(means, aes(x = order, y = total, group = time_condition)) + geom_point() + geom_line(aes(color = time_condition)) ``` <!-- --> --- ## Contrasts In the previous slides we used `emmeans` to do structured comparisons. However, for our contrasts we want to run them this way so we have all the factorial combinations in a list. ```r means <- emmeans(m1, ~time_condition*order) ``` ``` ## Substituting multivariate/lm model, as aov object missing. ``` ```r means ``` ``` ## time_condition order emmean SE df lower.CL upper.CL ## thirty decreasing 6 0.802 14 4.28 7.72 ## sixty decreasing 10 0.655 14 8.60 11.40 ## one_eighty decreasing 11 1.524 14 7.73 14.27 ## thirty increasing 10 0.802 14 8.28 11.72 ## sixty increasing 12 0.655 14 10.60 13.40 ## one_eighty increasing 23 1.524 14 19.73 26.27 ## ## Confidence level used: 0.95 ``` --- Our contrasts are similar to last week. To compare 30 vs 180, we code them for the decreasing condition and code them inversely for the increasing condition (anything that was negative is now positive and vice versa). ```r means ``` ``` ## time_condition order emmean SE df lower.CL upper.CL ## thirty decreasing 6 0.802 14 4.28 7.72 ## sixty decreasing 10 0.655 14 8.60 11.40 ## one_eighty decreasing 11 1.524 14 7.73 14.27 ## thirty increasing 10 0.802 14 8.28 11.72 ## sixty increasing 12 0.655 14 10.60 13.40 ## one_eighty increasing 23 1.524 14 19.73 26.27 ## ## Confidence level used: 0.95 ``` ```r contrasts <- list( thirty_vs_oneeighty = c(1, 0, -1, -1, 0, 1), sixty_vs_oneeighty = c(0, 1, -1, 0, -1, 1) ) ``` --- Note: The sign for the coefficients is different from SPSS. SPSS output would say 30 is 8 less (negative) than 180. R here is saying 180 is 8 more (positive) than 30 (our reference). ```r contrast(means, contrasts) ``` ``` ## contrast estimate SE df t.ratio p.value ## thirty_vs_oneeighty 8 2.67 14 3.001 0.0095 ## sixty_vs_oneeighty 10 2.33 14 4.292 0.0007 ```