This exercise repeats several of the tests used in Module 9

Step one: Run a t-test to compare the Legolas actors to the set of Aragorns and then the set of Gimlis. Do you find evidence for significant differences?

aragorn = rnorm(50, mean=180, sd=10)
gimli = rnorm(50, mean=132, sd=15)
legolas = rnorm(50, 195, 15)

T-test of Legolas and Aragorn actors

Null H0: Legolas and Aragorn actors have the same height

Alternative Ha: Legolas and Aragorn actors have different heights

t.test(legolas,aragorn, alternative="two.sided")
## 
##  Welch Two Sample t-test
## 
## data:  legolas and aragorn
## t = 6.2088, df = 88.384, p-value = 1.694e-08
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  10.38971 20.17087
## sample estimates:
## mean of x mean of y 
##  194.3023  179.0220

The low p-value (p-value = 1.257e-07) means we can reject our null hypothesis, suggesting that the difference in heights between Legolas and Aragorn actors is statistically significant

T-test of Legolas and Gimli actors

Null H0: Legolas and Gimli actors have the same height

Alternative Ha: Legolas and Gimli actors have different heights

t.test(legolas,gimli, alternative="two.sided")
## 
##  Welch Two Sample t-test
## 
## data:  legolas and gimli
## t = 21.931, df = 97.977, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  56.18058 67.35916
## sample estimates:
## mean of x mean of y 
##  194.3023  132.5324

The low p-value (p-value = < 2.2e-16) means we can reject our null hypothesis, suggesting that the difference in heights between Legolas and Gimli actors is statistically significant

Step two: Re-run the variance test (F-test) to compare the group of Gimli and Legolas actors. Do these groups have different variance?

var.test(gimli,legolas)
## 
##  F test to compare two variances
## 
## data:  gimli and legolas
## F = 0.9697, num df = 49, denom df = 49, p-value = 0.9147
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.5502824 1.7087965
## sample estimates:
## ratio of variances 
##          0.9697013

Comparing Gimli and Legolas actor heights using an F-test returned a p-value above 0.05 (p-value = 0.8395), which suggest there is not statistically significant difference in variance

Step three: Redo the correlation for the Sepal Length and Sepal Width for the Iris dataset, but for the three individual species. Are these correlated?

iris <- read.csv("iris.csv")

setosa_cor_test <- cor.test(iris[iris$Species == "setosa", "Sepal.Length"],
                            iris[iris$Species == "setosa", "Sepal.Width"])
setosa_cor_test
## 
##  Pearson's product-moment correlation
## 
## data:  iris[iris$Species == "setosa", "Sepal.Length"] and iris[iris$Species == "setosa", "Sepal.Width"]
## t = 7.6807, df = 48, p-value = 6.71e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.5851391 0.8460314
## sample estimates:
##       cor 
## 0.7425467

Using a correlation test comparing Sepal Length and Sepal Width for the setosa species, the p-value is below 0.05 (p-value = 6.71e-10), which suggests there is statistically significant difference

versicolor_cor_test <- cor.test(iris[iris$Species == "versicolor", "Sepal.Length"],
                                iris[iris$Species == "versicolor", "Sepal.Width"])
versicolor_cor_test
## 
##  Pearson's product-moment correlation
## 
## data:  iris[iris$Species == "versicolor", "Sepal.Length"] and iris[iris$Species == "versicolor", "Sepal.Width"]
## t = 4.2839, df = 48, p-value = 8.772e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2900175 0.7015599
## sample estimates:
##       cor 
## 0.5259107

For the versicolor species, p-value is below 0.05 (p-value = 8.772e-05), which suggests there is statistically significant difference

virginica_cor_test <- cor.test(iris[iris$Species == "virginica", "Sepal.Length"],
                               iris[iris$Species == "virginica", "Sepal.Width"])
virginica_cor_test
## 
##  Pearson's product-moment correlation
## 
## data:  iris[iris$Species == "virginica", "Sepal.Length"] and iris[iris$Species == "virginica", "Sepal.Width"]
## t = 3.5619, df = 48, p-value = 0.0008435
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2049657 0.6525292
## sample estimates:
##       cor 
## 0.4572278

For the virginica species, p-value is below 0.05 (p-value = 0.0008435), which suggests there is statistically significant difference

Step four: Using the deer dataset and the chisq.test() function, test:

  • If there are significant differences in the number of deer caught per month
deer <- read.csv("Deer.csv")

deer_caught_per_month <- table(deer$Month)
chisq.test(deer_caught_per_month)
## 
##  Chi-squared test for given probabilities
## 
## data:  deer_caught_per_month
## X-squared = 997.07, df = 11, p-value < 2.2e-16

The p-value is below 0.05 (p-value = < 2.2e-16), which suggests there is statistically significant difference in the number of deer caught per month

  • If the cases of tuberculosis are uniformly distributed across all farms
deer_tbcounts_per_farm <- table(deer$Farm[deer$Tb == 1])
chisq.test(deer_tbcounts_per_farm)
## 
##  Chi-squared test for given probabilities
## 
## data:  deer_tbcounts_per_farm
## X-squared = 189.78, df = 17, p-value < 2.2e-16

The p-value is below 0.05 (p-value = < 2.2e-16), which suggests there is statistically significant difference in the tuberculosis counts across all farms, which means the cases are not uniformly distributed across all farms