R Markdown

#Simple inference tests

Set the working directory

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 for reproducibility

set.seed(123)

Generate random heights for Aragorn actors

aragorn = rnorm(50, mean=180, sd=10)

Generate random heights for Legolas actors

legolas = rnorm(50, 195, 15)

Perform two-sided t-test to compare Legolas and Aragorn actors

t_test_legolas_aragorn = t.test(legolas, aragorn, alternative=“two.sided”)

Display t-test result

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.

Generate random heights for Gimli actors

gimli = rnorm(50, mean=132, sd=15)

Perform two-sided t-test to compare Legolas and Gimli actors

t_test_legolas_gimli = t.test(legolas, gimli, alternative=“two.sided”)

Display t-test result

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.

Perform F-test to compare variances between Gimli and Legolas actors

f_test_gimli_legolas = var.test(gimli, legolas)

Display F-test result

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.

Load the iris dataset

data(iris)

Get unique species in the dataset

species = unique(iris$Species)

Function to calculate correlation for each 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)) })

Combine results into a single dataframe

cor_results = do.call(rbind, cor_results)

Display correlation 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.

Load the deer dataset

deer = read.csv(“Deer.csv”)

Perform chi-squared test to check for significant differences in the number of deer caught per month

chisq_test_month = chisq.test(table(deer$Month))

Display chi-squared test result for 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.

Perform Fisher’s Exact Test to check if tuberculosis cases are uniformly distributed across farms

Using simulate.p.value=TRUE due to large problem size

fisher_test_tb_farms = fisher.test(table(deer\(Farm, deer\)Tb), simulate.p.value=TRUE)

Display Fisher’s Exact Test result for tuberculosis distribution

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.