User Tools

Site Tools


r:repeated_measure_anova

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:repeated_measure_anova [2024/05/08 08:23] – [E.g. 2] hkimscilr:repeated_measure_anova [2025/05/12 08:25] (current) – [E.g.2 by hand] hkimscil
Line 30: Line 30:
 #view model summary #view model summary
 summary(m.aov) summary(m.aov)
 +
 +# Error(patient/drug) = patient error embedded in drug
 +# the same thing
 +# the latter should be preferred
 +m2.aov <- aov(response ~ drug 
 +              + Error(patient/drug), 
 +              data = df)
 +summary(m2.aov)
 +
 +# check this 
 +m3.aov <- aov(response ~ drug, data = df)
 +summary(m3.aov)
  
 # check probability level (pr) # check probability level (pr)
 1 - pf(24.75886525, 3, 12) 1 - pf(24.75886525, 3, 12)
 +# or 
 +library(broom)
 +fit <- tidy(m2.aov)
 +fit
 +fit$statistic[2]
 +1 - pf(fit$statistic[2], 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).
  
 # A one-way repeated measures ANOVA was conducted  # A one-way repeated measures ANOVA was conducted 
Line 44: Line 69:
  
 <code> <code>
-> ################################################### 
-> ################################################### 
-> ################################################### 
- 
 > # data > # data
 > df <- data.frame(patient=rep(1:5, each=4), > df <- data.frame(patient=rep(1:5, each=4),
Line 103: Line 124:
 Residuals 12  112.8     9.4                      Residuals 12  112.8     9.4                     
 --- ---
-Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1+Signif. codes:   
 +0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
 +>  
 +> # Error(patient/drug) = patient error embedded in drug 
 +> # the same thing 
 +> # the latter should be preferred 
 +> m2.aov <- aov(response ~ drug  
 ++               + Error(patient/drug),  
 ++               data = df) 
 +> summary(m2.aov) 
 + 
 +Error: patient 
 +          Df Sum Sq Mean Sq F value Pr(>F) 
 +Residuals  4  680.8   170.2                
 + 
 +Error: patient:drug 
 +          Df Sum Sq Mean Sq F value   Pr(>F)     
 +drug        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 this  
 +> m3.aov <- aov(response ~ drug, data = df) 
 +> summary(m3.aov) 
 +            Df Sum Sq Mean Sq F value Pr(>F)   
 +drug          698.2   232.7   4.692 0.0155 * 
 +Residuals   16  793.6    49.6                  
 +--- 
 +Signif. codes:   
 +0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  
 > # check probability level (pr) > # check probability level (pr)
 > 1 - pf(24.75886525, 3, 12) > 1 - pf(24.75886525, 3, 12)
 [1] 1.992501e-05 [1] 1.992501e-05
->  +# or  
-# A one-way repeated measures ANOVA was conducted  +library(broom) 
-# on five individuals to examine the effect that  +fit <- tidy(m2.aov) 
-> # four different drugs had on response time+fit 
- +A tibble: 3 × 7 
-# Results showed that the type of drug used lead  +  stratum      term     df sumsq meansq statistic  p.value 
-# to statistically significant differences in  +  <chr       <chr<dbl> <dbl>  <dbl>     <dbl>    <dbl> 
-# response time (F(3, 12) = 24.76, p < 0.001).+1 patient      Resi…      681.  170.       NA   NA       
 +2 patient:drug drug      3  698.  233.       24.8  1.99e-5 
 +3 patient:drug Resi…    12  113.    9.4      NA   NA       
 +fit$statistic[2] 
 +[1] 24.75887 
 +1 - pf(fit$statistic[2], 3, 12) 
 +[1] 1.992501e-05
  
  
Line 126: Line 184:
                  movie=rep(1:3, times=5),                  movie=rep(1:3, times=5),
                  score=c(88, 84, 92,                  score=c(88, 84, 92,
-                         76, 78, 90, +                            76, 78, 90, 
-                         78, 94, 95, +                            78, 94, 95, 
-                         80, 83, 88,  +                            80, 83, 88,  
-                         82, 90, 99))+                            82, 90, 99))
  
 #view data #view data
