#Simple inference tests
setwd(“/Users/ethanmuhlestein/Creative Cloud Files/PMST homework/GEOG 5800/Module_09”) # Get the current working directory to confirm it has been set correctly getwd() #List the files wihtin the directory to make sure we have all the documents needed list.files()
set.seed(123)
aragorn = rnorm(50, mean=180, sd=10)
legolas = rnorm(50, 195, 15)
t_test_legolas_aragorn = t.test(legolas, aragorn, alternative=“two.sided”)
t_test_legolas_aragorn
#This t-test comparing heights of Legolas and Aragon actors yield a t-value of 7.2495 and a p-value of 1.6593-10. Since the p-value is much less than 0.05, we reject the null hypothesis and conclude that there is a statistically significant difference in mean heights between the actors. The 95% confidence interval for the difference in means is between 12.23 and 21.47.
gimli = rnorm(50, mean=132, sd=15)
t_test_legolas_gimli = t.test(legolas, gimli, alternative=“two.sided”)
t_test_legolas_gimli
#This t-test comparing heights of Legolas and Gimli actors yield a t-value of 24.255 and a p-value less than 2.2e-16. Since the p-value is much less than 0.05, we reject the null hypothesis and conclude that there is a statistically significant difference in mean heights between the actors. The 95% confidence interval for the difference in means is between 63.36 and 74.65.
f_test_gimli_legolas = var.test(gimli, legolas)
f_test_gimli_legolas
#This f-test comparing variances of height of Legolas and Gimli actors yield a f-value of 1.1939 and a p-value of 0.5375. Since the p-value is greater than 0.05, we do not reject the null hypothesis and conclude that there is no statistically significant difference in variances of heights between the actors.
data(iris)
species = unique(iris$Species)
cor_results = lapply(species, function(sp) { # Subset data for the species subset_data = iris[iris$Species == sp, ]
# Calculate correlation between Sepal Length and Sepal Width correlation = cor(subset_data\(Sepal.Length, subset_data\)Sepal.Width)
# Return a dataframe with species and correlation return(data.frame(Species = sp, Correlation = correlation)) })
cor_results = do.call(rbind, cor_results)
cor_results
#The correlations between Sepal Length and Sepal Width for the species in the iris dataset are Setosa 0.7425, Veriscolor 0.5259, Virginica 0.4572. These results show a positive correlation for each species, with Setosa’s having the strongest correlation.
deer = read.csv(“Deer.csv”)
chisq_test_month = chisq.test(table(deer$Month))
chisq_test_month
#The chi-squared test for the number of deer caught per month yields an X-squared value of 997.07 and a p-value less than 2.2e-16. Because the p-value is less than 0.05, we reject the null hypothesis and finalize that there are statistically significant differences in the number of deer caught per month.
fisher_test_tb_farms = fisher.test(table(deer\(Farm, deer\)Tb), simulate.p.value=TRUE)
fisher_test_tb_farms
#The Fisher’s Exact Test for the distribution of tuberculosis cases across farms has a p-value of 0.0004998. Since the p-value is less than 0.005, we reject the null hypothesis and conclude that tuberculosis cases are not uniformly distributed across farms.