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.
nscc_student_data <- read.csv("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.)
Write hypotheses and determine tails of the test
\(H_0\): \(\mu\) = 0.5 \(H_A\): \(\mu\) \(\neq\) 0.5 two tailed test
Find p-value of sample data and use to make decision to reject H0 or fail to reject H0
# Calculate sample proportion of games won by coin flip team
PHAT <- 235/414
# Standard Error
p <- 0.5
se <- sqrt(p*(1-p)/414)
# Test stat
testStatNFL <- (PHAT - p)/se
# p-value
pnorm(testStatNFL, lower.tail = FALSE)*2
## [1] 0.005918735
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)
Write hypotheses and determine tails of the test \(H_0\): \(p_1\) = \(p_2\) \(H_A\): \(p_1\) \(\neq\) \(p_2\)
Find p-value and use to make decision to reject H0 or fail to reject H0
# table function to find atheism response values
table(spain2005$response)
##
## atheist non-atheist
## 115 1031
table(spain2012$response)
##
## atheist non-atheist
## 103 1042
# Store values
x1 <- 115
x2 <- 103
n1 <- 115 + 1031
n2 <- 103 + 1042
# xxnn1
xxnn1 <- (x1 + x2) / (n1 + n2)
# Standard Error
SEsp <- sqrt((xxnn1 * (1 - xxnn1) / n1) + (xxnn1*(1 - xxnn1) / n2))
# Test stat
p1 <- x1 / n1
p2 <- x2 / n2
testStatSp <- (p1-p2) / SEsp
testStatSp
## [1] 0.8476341
# p-value
pnorm(-testStatSp) * 2
## [1] 0.3966418
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)
Write hypotheses and determine tails of the test \(H_0\): \(p_1\) - \(p_2\) = 0 \(H_A\): \(p_1\) - \(p_2\) \(\neq\) 0
Find p-value and use to make decision to reject H0 or fail to reject H0
# table function to find atheism repsonse values
table(USA2005$response)
##
## atheist non-atheist
## 10 992
table(USA2012$response)
##
## atheist non-atheist
## 50 952
# store values
xx1 <- 10
xx2 <- 50
nn1 <- 10 + 992
nn2 <- 50 + 952
# xxnn2
xxnn2 <- (xx1+xx2)/(nn1+nn2)
# Standard Error
USAse <- sqrt((xxnn2*(1-xxnn2)/nn1)+(xxnn2*(1-xxnn2)/nn2))
# Test stat
pp1 <- xx1 / nn1
pp2 <- xx2 / nn2
testStatUSA <- (pp1-pp2)/USAse
testStatUSA
## [1] -5.243063
# p-value
pnorm(testStatUSA)*2
## [1] 1.579324e-07
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 2%. 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?
1.96^2 * 0.5 * 0.5 / 0.02^2
## [1] 2401
The sample size would need to be at least 2401 people.
Use the NSCC Student Dataset for the Questions 5-8.
Construct a 95% confidence interval of the true proportion of all NSCC students that are registered voters.
# table function to find sample sizes
table(nscc_student_data$VoterReg)
##
## No Yes
## 9 31
# Store data
registered <- 31/40
# Sample Size
sampleSZ <- 40
# Standard Error
seReg <- sqrt((registered) * (1 - registered) / sampleSZ)
# Upper bound of confidence interval
registered + 1.96 * seReg
## [1] 0.9044101
# Lower bound of confidence interval
registered - 1.96 * seReg
## [1] 0.6455899
The data shows that we can be 95% confident that between 65% and 90% of Northshore Students vote.
Construct a 95% confidence interval of the average height of all NSCC students.
# mean
mn <- mean(nscc_student_data$Height, na.rm = TRUE)
# sd
sd <- sd(nscc_student_data$Height, na.rm = TRUE)
# Upper bound of confidence interval
mn + 1.96 * sd / sqrt(40)
## [1] 67.81053
# Lower bound of confidence interval
mn - 1.96 * sd / sqrt(40)
## [1] 61.23819
The data shows that we can be 95% confident that the average height of NSCC students are between 61.2 inches and 67.8 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\): \(\mu\) = 0.64 \(H_A\): \(\mu\) \(\neq\) 0.64
b.) Calculate sample statistics
table(nscc_student_data$Coffee)
##
## No Yes
## 10 30
# prop test
prop.test(x = 30, n = 40, p = 0.64, alternative = "greater", correct = FALSE)
##
## 1-sample proportions test without continuity correction
##
## data: 30 out of 40, null probability 0.64
## X-squared = 2.1007, df = 1, p-value = 0.07362
## alternative hypothesis: true p is greater than 0.64
## 95 percent confidence interval:
## 0.6240271 1.0000000
## sample estimates:
## p
## 0.75
c.) Determine probability of getting sample data by chance and use that to reject Ho or fail to reject Ho
The p-value is 0.07362. This is greater than 0.05, therefore we cannot reject the null hypothesis.
d.) Conclusion
There is not enough evidence in the data to say the percentage of NSCC students who drink coffee is larger than the nation’s average in 2015.