Line 143: Line 201:
              + Error(reviewer),               + Error(reviewer), 
              data = movrev)              data = movrev)
 +m2.aov <- aov(score ~ movie
 +              + Error(reviewer/movie),
 +              data = movrev)
 +
 +#view model summary
 +summary(m.aov)
 +summary(m2.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")
 +detach(movrev)
 +# 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
 +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)
 +m2.aov <- aov(score ~ movie
 +              + Error(reviewer/movie),
 +              data = movrev)
 +
 #view model summary #view model summary
 summary(m.aov) summary(m.aov)
 +summary(m2.aov)
  
 # pairwise.t.test(movrev$score, movrev$movie, paired = T, p.adjust.method = "bonf") # pairwise.t.test(movrev$score, movrev$movie, paired = T, p.adjust.method = "bonf")
 attach(movrev) attach(movrev)
 pairwise.t.test(score, movie, paired = T, p.adjust.method = "bonf") pairwise.t.test(score, movie, paired = T, p.adjust.method = "bonf")
 +detach(movrev)
 # or # or
 with(movrev,  with(movrev, 
Line 196: Line 298:
 +              + Error(reviewer),  +              + Error(reviewer), 
 +              data = movrev) +              data = movrev)
 +> m2.aov <- aov(score ~ movie
 ++               + Error(reviewer/movie),
 ++               data = movrev)
 +
 > #view model summary > #view model summary
 > summary(m.aov) > summary(m.aov)
Line 208: Line 314:
 Residuals  8  142.7   17.83                    Residuals  8  142.7   17.83                   
 --- ---
-Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1+Signif. codes:   
 +0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
 +> summary(m2.aov) 
 + 
 +Error: reviewer 
 +          Df Sum Sq Mean Sq F value Pr(>F) 
 +Residuals  4  173.7   43.43                
 + 
 +Error: reviewer:movie 
 +          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") > # pairwise.t.test(movrev$score, movrev$movie, paired = T, p.adjust.method = "bonf")
 > attach(movrev) > attach(movrev)
-The following objects are masked from movrev (pos = 5):+The following objects are masked from movrev (pos = 12): 
 + 
 +    movie, reviewer, score 
 + 
 +The following objects are masked from movrev (pos = 14):
  
     movie, reviewer, score     movie, reviewer, score
