User Tools

Site Tools


r:repeated_measure_anova

E.g. 1

rep.meas.anova.csv

###################################################
###################################################
###################################################

# data
df <- data.frame(patient=rep(1:5, each=4),
                 drug=rep(1:4, times=5),
                 response=c(30, 28, 16, 34,
                            14, 18, 10, 22,
                            24, 20, 18, 30,
                            38, 34, 20, 44,
                            26, 28, 14, 30))

#view data
df
write.csv(df, file="rep.meas.anova.csv")


#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)

# check probability level (pr)
1 - pf(24.75886525, 3, 12)

# 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).
> ###################################################
> ###################################################
> ###################################################
> 
> # data
> df <- data.frame(patient=rep(1:5, each=4),
+                  drug=rep(1:4, times=5),
+                  response=c(30, 28, 16, 34,
+                             14, 18, 10, 22,
+                             24, 20, 18, 30,
+                             38, 34, 20, 44,
+                             26, 28, 14, 30))
> 
> #view data
> df
   patient drug response
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       3    2       20
11       3    3       18
12       3    4       30
13       4    1       38
14       4    2       34
15       4    3       20
16       4    4       44
17       5    1       26
18       5    2       28
19       5    3       14
20       5    4       30
> write.csv(df, file="rep.meas.anova.csv")
> 
> 
> #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  4  680.8   170.2               

Error: Within
          Df Sum Sq Mean Sq F value   Pr(>F)    
drug       3  698.2   232.7   24.76 1.99e-05 ***
Residuals 12  112.8     9.4                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> # check probability level (pr)
> 1 - pf(24.75886525, 3, 12)
[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

rep.meas.anova.eg.movie.review.csv

# the second
movrev <- data.frame(reviewer=rep(1:5, each=3),
                 movie=rep(1:3, times=5),
                 score=c(88, 84, 92,
                         76, 78, 90,
                         78, 94, 95,
                         80, 83, 88, 
                         82, 90, 99))

#view data
movrev
write.csv(movrev, file="rep.meas.anova.eg.movie.review.csv")

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/movie)
m.aov <- aov(score ~ movie 
             + Error(reviewer), 
             data = movrev)
#view model summary
summary(m.aov)

# pairwise.t.test(movrev$score, movrev$movie, paired = T, p.adjust.method = "bonf")
attach(movrev)
pairwise.t.test(score, movie, paired = T, p.adjust.method = "bonf")
# or
with(movrev, 
     pairwise.t.test(score, movie, 
                     paired = T, 
                     p.adjust.method = "bonf"))
> # the second
> movrev <- data.frame(reviewer=rep(1:5, each=3),
+                  movie=rep(1:3, times=5),
+                  score=c(88, 84, 92,
+                             76, 78, 90,
+                             78, 94, 95,
+                             80, 83, 88, 
+                             82, 90, 99))
> 
> #view data
> movrev
   reviewer movie score
1         1     1    88
2         1     2    84
3         1     3    92
4         2     1    76
5         2     2    78
6         2     3    90
7         3     1    78
8         3     2    94
9         3     3    95
10        4     1    80
11        4     2    83
12        4     3    88
13        5     1    82
14        5     2    90
15        5     3    99
> write.csv(movrev, file="rep.meas.anova.eg.movie.review.csv")
> 
> 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/movie)
> 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  4  173.7   43.43               

Error: Within
          Df Sum Sq Mean Sq F value  Pr(>F)   
movie      2  363.3  181.67   10.19 0.00632 **
Residuals  8  142.7   17.83                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> # pairwise.t.test(movrev$score, movrev$movie, paired = T, p.adjust.method = "bonf")
> attach(movrev)
The following objects are masked from movrev (pos = 5):

    movie, reviewer, score

> pairwise.t.test(score, movie, paired = T, p.adjust.method = "bonf")

	Pairwise comparisons using paired t tests 

data:  score and movie 

  1     2    
2 0.628 -    
3 0.029 0.060

P value adjustment method: bonferroni 
> # or
> with(movrev, 
+      pairwise.t.test(score, movie, 
+                      paired = T, 
+                      p.adjust.method = "bonf"))

	Pairwise comparisons using paired t tests 

data:  score and movie 

  1     2    
2 0.628 -    
3 0.029 0.060

P value adjustment method: bonferroni 
> 
r/repeated_measure_anova.txt · Last modified: 2024/05/08 08:23 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki