Lawson, John. Design and Analysis of Experiments with R (Chapman & Hall/CRC Texts in Statistical Science) (p. 52). CRC Press. Kindle Edition.
library(daewr)
library(mvtnorm)
library(survival)
library(multcomp)
library(dplyr)
library(agricolae)
id <- rep(c("A", "B", "C", "D"), each = 4)
fac <- factor(rep(c(0.25, 0.5, 0.75, 1), each = 4))
fac
## [1] 0.25 0.25 0.25 0.25 0.5 0.5 0.5 0.5 0.75 0.75 0.75 0.75 1 1
## [15] 1 1
## Levels: 0.25 0.5 0.75 1
ht <- c(11.4, 11.0, 11.3, 9.5,
27.8, 29.2, 26.8, 26.0,
47.6, 47.0, 47.3, 45.5,
61.6, 62.4, 63.0, 63.9)
experiment <- data.frame(id, treatment = fac, response = ht)
Ho: u1 = u2 = u3 = u4 Ha: Atleast one of the means is different # Based on the p-value, we can reject the null hypothesis and state that at least one of the treatment is different than the rest. At the chosen level of significance i.e 0.05, there are significant differences in rise heights based on applied baking powder factors.
mod <- aov(response ~ treatment, data = experiment)
summary(mod)
## Df Sum Sq Mean Sq F value Pr(>F)
## treatment 3 6146 2048.6 1823 3.23e-16 ***
## Residuals 12 13 1.1
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
contrasts(experiment$treatment) <- contr.poly(4)
contrasts(experiment$treatment)
## .L .Q .C
## 0.25 -0.6708204 0.5 -0.2236068
## 0.5 -0.2236068 -0.5 0.6708204
## 0.75 0.2236068 -0.5 -0.6708204
## 1 0.6708204 0.5 0.2236068
summary.lm(aov(response ~ treatment, data = experiment))
##
## Call:
## aov(formula = response ~ treatment, data = experiment)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.4500 -0.7688 0.2375 0.5250 1.7500
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 36.9562 0.2650 139.435 <2e-16 ***
## treatment.L 39.1703 0.5301 73.894 <2e-16 ***
## treatment.Q -0.3875 0.5301 -0.731 0.4788
## treatment.C -1.4031 0.5301 -2.647 0.0213 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.06 on 12 degrees of freedom
## Multiple R-squared: 0.9978, Adjusted R-squared: 0.9973
## F-statistic: 1823 on 3 and 12 DF, p-value: 3.231e-16
# To measure experimental error, which is the difference between observed response for a particular experiment and the long run average of all experiments conducted at the same setting, we mutate the dataset and calculate the experimental error for each group.
experiment %>%
select(id, response) %>%
group_by(id) %>%
mutate(mean_response = mean(response),
exp_error = response - mean_response) -> var_df
var_df
# Variance by treatment factor
# Take note of the variance by treatment factor (specially for treatment B = 0.5 tsp). This is a cause for concern.
var_df %>%
select(id, exp_error) %>%
group_by(id) %>%
summarise(exp_error_var = var(exp_error))
# Total variance
var(mod$residuals)
## [1] 0.8991667
par(mfrow = c(1,2))
plot(mod, which = 1)
plot(mod, which = 2)
fac <- factor(rep(c("Control", "IAA", "ABA", "GA3", "CPPU"), each = 5))
fac
## [1] Control Control Control Control Control IAA IAA IAA
## [9] IAA IAA ABA ABA ABA ABA ABA GA3
## [17] GA3 GA3 GA3 GA3 CPPU CPPU CPPU CPPU
## [25] CPPU
## Levels: ABA Control CPPU GA3 IAA
res <- c(94.7, 96.1, 86.5, 98.5, 94.9,
89.9, 94.0, 99.1, 92.8, 99.4,
96.8, 87.8, 89.1, 91.1, 89.4,
99.1, 95.3, 94.6, 93.1, 95.7,
104.4, 98.9, 98.9, 106.5, 104.8)
res
## [1] 94.7 96.1 86.5 98.5 94.9 89.9 94.0 99.1 92.8 99.4 96.8
## [12] 87.8 89.1 91.1 89.4 99.1 95.3 94.6 93.1 95.7 104.4 98.9
## [23] 98.9 106.5 104.8
experiment <- data.frame(treatment = fac, response = res)
experiment
Ho: u1 = u2 = u3 = u4 = u5 Ha: Atleast one of the means is different # We can reject the null hypothesis of no treatment effect # Based on the p-value, we can reject the null hypothesis and state that at least one of the treatment is different than the rest. At the chosen level of significance i.e 0.05, there are significant differences in the effect of plant growth regulators and spear bud scales on spear elongation in asparagus.
mod <- aov(response ~ treatment, data = experiment)
mod
## Call:
## aov(formula = response ~ treatment, data = experiment)
##
## Terms:
## treatment Residuals
## Sum of Squares 377.4936 270.2680
## Deg. of Freedom 4 20
##
## Residual standard error: 3.676058
## Estimated effects may be unbalanced
summary(mod)
## Df Sum Sq Mean Sq F value Pr(>F)
## treatment 4 377.5 94.37 6.984 0.00109 **
## Residuals 20 270.3 13.51
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tukeys <- TukeyHSD(mod, ordered = T)
tukeys
## Tukey multiple comparisons of means
## 95% family-wise confidence level
## factor levels have been ordered
##
## Fit: aov(formula = response ~ treatment, data = experiment)
##
## $treatment
## diff lwr upr p adj
## Control-ABA 3.30 -3.6571003 10.2571 0.6229509
## IAA-ABA 4.20 -2.7571003 11.1571 0.3973453
## GA3-ABA 4.72 -2.2371003 11.6771 0.2881487
## CPPU-ABA 11.86 4.9028997 18.8171 0.0004696
## IAA-Control 0.90 -6.0571003 7.8571 0.9948496
## GA3-Control 1.42 -5.5371003 8.3771 0.9717058
## CPPU-Control 8.56 1.6028997 15.5171 0.0114471
## GA3-IAA 0.52 -6.4371003 7.4771 0.9993935
## CPPU-IAA 7.66 0.7028997 14.6171 0.0265612
## CPPU-GA3 7.14 0.1828997 14.0971 0.0425233
dunnet <- SNK.test(mod, "treatment", alpha = 0.05)
print(dunnet)
## $statistics
## MSerror Df Mean CV
## 13.5134 20 95.656 3.842997
##
## $parameters
## test name.t ntr alpha
## SNK treatment 5 0.05
##
## $snk
## Table CriticalRange
## 2 2.949998 4.849746
## 3 3.577935 5.882064
## 4 3.958293 6.507367
## 5 4.231857 6.957100
##
## $means
## response std r Min Max Q25 Q50 Q75
## ABA 90.84 3.533129 5 87.8 96.8 89.1 89.4 91.1
## Control 94.14 4.530784 5 86.5 98.5 94.7 94.9 96.1
## CPPU 102.70 3.557387 5 98.9 106.5 98.9 104.4 104.8
## GA3 95.56 2.213143 5 93.1 99.1 94.6 95.3 95.7
## IAA 95.04 4.123469 5 89.9 99.4 92.8 94.0 99.1
##
## $comparison
## NULL
##
## $groups
## response groups
## CPPU 102.70 a
## GA3 95.56 b
## IAA 95.04 b
## Control 94.14 b
## ABA 90.84 b
##
## attr(,"class")
## [1] "group"