r:repeated_measure_anova
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
r:repeated_measure_anova [2024/05/08 08:12] – created hkimscil | r:repeated_measure_anova [2024/05/08 08:23] (current) – [E.g. 2] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== E.g. 1 ====== | ||
+ | {{r: | ||
< | < | ||
- | # Data preparation | ||
- | # Wide format | ||
- | # install.packages(" | ||
- | # library(datarium) | ||
- | |||
- | data(" | ||
- | head(selfesteem, | ||
- | |||
- | # Gather columns t1, t2 and t3 into long format | ||
- | # Convert id and time into factor variables | ||
- | |||
- | # for %>% function, dplyr packages needed | ||
- | # install.packages(" | ||
- | # library(dplyr) | ||
- | |||
- | # for convert_as_factor function, rstatix needed | ||
- | # install.packages(" | ||
- | # library(rstatix) | ||
- | |||
- | selfesteem <- selfesteem %> | ||
- | gather(key = " | ||
- | convert_as_factor(id, | ||
- | head(selfesteem, | ||
- | |||
- | # get_summary_stats(group_by(selfesteem, | ||
- | # score, type = " | ||
- | # the above is the same as the below | ||
- | selfesteem %>% | ||
- | group_by(time) %>% | ||
- | get_summary_stats(score, | ||
- | |||
- | |||
- | res.aov <- anova_test( | ||
- | data = selfesteem, | ||
- | dv = score, | ||
- | wid = id, | ||
- | within = time) | ||
- | get_anova_table(res.aov) | ||
- | |||
- | # ges = generalized effect size | ||
- | # F | ||
- | # (2,18) | ||
- | |||
################################################### | ################################################### | ||
################################################### | ################################################### | ||
Line 82: | Line 41: | ||
# to statistically significant differences in | # to statistically significant differences in | ||
# response time (F(3, 12) = 24.76, p < 0.001). | # response time (F(3, 12) = 24.76, p < 0.001). | ||
+ | </ | ||
+ | < | ||
+ | > ################################################### | ||
+ | > ################################################### | ||
+ | > ################################################### | ||
+ | > | ||
+ | > # data | ||
+ | > df <- data.frame(patient=rep(1: | ||
+ | + drug=rep(1: | ||
+ | + response=c(30, | ||
+ | + 14, 18, 10, 22, | ||
+ | + 24, 20, 18, 30, | ||
+ | + 38, 34, 20, 44, | ||
+ | + 26, 28, 14, 30)) | ||
+ | > | ||
+ | > #view data | ||
+ | > df | ||
+ | | ||
+ | 1 1 1 30 | ||
+ | 2 1 2 28 | ||
+ | 3 1 3 16 | ||
+ | 4 1 4 34 | ||
+ | 5 2 1 14 | ||
+ | 6 2 2 18 | ||
+ | 7 2 3 10 | ||
+ | 8 2 4 22 | ||
+ | 9 3 1 24 | ||
+ | 10 | ||
+ | 11 | ||
+ | 12 | ||
+ | 13 | ||
+ | 14 | ||
+ | 15 | ||
+ | 16 | ||
+ | 17 | ||
+ | 18 | ||
+ | 19 | ||
+ | 20 | ||
+ | > write.csv(df, | ||
+ | > | ||
+ | > | ||
+ | > #fit repeated measures ANOVA model | ||
+ | > df$drug <- factor(df$drug) | ||
+ | > df$patient <- factor(df$patient) | ||
+ | > | ||
+ | > # Error(patient) = patient error should be isolated | ||
+ | > m.aov <- aov(response ~ drug | ||
+ | + + Error(patient), | ||
+ | + data = df) | ||
+ | > #view model summary | ||
+ | > summary(m.aov) | ||
+ | Error: patient | ||
+ | Df Sum Sq Mean Sq F value Pr(>F) | ||
+ | Residuals | ||
+ | |||
+ | Error: Within | ||
+ | Df Sum Sq Mean Sq F value | ||
+ | drug | ||
+ | Residuals 12 112.8 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | > | ||
+ | > # check probability level (pr) | ||
+ | > 1 - pf(24.75886525, | ||
+ | [1] 1.992501e-05 | ||
+ | > | ||
+ | > # A one-way repeated measures ANOVA was conducted | ||
+ | > # on five individuals to examine the effect that | ||
+ | > # four different drugs had on response time. | ||
+ | > | ||
+ | > # Results showed that the type of drug used lead | ||
+ | > # to statistically significant differences in | ||
+ | > # response time (F(3, 12) = 24.76, p < 0.001). | ||
+ | > | ||
+ | > | ||
+ | </ | ||
+ | ====== E.g. 2 ====== | ||
+ | {{: | ||
+ | < | ||
# the second | # the second | ||
movrev <- data.frame(reviewer=rep(1: | movrev <- data.frame(reviewer=rep(1: | ||
| | ||
| | ||
- | | + | 76, 78, 90, |
- | 78, 94, 95, | + | |
- | 80, 83, 88, | + | |
- | 82, 90, 99)) | + | |
#view data | #view data | ||
movrev | movrev | ||
- | write.csv(movrev, | + | write.csv(movrev, |
movrev$movie <- factor(movrev$movie) | movrev$movie <- factor(movrev$movie) | ||
Line 117: | Line 155: | ||
| | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | > # the second | ||
+ | > movrev <- data.frame(reviewer=rep(1: | ||
+ | + movie=rep(1: | ||
+ | + score=c(88, 84, 92, | ||
+ | + 76, 78, 90, | ||
+ | + 78, 94, 95, | ||
+ | + 80, 83, 88, | ||
+ | + 82, 90, 99)) | ||
+ | > | ||
+ | > #view data | ||
+ | > movrev | ||
+ | | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 4 | ||
+ | 11 4 | ||
+ | 12 4 | ||
+ | 13 5 | ||
+ | 14 5 | ||
+ | 15 5 | ||
+ | > write.csv(movrev, | ||
+ | > | ||
+ | > movrev$movie <- factor(movrev$movie) | ||
+ | > movrev$reviewer <- factor(movrev$reviewer) | ||
+ | > | ||
+ | > # Error(reviewer) = reviewer error should be isolated | ||
+ | > # The above is the same as Error(reviewer/ | ||
+ | > m.aov <- aov(score ~ movie | ||
+ | + + Error(reviewer), | ||
+ | + data = movrev) | ||
+ | > #view model summary | ||
+ | > summary(m.aov) | ||
+ | |||
+ | Error: reviewer | ||
+ | Df Sum Sq Mean Sq F value Pr(>F) | ||
+ | Residuals | ||
+ | |||
+ | Error: Within | ||
+ | Df Sum Sq Mean Sq F value Pr(> | ||
+ | movie 2 363.3 181.67 | ||
+ | Residuals | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | > | ||
+ | > # pairwise.t.test(movrev$score, | ||
+ | > attach(movrev) | ||
+ | The following objects are masked from movrev (pos = 5): | ||
+ | |||
+ | movie, reviewer, score | ||
+ | |||
+ | > pairwise.t.test(score, | ||
+ | |||
+ | Pairwise comparisons using paired t tests | ||
+ | |||
+ | data: score and movie | ||
+ | |||
+ | 1 | ||
+ | 2 0.628 - | ||
+ | 3 0.029 0.060 | ||
+ | |||
+ | P value adjustment method: bonferroni | ||
+ | > # or | ||
+ | > with(movrev, | ||
+ | + pairwise.t.test(score, | ||
+ | + paired = T, | ||
+ | + p.adjust.method = " | ||
+ | |||
+ | Pairwise comparisons using paired t tests | ||
+ | data: score and movie | ||
+ | 1 | ||
+ | 2 0.628 - | ||
+ | 3 0.029 0.060 | ||
+ | P value adjustment method: bonferroni | ||
+ | > | ||
</ | </ |
r/repeated_measure_anova.1715123562.txt.gz · Last modified: 2024/05/08 08:12 by hkimscil