Line 227: Line 351:
  
 P value adjustment method: bonferroni  P value adjustment method: bonferroni 
 +> detach(movrev)
 > # or > # or
 > with(movrev,  > with(movrev, 
Line 242: Line 367:
  
 P value adjustment method: bonferroni  P value adjustment method: bonferroni 
 +
 +
 +</code>
 +====== E.g.2 by hand ======
 +<code>
 +####
 +# by hand
 +####
 +movrev
 +tapply(movrev$score, list(reviewer, movie), mean) # 각 셀의 평균값
 +m.tot <- mean(movrev$score)
 +
 +
 +m.by.movie <- tapply(movrev$score, list(movie), mean)
 +m.by.er <- tapply(movrev$score, list(reviewer), mean)
 +
 +ss.bet <- sum(5*(m.tot-m.by.movie)^2)
 +df.bet <- 3-1 # 영화 가짓수 - 1
 +ms.bet <- ss.bet / df.bet
 +
 +var.by.movie <- tapply(movrev$score, list(movie), var)
 +ss.with <- sum(var.by.movie*4)
 +df.with <- 4 + 4 + 4
 +ms.with <- ss.with / df.with
 +ms.bet
 +ms.with
 +
 +ss.sub <- sum(3 * (m.tot-m.by.er)^2) # 3 treatments (movies)
 +df.sub <- 5 - 1 # 5 persons - 1
 +ms.sub <- ss.sub/df.sub
 +ms.sub
 +
 +ss.res <- ss.with - ss.sub
 +ss.res
 +df.res <- 4 * 2 # (N-1)*(k-1)
 +ms.res <- ss.res/df.res
 +ms.res
 +
 +f.cal <- ms.bet/ms.res
 +f.cal
 +pf(f.cal, 2, 8, lower.tail = F)
 +
 +ss.bet
 +ss.with
 +ss.sub
 +ss.res
 +df.bet
 +df.with
 +df.sub
 +df.res
 +ms.bet 
 +ms.with
 +ms.sub
 +ms.res
 +
 +# check
 +ss.with == ss.sub + ss.res
 +df.with == df.sub + df.res
 +summary(m.aov)
 +#
 +# no embedded
 +# understand the meaning of embedded
 +m0.aov <- aov(score ~ movie, data = movrev)
 +summary(m0.aov)
 +summary(m.aov)
 +
 +
 +</code>
 +
 +<code>
 +> ####
 +> # by hand
 +> ####
 +> movrev
 +   reviewer movie score
 +1                88
 +2                84
 +3                92
 +4                76
 +5                78
 +6                90
 +7                78
 +8                94
 +9                95
 +10        4        80
 +11        4        83
 +12        4        88
 +13        5        82
 +14        5        90
 +15        5        99
 +> tapply(movrev$score, list(reviewer, movie), mean) # 각 셀의 평균값
 +    2  3
 +1 88 84 92
 +2 76 78 90
 +3 78 94 95
 +4 80 83 88
 +5 82 90 99
 +> m.tot <- mean(movrev$score)
 +
 +
 +> m.by.movie <- tapply(movrev$score, list(movie), mean)
 +> m.by.er <- tapply(movrev$score, list(reviewer), mean)
 +
 +> ss.bet <- sum(5*(m.tot-m.by.movie)^2)
 +> df.bet <- 3-1 # 영화 가짓수 - 1
 +> ms.bet <- ss.bet / df.bet
 +
 +> var.by.movie <- tapply(movrev$score, list(movie), var)
 +> ss.with <- sum(var.by.movie*4)
 +> df.with <- 4 + 4 + 4
 +> ms.with <- ss.with / df.with
 +> ms.bet
 +[1] 181.6667
 +> ms.with
 +[1] 26.36667
 +
 +> ss.sub <- sum(3 * (m.tot-m.by.er)^2) # 3 treatments (movies)
 +> df.sub <- 5 - 1 # 5 persons - 1
 +> ms.sub <- ss.sub/df.sub
 +> ms.sub
 +[1] 43.43333
 +
 +> ss.res <- ss.with - ss.sub
 +> ss.res
 +[1] 142.6667
 +> df.res <- 4 * 2 # (N-1)*(k-1)
 +> ms.res <- ss.res/df.res
 +> ms.res
 +[1] 17.83333
 +
 +> f.cal <- ms.bet/ms.res
 +> f.cal
 +[1] 10.18692
 +> pf(f.cal, 2, 8, lower.tail = F)
 +[1] 0.006319577
 +
 +> ss.bet
 +[1] 363.3333
 +> ss.with
 +[1] 316.4
 +> ss.sub
 +[1] 173.7333
 +> ss.res
 +[1] 142.6667
 +> df.bet
 +[1] 2
 +> df.with
 +[1] 12
 +> df.sub
 +[1] 4
 +> df.res
 +[1] 8
 +> ms.bet 
 +[1] 181.6667
 +> ms.with
 +[1] 26.36667
 +> ms.sub
 +[1] 43.43333
 +> ms.res
 +[1] 17.83333
 +
 +> # check
 +> ss.with == ss.sub + ss.res
 +[1] TRUE
 +> df.with == df.sub + df.res
 +[1] TRUE
 +> 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
 +> #
 +> # no embedded
 +> # understand the meaning of embedded
 +> m0.aov <- aov(score ~ movie, data = movrev)
 +> summary(m0.aov)
 +            Df Sum Sq Mean Sq F value Pr(>F)  
 +movie        2  363.3  181.67    6.89 0.0102 *
 +Residuals   12  316.4   26.37                 
 +---
 +Signif. codes:  
 +0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +> 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
 +
 +>
  
 </code> </code>
r/repeated_measure_anova.1715124188.txt.gz · Last modified: 2024/05/08 08:23 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki