Part 1: t-tests
# Simulate actor heights
set.seed(123)
legolas <- rnorm(50, mean = 178, sd = 8)
aragorn <- rnorm(50, mean = 180, sd = 10)
gimli <- rnorm(50, mean = 160, sd = 7)
# t-test: Legolas vs Aragorn
t.test(legolas, aragorn)
##
## Welch Two Sample t-test
##
## data: legolas and aragorn
## t = -1.9275, df = 94.296, p-value = 0.05692
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -6.47350426 0.09579568
## sample estimates:
## mean of x mean of y
## 178.2752 181.4641
# t-test: Legolas vs Gimli
t.test(legolas, gimli)
##
## Welch Two Sample t-test
##
## data: legolas and gimli
## t = 13.983, df = 97.56, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 17.20657 22.89849
## sample estimates:
## mean of x mean of y
## 178.2752 158.2227
Part 2: Variance (F) Test
# Variance comparison between Gimli and Legolas
var.test(gimli, legolas)
##
## F test to compare two variances
##
## data: gimli and legolas
## F = 0.87418, num df = 49, denom df = 49, p-value = 0.6397
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.4960775 1.5404736
## sample estimates:
## ratio of variances
## 0.8741821
Part 3: Correlation in Iris Dataset
# Load iris data
iris <- read.csv("iris.csv")
# Correlation: Setosa
cor.test(iris$Sepal.Length[iris$Species == "setosa"],
iris$Sepal.Width[iris$Species == "setosa"])
##
## Pearson's product-moment correlation
##
## data: iris$Sepal.Length[iris$Species == "setosa"] and iris$Sepal.Width[iris$Species == "setosa"]
## 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
# Correlation: Versicolor
cor.test(iris$Sepal.Length[iris$Species == "versicolor"],
iris$Sepal.Width[iris$Species == "versicolor"])
##
## Pearson's product-moment correlation
##
## data: iris$Sepal.Length[iris$Species == "versicolor"] and iris$Sepal.Width[iris$Species == "versicolor"]
## 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
# Correlation: Virginica
cor.test(iris$Sepal.Length[iris$Species == "virginica"],
iris$Sepal.Width[iris$Species == "virginica"])
##
## Pearson's product-moment correlation
##
## data: iris$Sepal.Length[iris$Species == "virginica"] and iris$Sepal.Width[iris$Species == "virginica"]
## 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
Part 4: Chi-Square Tests (Deer Dataset)
# Load deer data
deer <- read.csv("Deer.csv")
# Test: Deer counts by Month
table_month <- table(deer$Month)
chisq.test(table_month)
##
## Chi-squared test for given probabilities
##
## data: table_month
## X-squared = 997.07, df = 11, p-value < 2.2e-16
# Test: TB cases by Farm
table_tb <- table(deer$Farm, deer$Tb)
chisq.test(table_tb)
## Warning in chisq.test(table_tb): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: table_tb
## X-squared = 129.09, df = 26, p-value = 1.243e-15