In this project, students will demonstrate their understanding of the inference on categorical data. There will also be inference on numerical data mixed in and the student is expected to identify the proper type of inference to apply depending on the variable type. If not specifically mentioned, students should assume a significance level of 0.05.
The project will use two datasets from the internet – atheism and nscc_student_data. Store the atheism dataset in your environment by using the following R chunk. Do some exploratory analysis using the str() function and viewing the dataframe. None of this will be graded, just something for you to do on your own.
# Download atheism dataset from web
download.file("http://s3.amazonaws.com/assets.datacamp.com/course/dasi/atheism.RData", destfile = "atheism.RData")
# Load dataset into environment
load("atheism.RData")
Load the “nscc_student_data.csv” file in the following R chunk below and refamiliarize yourself with this dataset as well.
# load and store the "nscc_student_data.csv" file
nscc_student_data <- read.csv("~/Desktop/nscc_student_data.csv")
In the 2010 playoffs, the National Football League (NFL) changed their overtime rules amid concerns that whichever team won an overtime coin toss (by luck) had a significant advantage to win the game. Nicholas Gorgievski, et al, published research in 2010 that stated that out of 414 games won in overtime up to that point, 235 were won by the team that won the coin toss. Test the claim that the team which wins the coin flip wins more often than its opponent.
(Hint: You must recognize what percent of games you’d expect a team to win if they do not win any more or less than their opponent.)
Two tailed
# Calculate sample proportion
(p_hat1 <- 235/414)
## [1] 0.5676329
# SE
(se1 <- sqrt(0.5*(1-0.5) / 414))
## [1] 0.02457366
# Test statistic
(ts1 <- (p_hat1 - 0.5) / se1)
## [1] 2.75225
# p-value
pnorm(abs(ts1), lower.tail = FALSE)*2
## [1] 0.005918735
The p-value is 0.005918735 and it is less than \(\alpha\), we reject \(H_0\).
For questions 2 and 3, consider the atheism dataset loaded at the beginning of the project. An atheism index is defined as the percent of a population that identifies as atheist. Is there convincing evidence that Spain has seen a change in its atheism index from 2005 to 2012?
# Create subsets for Spain 2005 and 2012
spain2005 <- subset(atheism, nationality == "Spain" & year == 2005)
spain2012 <- subset(atheism, nationality == "Spain" & year == 2012)
Two tailed.
# Finding x1 and n1, x2 and n2
table(spain2005$response)
##
## atheist non-atheist
## 115 1031
table(spain2012$response)
##
## atheist non-atheist
## 103 1042
# storing values
x1 <- 115
n1 <- 115 + 1031
x2 <- 103
n2 <- 103 + 1042
# p_pool
(p_pool1 <- (x1 + x2) / (n1 + n2))
## [1] 0.09515495
# Standard error
(se2 <- sqrt((p_pool1*(1-p_pool1) / n1) + (p_pool1*(1-p_pool1) / n2)))
## [1] 0.01226084
# Test statistic
(ts2 <- (((x1/n1) - (x2/n2)) / se2))
## [1] 0.8476341
# p-value
pnorm(ts2, lower.tail = FALSE)*2
## [1] 0.3966418
The p-value is 0.3966418, we cannot reject the null hypothesis (\(H_0\)).
Is there convincing evidence that the United States has seen a change in its atheism index from 2005 to 2012?
# Create subsets for USA 2005 and 2012
USA2005 <- subset(atheism, nationality == "United States" & year == 2005)
USA2012 <- subset(atheism, nationality == "United States" & year == 2012)
Two tailed.
# Finding x_1 and n_1, x_2 and n_2
table(USA2005$response)
##
## atheist non-atheist
## 10 992
table(USA2012$response)
##
## atheist non-atheist
## 50 952
# storing values
x_1 <- 10
n_1 <- 10 + 992
x_2 <- 50
n_2 <- 50 + 952
# p_pool
(p_pool2 <- (x_1 + x_2) / (n_1 + n_2))
## [1] 0.02994012
# Standard error
(se3 <- sqrt((p_pool2*(1-p_pool2) / n_1) + (p_pool2*(1-p_pool2) / n_2)))
## [1] 0.0076139
# test statistic
(ts3 <- (((x_1/n_1) - (x_2/n_2)) / se3))
## [1] -5.243063
# p-value
pnorm(ts3)*2
## [1] 1.579324e-07
The p-value is 1.579324e-07 and it is a small value, Therefore we reject the \(H_0\).
Suppose you’re hired by the local government to estimate the proportion of residents in your state that attend a religious service on a weekly basis. According to the guidelines, the government desires a 95% confidence interval with a margin of error no greater than 3%. You have no idea what to expect for \(\hat{p}\). How many people would you have to sample to ensure that you are within the specified margin of error and confidence level?
# Calculate minimum sample size 95% CL
(z <- qnorm(0.025, lower.tail = FALSE))
## [1] 1.959964
# Number of people to sample
z^2*0.5*0.5/(0.03^2)
## [1] 1067.072
The sample size would have to be at least 1067 people.
Use the NSCC Student Dataset for the Questions 5-7.
Construct a 95% confidence interval of the true proportion of all NSCC students that are registered voters.
# Using the table function to find the sample size
table(nscc_student_data$VoterReg)
##
## No Yes
## 9 31
The sample size is 40.
# Storing data
(registeredvoters <- 31 / 40)
## [1] 0.775
# Standard error
(se4 <- sqrt((registeredvoters)*(1 - registeredvoters) / 40))
## [1] 0.06602556
# Lower bound
registeredvoters - 1.96 * se4
## [1] 0.6455899
# Upper bound
registeredvoters + 1.96 * se4
## [1] 0.9044101
We are 95% confident that the number of student at NSCC that are registered to vote is between 65% and 90%.
Construct a 95% confidence interval of the average height of all NSCC students.
# Finding mean and standard deviation
(mn <- mean(nscc_student_data$Height, na.rm = TRUE))
## [1] 64.52436
(sd <- sd(nscc_student_data$Height, na.rm = TRUE))
## [1] 10.60386
# Lower bound
mn - 1.96 * sd / sqrt(40)
## [1] 61.23819
# Upper bound
mn + 1.96 * sd / sqrt(40)
## [1] 67.81053
We are 95% confident that the average height of all NSCC students is between 61.23 and 67.81 inches.
Starbucks is considering opening a coffee shop on NSCC Danvers campus if they believe that more NSCC students drink coffee than the national proportion. A Gallup poll in 2015 found that 64% of all Americans drink coffee. Conduct a hypothesis test to determine if more NSCC students drink coffee than other Americans.
a.) Write hypotheses and determine tails of the test
\(H_0: p = 0.64\)
\(H_A: p > 0.64\)
One tailed
b.) Calculate sample statistics
# Sample size
table(nscc_student_data$Coffee)
##
## No Yes
## 10 30
# sample proportion
(p_hat2 <- 30/40)
## [1] 0.75
# Standard error
(se5 <- sqrt(0.64 * (1 - 0.64) / 40))
## [1] 0.07589466
# Test statistic
(ts5 <- (p_hat2 - 0.64) / se5)
## [1] 1.449377
# p-value
pnorm(ts5, lower.tail = FALSE)
## [1] 0.07361613
c.) Determine probability of getting sample data by chance and use that to reject Ho or fail to reject Ho
The p-value is equal to 0.07361613.
p-value is greater than \(\alpha\), we fail to reject \(H_0\).
d.) Conclusion
There is not enough evidence to suggest that NSCC students drink more coffee than Americans in general.