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 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")
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.)
#HO: p = 0.5 #HA: p > 0.5
# 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
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.
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.
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 #HO: the mean of atheism is the same between 2005 and 2012 #HA: the mean of atheism is different between 2005 and 2012
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
Decision
#with a p.value of 0.9995, we reject the null hypothesis
State conclusion
#there is significant evidence to disprove the null hypothesis,
therefore we can state a difference between atheists in 2005 and
2012
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 #HO: mu1 = mu2 #HA: mu1=/=mu2
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
Decision
#p value is above 0.05 so we reject null hypothesis
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.
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
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
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
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.