library(pwr)
## Warning: package 'pwr' was built under R version 4.1.3
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.1.3
pwr.anova.test(k = 4, n = NULL, f = sqrt(1/4.5), sig.level = 0.05, power = 0.8)
##
## Balanced one-way analysis of variance power calculation
##
## k = 4
## n = 13.28401
## f = 0.4714045
## sig.level = 0.05
## power = 0.8
##
## NOTE: n is number in each group
conclusion: For 4 groups, significance level of 0.05, power of 0.8, we need to collect appx 14 samples.
pwr.anova.test(k = 4, n = NULL, f = sqrt(0.5/4.5), sig.level = 0.05, power = 0.8)
##
## Balanced one-way analysis of variance power calculation
##
## k = 4
## n = 25.52904
## f = 0.3333333
## sig.level = 0.05
## power = 0.8
##
## NOTE: n is number in each group
Conclusion: we need approximately 26 samples to be collected to achieve a power of 80% at sig.level of 0.05.
fluid1 <- c(17.6, 18.9, 16.3, 17.4, 20.1, 21.6)
fluid2 <- c(16.9, 15.3, 18.6, 17.1, 19.5, 20.3)
fluid3 <- c(21.4, 23.6, 19.4, 18.5, 20.5, 22.3)
fluid4 <- c(19.3, 21.1, 16.9, 17.5, 18.3, 19.8)
mean(fluid1)
## [1] 18.65
mean(fluid2)
## [1] 17.95
mean(fluid3)
## [1] 20.95
mean(fluid4)
## [1] 18.81667
var(fluid1)
## [1] 3.811
var(fluid2)
## [1] 3.439
var(fluid3)
## [1] 3.531
var(fluid4)
## [1] 2.417667
fluid <- data.frame(fluid1, fluid2, fluid3, fluid4)
fluid <- pivot_longer(fluid, c(fluid1, fluid2, fluid3, fluid4))
pwr.anova.test(k = 4, n = 6, f = sqrt(1/4.5), sig.level = 0.1, power = NULL)
##
## Balanced one-way analysis of variance power calculation
##
## k = 4
## n = 6
## f = 0.4714045
## sig.level = 0.1
## power = 0.5334697
##
## NOTE: n is number in each group
For 6 samples, 4 groups, with a level of significance of 0.1, difference between mean of 1 hour, variance of 4.5 hours (corrected or given in the class), we get a power of 53% for the test.
Hypothesis testing for ANOVA:
Ho: u1 = u2 = u3 =u4 = u
Ha: At least one ui is different
anova <- aov(value~name, data = fluid)
summary(anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## name 3 30.16 10.05 3.047 0.0525 .
## Residuals 20 65.99 3.30
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
From the results of ANOVA, we see that our p value is 0.05 < 0.1, hence we reject the null hypothesis that all the means are equal.
plot(anova)
## hat values (leverages) are all = 0.1666667
## and there are no factor predictors; no plot no. 5
From the normal qq plot we see that residuals are fairly normal. From the residuals vs fitted plot we can assume that the variances are not that different (at least visually). Then, the model obeys the two fundamental assumptions of ANOVA--(a) constant variance (b) normality.
So the model is adequate.
tukey <- TukeyHSD(anova, conf.level = 0.90)
tukey
## Tukey multiple comparisons of means
## 90% family-wise confidence level
##
## Fit: aov(formula = value ~ name, data = fluid)
##
## $name
## diff lwr upr p adj
## fluid2-fluid1 -0.7000000 -3.2670196 1.8670196 0.9080815
## fluid3-fluid1 2.3000000 -0.2670196 4.8670196 0.1593262
## fluid4-fluid1 0.1666667 -2.4003529 2.7336862 0.9985213
## fluid3-fluid2 3.0000000 0.4329804 5.5670196 0.0440578
## fluid4-fluid2 0.8666667 -1.7003529 3.4336862 0.8413288
## fluid4-fluid3 -2.1333333 -4.7003529 0.4336862 0.2090635
plot(tukey)
From the familywise comparison using the Tukey’s test, we see that group 2 and 3 has p-value < 0.1. Also from the 90% confidence interval plot, we see that group2-3 do not have 0 in their interval. Rest of all the groups have 0 in their conf.interval.
Hence we conclude that only group 2 and group 3 are significantly different from each other.
#library(pwr)
?pwr.anova.test()
pwr.anova.test(k = 4, n = NULL, f = sqrt(1/4.5), sig.level = 0.05, power = 0.8)
pwr.anova.test(k = 4, n = NULL, f = sqrt(0.5/4.5), sig.level = 0.05, power = 0.8)
#library(tidyr)
fluid1 <- c(17.6, 18.9, 16.3, 17.4, 20.1, 21.6)
fluid2 <- c(16.9, 15.3, 18.6, 17.1, 19.5, 20.3)
fluid3 <- c(21.4, 23.6, 19.4, 18.5, 20.5, 22.3)
fluid4 <- c(19.3, 21.1, 16.9, 17.5, 18.3, 19.8)
mean(fluid1)
mean(fluid2)
mean(fluid3)
mean(fluid4)
var(fluid1)
var(fluid2)
var(fluid3)
var(fluid4)
fluid <- data.frame(fluid1, fluid2, fluid3, fluid4)
fluid <- pivot_longer(fluid, c(fluid1, fluid2, fluid3, fluid4))
View(fluid)
pwr.anova.test(k = 4, n = 6, f = sqrt(1/4.5), sig.level = 0.1, power = NULL)
anova <- aov(value~name, data = fluid)
summary(anova)
plot(anova)
?TukeyHSD
tukey <- TukeyHSD(anova, conf.level = 0.90)
plot(tukey)