library(tidyr)
data <- read.csv("7 Guineapigdata.csv")
print(data)
## len supp dose
## 1 4.2 Pill Dose0.5
## 2 11.5 Pill Dose0.5
## 3 7.3 Pill Dose0.5
## 4 5.8 Pill Dose0.5
## 5 6.4 Pill Dose0.5
## 6 10.0 Pill Dose0.5
## 7 11.2 Pill Dose0.5
## 8 11.2 Pill Dose0.5
## 9 5.2 Pill Dose0.5
## 10 7.0 Pill Dose0.5
## 11 16.5 Pill Dose1
## 12 16.5 Pill Dose1
## 13 15.2 Pill Dose1
## 14 17.3 Pill Dose1
## 15 22.5 Pill Dose1
## 16 17.3 Pill Dose1
## 17 13.6 Pill Dose1
## 18 14.5 Pill Dose1
## 19 18.8 Pill Dose1
## 20 15.5 Pill Dose1
## 21 23.6 Pill Dose2
## 22 18.5 Pill Dose2
## 23 33.9 Pill Dose2
## 24 25.5 Pill Dose2
## 25 26.4 Pill Dose2
## 26 32.5 Pill Dose2
## 27 26.7 Pill Dose2
## 28 21.5 Pill Dose2
## 29 23.3 Pill Dose2
## 30 29.5 Pill Dose2
## 31 15.2 OrangeJuice Dose0.5
## 32 21.5 OrangeJuice Dose0.5
## 33 17.6 OrangeJuice Dose0.5
## 34 9.7 OrangeJuice Dose0.5
## 35 14.5 OrangeJuice Dose0.5
## 36 10.0 OrangeJuice Dose0.5
## 37 8.2 OrangeJuice Dose0.5
## 38 9.4 OrangeJuice Dose0.5
## 39 16.5 OrangeJuice Dose0.5
## 40 9.7 OrangeJuice Dose0.5
## 41 19.7 OrangeJuice Dose1
## 42 23.3 OrangeJuice Dose1
## 43 23.6 OrangeJuice Dose1
## 44 26.4 OrangeJuice Dose1
## 45 20.0 OrangeJuice Dose1
## 46 25.2 OrangeJuice Dose1
## 47 25.8 OrangeJuice Dose1
## 48 21.2 OrangeJuice Dose1
## 49 14.5 OrangeJuice Dose1
## 50 27.3 OrangeJuice Dose1
## 51 25.5 OrangeJuice Dose2
## 52 26.4 OrangeJuice Dose2
## 53 22.4 OrangeJuice Dose2
## 54 24.5 OrangeJuice Dose2
## 55 24.8 OrangeJuice Dose2
## 56 30.9 OrangeJuice Dose2
## 57 26.4 OrangeJuice Dose2
## 58 27.3 OrangeJuice Dose2
## 59 29.4 OrangeJuice Dose2
## 60 23.0 OrangeJuice Dose2
library(car)
## Loading required package: carData
levene_test <- leveneTest(data$len ~ data$dose * data$supp, data = data)
print(levene_test)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 5 1.7086 0.1484
## 54
aov.res <- aov(formula = data$len ~ data$dose * data$supp, data=data)
print(aov.res)
## Call:
## aov(formula = data$len ~ data$dose * data$supp, data = data)
##
## Terms:
## data$dose data$supp data$dose:data$supp Residuals
## Sum of Squares 2426.434 205.350 108.319 712.106
## Deg. of Freedom 2 1 2 54
##
## Residual standard error: 3.631411
## Estimated effects may be unbalanced
summary(aov.res)
## Df Sum Sq Mean Sq F value Pr(>F)
## data$dose 2 2426.4 1213.2 92.000 < 2e-16 ***
## data$supp 1 205.4 205.4 15.572 0.000231 ***
## data$dose:data$supp 2 108.3 54.2 4.107 0.021860 *
## Residuals 54 712.1 13.2
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Conclusions from Levene's test: Becuase the p-value is greater than .05, this means that homogeneity of variance is staified and the following ANOVA results are trustwrothy and reliable.
#Interpretation of P-values: Factor A (Dose): The p-value shows that this factor is significant, showing that dose levels have a significant effect on length.Factor B (Supplement): Becuase the p-vlaue is less than .05 and thus significant, showing that the type of supplement has a significant impact on length. Interaction (Dose × Supplement): Becuase the p-vlaue is less than .05 which means the interaction is significant, suggesting that the relationship between dose and length varies by supplement type.
residuals_std <- rstandard(aov.res)
plot(residuals_std, main = "Standardized Residuals Plot", ylab = "Standardized Residuals", xlab = "Index", pch = 20)
abline(h = 0, col = "red", lty = 2)

qqnorm(residuals_std, main = "Q-Q Plot of Residuals")
qqline(residuals_std, col = "blue", lwd = 2)

