class: center, middle, inverse, title-slide # Repeated measures ANOVA ## Within-subjects designs ### Matthew Crump ### 2018/07/20 (updated: 2019-04-08) --- # Overview 1. Repeated Measures ANOVA 2. New partition for subjects 3. Lab example --- # Repeated measures ANOVA When to use: 1. Your design is within-subjects 2. Your IV has multiple levels (2 or more). --- class: center, middle, clear, nopad <img src="figs/rmanova/SS_ANOVA.png" width="90%" /> --- # A new partition for Subjects <img src="figs/rmanova/SS_RMANOVA.png" width="1627" /> --- class: center, middle, clear, nopad <img src="figs/rmanova/rmANOVAtable.png" width="1821" /> --- # Goal: Fill in this table <img src="figs/rmanova/rmblank.png" width="1088" /> --- # SS TOTAL <img src="figs/rmanova/rmTotal.png" width="90%" /> --- # SS Effect <img src="figs/rmanova/rmEffect.png" width="90%" /> --- # SS Subject <img src="figs/rmanova/rmSubject.png" width="90%" /> --- # Again: SS effect and subject <img src="figs/rmanova/rm1.png" width="100%" /> --- # ANOVA table <img src="figs/rmanova/rmfilled.png" width="90%" /> --- # R for RM anova ```r A <- c(1,2,1,2,4) B <- c(9,7,6,4,4) C <- c(2,9,2,3,4) DV <- c(A,B,C) subjects <- as.factor(c(1,2,3,4,5)) IV <- rep(c("A","B","C"), each=5) df <- data.frame(subjects,IV, DV) ``` --- # the data frame (rows 1:8) <table> <thead> <tr> <th style="text-align:left;"> subjects </th> <th style="text-align:left;"> IV </th> <th style="text-align:right;"> DV </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> A </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> A </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> A </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> 4 </td> <td style="text-align:left;"> A </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> 5 </td> <td style="text-align:left;"> A </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> B </td> <td style="text-align:right;"> 9 </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> B </td> <td style="text-align:right;"> 7 </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> B </td> <td style="text-align:right;"> 6 </td> </tr> </tbody> </table> --- # the data frame (rows 9:15) <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> subjects </th> <th style="text-align:left;"> IV </th> <th style="text-align:right;"> DV </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 9 </td> <td style="text-align:left;"> 4 </td> <td style="text-align:left;"> B </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> 10 </td> <td style="text-align:left;"> 5 </td> <td style="text-align:left;"> B </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> 11 </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> 12 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 9 </td> </tr> <tr> <td style="text-align:left;"> 13 </td> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> 14 </td> <td style="text-align:left;"> 4 </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:left;"> 15 </td> <td style="text-align:left;"> 5 </td> <td style="text-align:left;"> C </td> <td style="text-align:right;"> 4 </td> </tr> </tbody> </table> --- # R anova formula for repeated measures `aov(DV~IV + Error(subjects/IV), df)` ```r summary(aov(DV~IV+Error(subjects/IV),df)) ``` --- # R anova table ``` ## ## Error: subjects ## Df Sum Sq Mean Sq F value Pr(>F) ## Residuals 4 18 4.5 ## ## Error: subjects:IV ## Df Sum Sq Mean Sq F value Pr(>F) ## IV 2 40 20 4 0.0625 . ## Residuals 8 40 5 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- # calculating SS subjects ```r library(dplyr) subject_means <- df %>% group_by(subjects) %>% summarise(means=mean(DV)) grand_mean <- mean(df$DV) n_groups <- length(unique(df$IV)) SS_subjects <- n_groups*sum((subject_means$means-grand_mean)^2) print(SS_subjects) ``` ``` ## [1] 18 ``` --- # Fs and ps R has `pf()` and `qf()` functions for finding critical values of F given a particular p-value, and p-values associated with particular Fs --- # pf() What is the p-value associated with F(3,27) = 2.15 ? `pf(q, df1, df2)` (q is the f-value, stands for quantile) ```r 1-pf(2.15, 3, 27) ``` ``` ## [1] 0.1172199 ``` An F of 2.15 or larger, with degrees of freedom (3,27), occurs with this probability. --- # qf() Assume an alpha of .05. What is the critical value of F, with degrees of freedom (3,27)? `qf(p, df1, df2)` ```r qf(.95, 3, 27) ``` ``` ## [1] 2.960351 ``` We set p to .95. Fs larger than the critical value will occur less than 1-.95=.05 of the time. --- # Lab Example <img src="figs/rmanova/lab1.png" width="100%" /> --- # Research question One research question was whether we would replicate previous finding showing that people type faster when the letters are in word or word like arrangements, compared to random strings - Word: truck - english-like: quemp - random: wxzjy --- # Repeated Measures Design - everybody typed letters in the word, english-like, and random conditions <img src="figs/rmanova/lab2.png" width="90%" /> --- # Results <img src="figs/rmanova/lab3.png" width="100%" /> --- # Next class: Repeated measures ANOVA II 1. First ANOVA quiz due Wednesday, April 10th 2. Repeated measures ANOVA quiz will begin Wednesday, April 10th 3. Review of extra-credit assignment on Wednesday, April 10th