Purpose

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.

Instructions

  1. Update the author line at the top to have your name in it.
  2. You must knit this document to an html file and publish it to RPubs. Once you have published your project to the web, you must copy the web url link into the appropriate Course Project assignment in Blackboard before 11:59pm on the due date.
  3. Answer all the following questions completely. Some may ask for written responses.
  4. Use R chunks for code to be evaluated where needed and always comment all of your code so the reader can understand what your code aims to accomplish.
  5. Proofread your knitted document before publishing it to ensure it looks the way you want it to. Tip: Use double spaces at the end of a line to create a line break and make sure text does not have a header label that isn’t supposed to.
  6. DELETE ALL THESE INSTRUCTIONS BEFORE PUBLISHING YOUR DOCUMENT.
  7. There are 7 questions each worth 10 points. Writing mechanics (spelling, grammer, etc.) will count for 15 points. Document organization (commenting code chunks, headers, etc.) will also count for 15 points.

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 and do some exploratory analysis of 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")

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 games 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: p = 0.5 #HA: p > 0.5

  1. Find p-value of sample data occurring by chance
# Calculate sample proportion of games won by coin flip team
n <- 414
x0 <- 235
p <- 0.5

phat <- x0 / n
# SE
SE <- sqrt((phat * (1 - phat)) / n)


# Test statistic
z <- (phat - p) / SE

# p-value
1 - pnorm(z)
## [1] 0.002736588
  1. Decision
    #the p-value is below the null hypothesis and not above. therefore we do not have enough evidence, and so we shall fail to reject the null hypothesis.

  2. State conclusion
    #given the evidence I was given and the nature of the test. we have came to the conclusion that the odds of winning from the coin toss are fair.

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 #HO: the mean of atheism is the same between 2005 and 2012 #HA: the mean of atheism is different between 2005 and 2012

  2. Find p-value of sample data occurring by chance

atheist_counts_2005 <- sum(spain2005$response == "atheist")
non_atheist_counts_2005 <- sum(spain2005$response == "non-atheist")

atheist_counts_2012 <- sum(spain2012$response == "atheist")
non_atheist_counts_2012 <- sum(spain2012$response == "non-atheist")

# Perform two-sample t-test
t.test(c(atheist_counts_2005, non_atheist_counts_2005), 
                        c(atheist_counts_2012, non_atheist_counts_2012))
## 
##  Welch Two Sample t-test
## 
## data:  c(atheist_counts_2005, non_atheist_counts_2005) and c(atheist_counts_2012, non_atheist_counts_2012)
## t = 0.00076232, df = 1.9988, p-value = 0.9995
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2823.238  2824.238
## sample estimates:
## mean of x mean of y 
##     573.0     572.5
  1. Decision
    #with a p.value of 0.9995, we reject the null hypothesis

  2. State conclusion
    #there is significant evidence to disprove the null hypothesis, therefore we can state a difference between atheists in 2005 and 2012

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 #HO: mu1 = mu2 #HA: mu1=/=mu2

  2. Find p-value of sample data occurring by chance

atheistUS2005 <- sum(USA2005$response == "atheist")
non_atheistUS2005 <- sum(USA2005$response == "non-atheist")

atheistUS2012 <- sum(USA2012$response == "atheist")
non_atheistUS2012 <- sum(USA2012$response == "non-atheist")

# Perform two-sample t-test
t.test(c(atheistUS2005, non_atheistUS2005), 
                        c(atheistUS2012, non_atheistUS2012))
## 
##  Welch Two Sample t-test
## 
## data:  c(atheistUS2005, non_atheistUS2005) and c(atheistUS2012, non_atheistUS2012)
## t = 0, df = 1.9857, p-value = 1
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2888.402  2888.402
## sample estimates:
## mean of x mean of y 
##       501       501
  1. Decision
    #p value is above 0.05 so we reject null hypothesis

  2. State conclusion
    #there is significant data to reject the null hypothesis. therefore, we state that there is a difference between atheist numbers in the US between 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 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?

confidence_level <- 0.95
margin_of_error <- 0.03
proportion <- 0.5 

z_score <- qnorm((1 + confidence_level) / 2)

ceiling((z_score^2 * proportion * (1 - proportion)) / margin_of_error^2)
## [1] 1068

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.

unique_values <- unique(nscc_student_data$VoterReg)
value_of_interest <- unique_values[1] 
num_occurrences <- sum(nscc_student_data$VoterReg == "yes")
total <- length(nscc_student_data$VoterReg)
p_hat <- num_occurrences / total
binom.test(num_occurrences, total, conf.level = 0.95)
## 
##  Exact binomial test
## 
## data:  num_occurrences and total
## number of successes = 0, number of trials = 40, p-value = 1.819e-12
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.0000000 0.0880973
## sample estimates:
## probability of success 
##                      0

Question 6

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

 t.test(nscc_student_data$Height, conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  nscc_student_data$Height
## t = 38.001, df = 38, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  61.08699 67.96173
## sample estimates:
## mean of x 
##  64.52436

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: coffee = 64% #HA: coffee > 64%

b.) Calculate sample statistics

coffeecount <- sum(nscc_student_data$coffee == "yes")
p_hat <- coffeecount / n
p_null <- 0.64

c.) Determine probability of getting sample data by chance

prop.test(coffeecount, n, p = p_null, alternative = "greater")
## 
##  1-sample proportions test with continuity correction
## 
## data:  coffeecount out of n, null probability p_null
## X-squared = 733.22, df = 1, p-value = 1
## alternative hypothesis: true p is greater than 0.64
## 95 percent confidence interval:
##  0 1
## sample estimates:
## p 
## 0

d.) Decision
#the p-value is above 0.05 so we reject the null hypothesis

e.) Conclusion
#there is significant evidence to reject the null hypothesis, therefore we can state that more than 64% of nscc students drink coffee.