library(ggplot2)
library(dplyr)
data("PlantGrowth")
head(PlantGrowth)
## weight group
## 1 4.17 ctrl
## 2 5.58 ctrl
## 3 5.18 ctrl
## 4 6.11 ctrl
## 5 4.50 ctrl
## 6 4.61 ctrl
str(PlantGrowth)
## 'data.frame': 30 obs. of 2 variables:
## $ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
## $ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(PlantGrowth)
## weight group
## Min. :3.590 ctrl:10
## 1st Qu.:4.550 trt1:10
## Median :5.155 trt2:10
## Mean :5.073
## 3rd Qu.:5.530
## Max. :6.310
hist(PlantGrowth$weight)
# bar_centers <- boxplot(weight ~ group,
# data = PlantGrowth,
# main = "Boxplot of Plant Growth against treatment",
# xlab = "treatments",
# ylab = "weight") OR
# plot(PlantGrowth$weight~PlantGrowth$group) -
ggplot(PlantGrowth) + geom_boxplot(aes(x = group, y = weight)) + stat_summary(aes(x = group, y = weight), fun = mean, geom = "point", colour = "red")+ geom_point(aes(x = group, y = weight), colour= "blue")
#create a data.frame object with mean and sd
averages <- PlantGrowth %>%
group_by(group) %>%
summarise(mean = mean(weight), sd = sd(weight))
#plot the basic bar graph with the means and standard deviation error bars
#here the highest average weight is in treatment 2 plants
ggplot(data = averages, aes(x = group, y = mean)) +
geom_col() +
geom_errorbar(ymin = averages$mean-averages$sd, ymax = averages$mean+averages$sd) +
ylim(0, 6) +
ylab("mean weight")
From the graph above, different treatments do affect plant weight, where treatment 2 has the highest mean growth.
model <- lm(weight ~ group, data = PlantGrowth)
par(mfrow = c(2,2))
plot(model)
summary(model)
##
## Call:
## lm(formula = weight ~ group, data = PlantGrowth)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.0710 -0.4180 -0.0060 0.2627 1.3690
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.0320 0.1971 25.527 <2e-16 ***
## grouptrt1 -0.3710 0.2788 -1.331 0.1944
## grouptrt2 0.4940 0.2788 1.772 0.0877 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6234 on 27 degrees of freedom
## Multiple R-squared: 0.2641, Adjusted R-squared: 0.2096
## F-statistic: 4.846 on 2 and 27 DF, p-value: 0.01591
anova(model)
## Analysis of Variance Table
##
## Response: weight
## Df Sum Sq Mean Sq F value Pr(>F)
## group 2 3.7663 1.8832 4.8461 0.01591 *
## Residuals 27 10.4921 0.3886
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
An one-way ANOVA test conducted to compare the effect of the different treatments on plant weight. The one-way ANOVA revealed that there is a statistically significant difference in weight between the 3 groups. (F(2,27) = 4.8461, p-value = 0.01591 < 0,05). We reject the null hypothesis. At least one has a different mean plant weights.