First, we’ll read in the Finches data
finches <- case0201
We first visualize the data
finches$Year <- as.character(finches$Year)
ggplot(finches) + geom_bar(mapping = aes(x = Depth, y = ..prop..,fill = Year), alpha = 0.5, position = "dodge")
ggplot(finches) + geom_density(mapping = aes(x = Depth,fill = Year), alpha = 0.5)
ggplot(finches) + geom_boxplot(mapping = aes(x = Year, y = Depth))
Since we are satisfied that the model assumptions (that the two distributions in 1976 and 1978 are close to normal) are satisfied, we can proceed to compute the p-value
a <- t.test(filter(finches, Year == "1976")$Depth, filter(finches, Year == "1978")$Depth, alternative = "two.sided", var.equal = FALSE)
a$p.value
## [1] 8.739145e-06
If we set alternative
to "one.sided"
, the p-value would be computed by only considering values that are more extreme and positive (if the difference is positive in the first place).
Note that we could have used the formula we saw before and pt
in order to compute this value.