User Tools

Site Tools


r:oneway_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:oneway_anova [2018/12/04 03:16] hkimscilr:oneway_anova [2022/04/28 10:26] (current) – [data] hkimscil
Line 1: Line 1:
 ====== Oneway ANOVA ====== ====== Oneway ANOVA ======
 +===== data =====
 +see https://github.com/hkimscil/ms/blob/main/anova.R
 +
 | (온도조건)x1  | 50.5  | 52.1  | 51.9  | 52.4  | 50.6  | 51.4  | 51.2  | 52.2  | 51.5  | 50.8  | | (온도조건)x1  | 50.5  | 52.1  | 51.9  | 52.4  | 50.6  | 51.4  | 51.2  | 52.2  | 51.5  | 50.8  |
 | (온도조건)x2  | 47.5  | 47.7  | 46.6  | 47.1  | 47.2  | 47.8  | 45.2  | 47.4  | 45.0  | 47.9  | | (온도조건)x2  | 47.5  | 47.7  | 46.6  | 47.1  | 47.2  | 47.8  | 45.2  | 47.4  | 45.0  | 47.9  |
Line 71: Line 74:
 # or use, tapply function as shown the below # or use, tapply function as shown the below
 </code> </code>
 +
 +변인 이름과 levels 정리 
 +변인이름 
 +  * score <- 점수 (performance)
 +  * temp <- 교실의 온도
 +  * 즉 교실의 온도에 따라서 점수가 다르게 나옴
 +temp의 level 정리
 +  * x1 - low 낮은온도
 +  * x2 - mid 중간온도
 +  * x3 - hi  높은온도
 +<code>
 +colnames(xs) <- c("score", "temp")
 +xs
 +levels(xs$temp) <- c("low", "mid", "hi")
 +xs
 +</code>
 +
 +<code>
 +> colnames(xs) <- c("score", "temp")
 +> xs
 +   score temp
 +1   50.5   x1
 +2   52.1   x1
 +3   51.9   x1
 +4   52.4   x1
 +5   50.6   x1
 +6   51.4   x1
 +7   51.2   x1
 +8   52.2   x1
 +9   51.5   x1
 +10  50.8   x1
 +11  47.5   x2
 +12  47.7   x2
 +13  46.6   x2
 +14  47.1   x2
 +15  47.2   x2
 +16  47.8   x2
 +17  45.2   x2
 +18  47.4   x2
 +19  45.0   x2
 +20  47.9   x2
 +21  46.0   x3
 +22  47.1   x3
 +23  45.6   x3
 +24  47.1   x3
 +25  47.2   x3
 +26  46.4   x3
 +27  45.9   x3
 +28  47.1   x3
 +29  44.9   x3
 +30  46.2   x3
 +> str(xs)
 +'data.frame': 30 obs. of  2 variables:
 + $ score: num  50.5 52.1 51.9 52.4 50.6 51.4 51.2 52.2 51.5 50.8 ...
 + $ temp : Factor w/ 3 levels "x1","x2","x3": 1 1 1 1 1 1 1 1 1 1 ...
 +> levels(xs$temp) <- c("low", "mid", "hi")
 +> xs
 +   score temp
 +1   50.5  low
 +2   52.1  low
 +3   51.9  low
 +4   52.4  low
 +5   50.6  low
 +6   51.4  low
 +7   51.2  low
 +8   52.2  low
 +9   51.5  low
 +10  50.8  low
 +11  47.5  mid
 +12  47.7  mid
 +13  46.6  mid
 +14  47.1  mid
 +15  47.2  mid
 +16  47.8  mid
 +17  45.2  mid
 +18  47.4  mid
 +19  45.0  mid
 +20  47.9  mid
 +21  46.0   hi
 +22  47.1   hi
 +23  45.6   hi
 +24  47.1   hi
 +25  47.2   hi
 +26  46.4   hi
 +27  45.9   hi
 +28  47.1   hi
 +29  44.9   hi
 +30  46.2   hi
 +</code>
 +===== ANOVA by hand =====
 +<code>
 +mean.by.group.xs <- tapply(xs$score, xs$temp, mean)
 +var.by.group.xs <- tapply(xs$score, xs$temp, var)
 +n.by.group.xs <- tapply(xs$score, xs$temp, length)
 +df.by.group.xs <- n.xs-1
 +
 +mean.by.group.xs
 +var.by.group.xs
 +n.by.group.xs
 +df.by.group.xs
 +</code>
 +
 +<code>
 +> mean.by.group.xs <- tapply(xs$score, xs$temp, mean)
 +> var.by.group.xs <- tapply(xs$score, xs$temp, var)
 +> n.by.group.xs <- tapply(xs$score, xs$temp, length)
 +> df.by.group.xs <- n.xs-1
 +> ss.within <- sum(var.by.group.xs * df.by.group.xs)
 +
 +> mean.by.group.xs
 +  low   mid    hi 
 +51.46 46.94 46.35 
 +> var.by.group.xs
 +      low       mid        hi 
 +0.4671111 1.0848889 0.6027778 
 +> n.by.group.xs
 +low mid  hi 
 + 10  10  10 
 +> df.by.group.xs
 +low mid  hi 
 +  9     
 +
 +</code>
 +
 +<code>
 +mean.xs <- mean(xs$score)
 +n.total <- length(xs$score)
 +df.total <- n.total-1
 +n.group.xs <- 3
 +df.between <- n.group.xs -1
 +df.within <- sum(df.by.group.xs)
 +n.total
 +df.total
 +df.between
 +df.within
 +
 +ss.total <- var(xs$score) * (length(xs$score)-1)
 +ss.total <- var(xs$score) * df.total
 +ss.between <- sum(n.by.group.xs * (mean.by.group.xs - mean.xs)^2)
 +ss.within <- sum(var.by.group.xs * df.by.group.xs)
 +ss.total
 +ss.between
 +ss.within
 +ss.total 
 +ss.between + ss.within
 +
 +ms.between <- ss.between/df.between
 +ms.within <- ss.within/df.within
 +ms.total <- ss.total/df.total
 +
 +ms.total
 +ms.between
 +ms.within
 +
 +f.calculated <- ms.between/ms.within
 +f.calculated
 +var(xs$score)
 +</code>
 +
 +
 +<code>
 +> mean.xs <- mean(xs$score)
 +> n.total <- length(xs$score)
 +> df.total <- n.total-1
 +> n.group.xs <- 3
 +> df.between <- n.group.xs -1
 +> df.within <- sum(df.by.group.xs)
 +> n.total
 +[1] 30
 +> df.total
 +[1] 29
 +> df.between
 +[1] 2
 +> df.within
 +[1] 27
 +
 +> ss.total <- var(xs$score) * (length(xs$score)-1)
 +> ss.total <- var(xs$score) * df.total
 +> ss.between <- sum(n.by.group.xs * (mean.by.group.xs - mean.xs)^2)
 +> ss.within <- sum(var.by.group.xs * df.by.group.xs)
 +> ss.total
 +[1] 175.695
 +> ss.between
 +[1] 156.302
 +> ss.within
 +[1] 19.393
 +> ss.total 
 +[1] 175.695
 +> ss.between + ss.within
 +[1] 175.695
 +
 +> ms.between <- ss.between/df.between
 +> ms.within <- ss.within/df.within
 +> ms.total <- ss.total/df.total
 +
 +> ms.total
 +[1] 6.058448
 +> ms.between
 +[1] 78.151
 +> ms.within
 +[1] 0.7182593
 +
 +> f.calculated <- ms.between/ms.within
 +> f.calculated
 +[1] 108.8061
 +> var(xs$score)
 +[1] 6.058448
 +
 +</code>
 +===== ANOVA function (aov) =====
 +
 +<code>
 +x.mod <- aov(score~temp, data=xs)
 +x.mod
 +summary(x.mod)
 +TukeyHSD(x.mod)
 +</code>
 +
 <code> <code>