shapiro_test <- shapiro.test(residuals_std)
print(shapiro_test)
##
## Shapiro-Wilk normality test
##
## data: residuals_std
## W = 0.98499, p-value = 0.6694
interaction.plot(
x.factor = data$dose,
trace.factor = data$supp,
response = data$len,
xlab = "Dose Levels",
ylab = "Mean Length (len)",
trace.label = "Supplement Type",
col = c("blue", "red"),
lty = 1:2
)

#Interpretations of standardized residuals, QQ-plot and Shapiro-Wilks: In the standarized residuals plot the data isd randomly distributed around the dotted lien or 0, meaning that the data is normal. The data fits along the staight line in teh QQ-plot very well, meaning that the data is normal. Lastly, the shapiro-wilks test p-value is .6694, meaning that the p-value is greater than .05, thus the data is noramlly distributed.
#Interpetation of interaction plot: Becuase the lines are not straight accross and are in fact are sloped upwards, the dose has an affect on the length and in fact, as the dose increases for both supplement types, the avergae tooth length increases. Also, becuase the lines are not completely parralel, the supplement types respond differently at different does. Orange juice outperforms the pill at low doses such as 1 and .5 but they preform equally at a dose of 2, showing that the supplement type certainly does have a significant effect on the length.
grandmean <- mean(data$len)
Ameans <- c(
mean(data[data$dose == "Dose0.5", "len"]),
mean(data[data$dose == "Dose1", "len"]),
mean(data[data$dose == "Dose2", "len"])
)
Bmeans <- c(
mean(data[data$supp == "Pill", "len"]),
mean(data[data$supp == "OrangeJuice", "len"])
)
ABmeans <- c(
mean(data[data$dose == "Dose0.5" & data$supp == "Pill", "len"]),
mean(data[data$dose == "Dose1" & data$supp == "Pill", "len"]),
mean(data[data$dose == "Dose2" & data$supp == "Pill", "len"]),
mean(data[data$dose == "Dose0.5" & data$supp == "OrangeJuice", "len"]),
mean(data[data$dose == "Dose1" & data$supp == "OrangeJuice", "len"]),
mean(data[data$dose == "Dose2" & data$supp == "OrangeJuice", "len"])
)
totalss <- sum((data$len - grandmean)^2)
cellss <- (nrow(data) / length(ABmeans)) * sum((ABmeans - grandmean)^2)
errorss <- totalss - cellss
Afactorss <- (nrow(data) / length(Ameans)) * sum((Ameans - grandmean)^2)
Bfactorss <- (nrow(data) / length(Bmeans)) * sum((Bmeans - grandmean)^2)
interactions <- cellss - Afactorss - Bfactorss
totaldf <- nrow(data) - 1
celldf <- length(ABmeans) - 1
Adf <- length(Ameans) - 1
Bdf <- length(Bmeans) - 1
interactiondf <- Adf * Bdf
errordf <- totaldf - celldf
AMS <- Afactorss / Adf
BMS <- Bfactorss / Bdf
interactionMS <- interactions / interactiondf
errorMS <- errorss / errordf
FA <- AMS / errorMS
FB <- BMS / errorMS
Finteraction <- interactionMS / errorMS
p_value_A <- pf(FA, Adf, errordf, lower.tail = FALSE)
p_value_B <- pf(FB, Bdf, errordf, lower.tail = FALSE)
p_value_interaction <- pf(Finteraction, interactiondf, errordf, lower.tail = FALSE)
list(
FA = FA,
FB = FB,
Finteraction = Finteraction,
p_value_A = p_value_A,
p_value_B = p_value_B,
p_value_interaction = p_value_interaction
)
## $FA
## [1] 91.99996
##
## $FB
## [1] 15.57198
##
## $Finteraction
## [1] 4.106991
##
## $p_value_A
## [1] 4.046291e-18
##
## $p_value_B
## [1] 0.0002311828
##
## $p_value_interaction
## [1] 0.02186027
anova_table <- data.frame(
Source = c("Factor A (Dose)", "Factor B (Supplement)", "Interaction (Dose × Supplement)", "Error", "Total"),
SS = c(Afactorss, Bfactorss, interactions, errorss, totalss),
DF = c(Adf, Bdf, interactiondf, errordf, totaldf),
MS = c(AMS, BMS, interactionMS, errorMS, NA),
F = c(FA, FB, Finteraction, NA, NA),
p_value = c(p_value_A, p_value_B, p_value_interaction, NA, NA)
)
print(anova_table)
## Source SS DF MS F p_value
## 1 Factor A (Dose) 2426.434 2 1213.21717 91.999965 4.046291e-18
## 2 Factor B (Supplement) 205.350 1 205.35000 15.571979 2.311828e-04
## 3 Interaction (Dose × Supplement) 108.319 2 54.15950 4.106991 2.186027e-02
## 4 Error 712.106 54 13.18715 NA NA
## 5 Total 3452.209 59 NA NA NA