Y = Tooth length: continuous normally distributed
str(ToothGrowth)
## 'data.frame': 60 obs. of 3 variables:
## $ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
## $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
## $ dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
hist(ToothGrowth$len)
X = Dosage: keeping as numerical
Test to use: ANOVA
There is a statistically significant difference in tooth length with dose (F(1, 58) = 105.1, p < 0.001), and this is a quite strong linear relationship (R²=0.64).
len_model <- lm(len ~ dose, data = ToothGrowth)
summary(len_model)
##
## Call:
## lm(formula = len ~ dose, data = ToothGrowth)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.4496 -2.7406 -0.7452 2.8344 10.1139
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.4225 1.2601 5.89 2.06e-07 ***
## dose 9.7636 0.9525 10.25 1.23e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.601 on 58 degrees of freedom
## Multiple R-squared: 0.6443, Adjusted R-squared: 0.6382
## F-statistic: 105.1 on 1 and 58 DF, p-value: 1.233e-14
#plotting the results
r2 <- summary(len_model)$r.squared
ggplot(ToothGrowth, aes(x = dose, y = len, color = supp)) +
geom_point(position = position_jitter(width = 0.05), size = 2, alpha = 0.7) + geom_smooth(method = "lm", se = FALSE, color = "red")+
labs( x = "Dose (mg/day)", y = "Tooth Length (cm)",
color = "Supplement",
caption = paste0(
"R² = ", round(r2, 2)
)) +
theme_minimal()
Y = Tooth length
X1 = dose (continuous)
X2 = supplement (2 levels)
Test: 2-way ANOVA
model_lm <- lm(len ~ supp * dose, data = ToothGrowth)
anova(model_lm)
## Analysis of Variance Table
##
## Response: len
## Df Sum Sq Mean Sq F value Pr(>F)
## supp 1 205.35 205.35 12.3170 0.0008936 ***
## dose 1 2224.30 2224.30 133.4151 < 2.2e-16 ***
## supp:dose 1 88.92 88.92 5.3335 0.0246314 *
## Residuals 56 933.63 16.67
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Dose: extremely statistically significant (F(1,56) =133.415, p < 0.001) as tooth length differs significantly among the three dose levels
Supplement: There is an overall difference in tooth length between the two supplements (F(1,56) = 12.317, p < 0.001)
Supp:dose - The effect of supplement depends on the dose level is statistically significant with a (F(1,56) = 5.334, p = 0.025)
interaction.plot(x.factor = ToothGrowth$dose,
trace.factor = ToothGrowth$supp,
response = ToothGrowth$len,
type = "b", col = c("red","darkturquoise"),
xlab = "Dose", ylab = "Mean Tooth Length",
trace.label = "Supplement")