-> x.mod <- aov(values~ind,data=xs)+> x.mod <- aov(score~temp, data=xs)
 > x.mod > x.mod
 Call: Call:
-   aov(formula = values ind, data = xs)+   aov(formula = score temp, data = xs)
  
 Terms: Terms:
-                    ind Residuals+                   temp Residuals
 Sum of Squares  156.302    19.393 Sum of Squares  156.302    19.393
 Deg. of Freedom              27 Deg. of Freedom              27
Line 86: Line 307:
 > summary(x.mod) > summary(x.mod)
             Df Sum Sq Mean Sq F value  Pr(>F)                 Df Sum Sq Mean Sq F value  Pr(>F)    
-ind          2 156.30   78.15   108.8 1.2e-13 ***+temp         2 156.30   78.15   108.8 1.2e-13 ***
 Residuals   27  19.39    0.72                     Residuals   27  19.39    0.72                    
 --- ---
Line 96: Line 317:
     95% family-wise confidence level     95% family-wise confidence level
  
-Fit: aov(formula = values ind, data = xs)+Fit: aov(formula = score temp, data = xs)
  
-$ind +$temp 
-       diff       lwr        upr     p adj +         diff       lwr        upr     p adj 
-x2-x1 -4.52 -5.459735 -3.5802652 0.0000000 +mid-low -4.52 -5.459735 -3.5802652 0.0000000 
-x3-x1 -5.11 -6.049735 -4.1702652 0.0000000 +hi-low  -5.11 -6.049735 -4.1702652 0.0000000 
-x3-x2 -0.59 -1.529735  0.3497348 0.2813795 +hi-mid  -0.59 -1.529735  0.3497348 0.2813795
- +
-</code> +
- +
-<code>> tapply(xs$values, xs$ind, mean) +
-   x1    x2    x3  +
-51.46 46.94 46.35 </code> +
- +
- +
-SS<sub>between</sub> +
-<code>> meanxs <- mean(xs$values) +
-> mx1 <- mean(x1) +
-> mx2 <- mean(x2) +
-> mx3 <- mean(x3) +
- +
-> x2ss <- 10*((meanxs - mx2)^2) +
-> x3ss <- 10*((meanxs - mx3)^2) +
-> x1ss <- 10*((meanxs - mx1)^2) +
-> xss <- x1ss+x2ss+x3ss +
-> xss +
-[1] 156.302 +
-</code>+
  
 +> </code>
  
 ====== E.g. 1 ====== ====== E.g. 1 ======
r/oneway_anova.1543860971.txt.gz · Last modified: 2018/12/04 03:16 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki