Preparation

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 dataset
nscc_student_data <- read.csv("~/Desktop/honorsStats/nscc_student_data.csv")

Question 1 - Single Proportion Hypothesis Test

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.)

  1. Write hypotheses and determine tails of the test

Ho: \(\mu = 0.5\)

Ha:\(\mu \neq 0.5\)
Two-tailed

  1. 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

Teams that won the coin toss in 2010 won the game 56.7% of the time according to Nicholas Gorgievski.

# SE
p <- 0.5
se <- sqrt(p*(1-p)/414)

# Test statistic
(pHat-p)/se
## [1] 2.75225
# p-value
pnorm(2.75225, lower.tail=FALSE)*2
## [1] 0.005918732
  1. State conclusion
    The data suggests that the NFL team that won the overtime coin toss (by luck) did, in fact, have a significant advantage to win the game.

Question 2 - Two Independent Proportions Hypothesis Test

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)
  1. Write hypotheses and determine tails of the test

\(H_0: p_1 = p_2\)

\(H_A: p_1 \neq p_2\)

  1. Find p-value and use to make decision to reject H0 or fail to reject H0
# Use table() to get x1, x2, n1, and n2
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

# p-pool
ppool <- (x1+x2)/(n1+n2) 
# SE
se <- sqrt((ppool*(1-ppool)/n1)+(ppool*(1-ppool)/n2))

# Test statistic
p1 <- x1/n1
p2 <- x2/n2
(p1-p2)/se
## [1] 0.8476341
# p-value
pnorm(-0.8476341)*2
## [1] 0.3966418

The p-value is 0.396: cannot reject null hypothesis.

  1. State conclusion

There is not sufficient evidence that there is a difference between the atheism index in the years 2005 and 2012 in Spain.


Question 3 - Two Independent Proportions Hypothesis Test

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)
  1. Write hypotheses and determine tails of the test

\(H_0: p_1-p_2=0\)
\(H_A: p_1-p_2 \neq 0\)

  1. Find p-value and use to make decision to reject H0 or fail to reject H0
# Use table() to get x1, x2, n1, and n2
table(USA2005$response)
## 
##     atheist non-atheist 
##          10         992
table(USA2012$response)
## 
##     atheist non-atheist 
##          50         952
# Store values
x1 <- 10
x2 <- 50
n1 <- 10+992
n2 <- 50+952

# p-pool
ppool <- (x1+x2)/(n1+n2) 
# SE
se <- sqrt((ppool*(1-ppool)/n1)+(ppool*(1-ppool)/n2))

# Test statistic
p1 <- x1/n1
p2 <- x2/n2
(p1-p2)/se
## [1] -5.243063
# p-value
pnorm(-5.243063)*2
## [1] 1.579326e-07

The p-value is 1.5e-07 which is tiny: reject the null hypothesis.

  1. State conclusion

The data suggests there was a different atheist index in the U.S. in 2005 and 2012.


Question 4 - Minimum Sample Size

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 is the z-score for a 95% confidence interval
# 0.02 represents the 2 percentage points
1.96^2*0.5*0.5/0.02^2
## [1] 2401

The sample size for this situation would have to consist of at least 2,401 people.


Question 5

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.

# Sample proportion of NSCC students registered to vote
table(nscc_student_data$VoterReg)
## 
##  No Yes 
##   9  31
# Store data for the number of registered voters divided by the sample size
registered <- 31/40

ss <- 40

# Calculate SE
se <- sqrt((registered)*(1-registered)/ss)
# Upper bound of confidence interval
registered+1.96*se
## [1] 0.9044101
# Lower bound of confidence interval
registered-1.96*se
## [1] 0.6455899

We can be 95% confident that the percentage of students at NSCC that are registered to vote is between 65% and 90%.


Question 6

Construct a 95% confidence interval of the average height of all NSCC students.

# We need the mean and standard deviation
mn <- mean(nscc_student_data$Height, na.rm = TRUE)

stdev <- sd(nscc_student_data$Height, na.rm = TRUE)

# Upper bound of confidence interval
mn + 1.96 * stdev / sqrt(40)
## [1] 67.81053
# Lower bound of confidence interval
mn - 1.96 * stdev / sqrt(40)
## [1] 61.23819

We can be 95% confident that the average height of NSCC students is between 61.2 inches and 67.8 inches.


Question 7

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

Ho: \(\mu = 0.64\) Ha:\(\mu > 0.64\)

b.) Calculate sample statistics

# A table will tell us the number of coffee-drinkers and the sample size
table(nscc_student_data$Coffee)
## 
##  No Yes 
##  10  30
# YAY! We can use the prop.test function because it is easy

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 which is greater than 0.05, so we cannot reject the null hypothesis.

d.) Conclusion

There is not enough evidence in this dataset to say the percentage of NSCC students who drink coffee is larger than the nation’s average in 2015.