download.file("http://www.openintro.org/stat/data/atheism.RData", destfile = "atheism.RData")
load("atheism.RData")
head(atheism)
## nationality response year
## 1 Afghanistan non-atheist 2012
## 2 Afghanistan non-atheist 2012
## 3 Afghanistan non-atheist 2012
## 4 Afghanistan non-atheist 2012
## 5 Afghanistan non-atheist 2012
## 6 Afghanistan non-atheist 2012
Exercise 1: In the first paragraph, several key findings are reported. Do these percentages appear to be sample statistics (derived from the data sample) or population parameters? These percentages appear to be sample statistics because they are from a sample size of the population.
Exercise 2: The title of the report is “Global Index of Religiosity and Atheism”. To generalize the report’s findings to the global human population, what must we assume about the sampling method? Does that seem like a reasonable assumption?
The sampling method have to be randomly selected from the population of the Earth. The cases have to be independent, less than 10% of the population is sampled, and also 10 successes and 10 failures(in this scenario, atheist and non-atheists). All these 3 conditions are met, so it seems like a reasonable assumption.
Exercise 3: What does each row of Table 6 correspond to? What does each row of atheism correspond to?
In Table 6, each row represents the regiosity of the each country. Each row of atheism corresponds to a response of a individual.
Exercise 4: Using the command below, create a new dataframe called us12 that contains only the rows in atheism associated with respondents to the 2012 survey from the United States. Next, calculate the proportion of atheist responses. Does it agree with the percentage in Table 6? If not, why? It does agree. 0.0499002 is closed to the 5 % in Table 6.
us12 <- subset(atheism, nationality == "United States" & year == "2012")
prop_us12<-subset(us12, response == "atheist")
dim(us12)
## [1] 1002 3
dim(prop_us12)
## [1] 50 3
prop_ath<-50/1002
prop_ath
## [1] 0.0499002
Exercise 5: Write out the conditions for inference to construct a 95% confidence interval for the proportion of atheists in the United States in 2012. Are you confident all conditions are met?
The cases are independent. The sample size of 51927 have to be less than 10% of the total Earth population, which is. We have 10 successes and 10 failures which are the atheists and non-atheists.
inference(us12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.0499 ; n = 1002
## Check conditions: number of successes = 50 ; number of failures = 952
## Standard error = 0.0069
## 95 % Confidence interval = ( 0.0364 , 0.0634 )
Exercise 6: Based on the R output, what is the margin of error for the estimate of the proportion of the proportion of atheists in US in 2012?
ME<-(0.0634-0.0364)/2
ME
## [1] 0.0135
1.35% margin of error.
Exercise 7: Using the inference function, calculate confidence intervals for the proportion of atheists in 2012 in two other countries of your choice, and report the associated margins of error. Be sure to note whether the conditions for inference are met. It may be helpful to create new data sets for each of the two countries first, and then use these data sets in the inference function to construct the confidence intervals.
The conditions for inference meets for both countries.
cn12 <- subset(atheism, nationality == "China" & year == "2012")
inference(cn12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.47 ; n = 500
## Check conditions: number of successes = 235 ; number of failures = 265
## Standard error = 0.0223
## 95 % Confidence interval = ( 0.4263 , 0.5137 )
ME_cn<-(0.5137-0.4263)/2
ME_cn
## [1] 0.0437
4.37% margin of error.
hk12 <- subset(atheism, nationality == "Hong Kong" & year == "2012")
inference(hk12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.09 ; n = 500
## Check conditions: number of successes = 45 ; number of failures = 455
## Standard error = 0.0128
## 95 % Confidence interval = ( 0.0649 , 0.1151 )
ME_hk<-(0.1151-0.0649)/2
ME_hk
## [1] 0.0251
2.51% margin of error.
n <- 1000 p <- seq(0, 1, 0.01) me <- 2 * sqrt(p * (1 - p)/n) plot(me ~ p, ylab = “Margin of Error”, xlab = “Population Proportion”)
Exercise 8: Describe the relationship between p and me.
As the population proportion increase, the margin of error increase up to around 50%. After the 50% population proportion, we see the margin of error decreases. There is an inverse relationship between the two.
p <- 0.1
n <- 1040
p_hats <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats[i] <- sum(samp == "atheist")/n
}
hist(p_hats, main = "p = 0.1, n = 1040", xlim = c(0, 0.18))
Exercise 9: Describe the sampling distribution of sample proportions at \(n = 1040\) and \(p = 0.1\). Be sure to note the center, spread, and shape. Hint: Remember that R has functions such as mean to calculate summary statistics.
summary(p_hats)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.07019 0.09327 0.09904 0.09969 0.10577 0.12981
sd(p_hats)
## [1] 0.009287382
IQR(p_hats)
## [1] 0.0125
The mean is 0.09969, the standard deviation is 0.009287382 and the IQR is 0.0125.
The distribution represents a bell curve shape.
Exercise 10: eat the above simulation three more times but with modified sample sizes and proportions: for \(n = 400\) and \(p = 0.1\), \(n = 1040\) and \(p = 0.02\), and \(n = 400\) and \(p = 0.02\). Plot all four histograms together by running the par(mfrow = c(2, 2)) command before creating the histograms. You may need to expand the plot window to accommodate the larger two-by-two plot. Describe the three new sampling distributions. Based on these limited plots, how does \(n\) appear to affect the distribution of \(\hat{p}\)? How does \(p\) affect the sampling distribution?
The p is the center of the curve, it shifts the curve left or right depending on the value. Increasing n affects the sampling distribution by making the peak sharper and narrower, making it approach normal distribution.
(mfrow = c(2, 2))
## [1] 2 2
p <- 0.1
n <- 400
p_hats <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats[i] <- sum(samp == "atheist")/n
}
hist(p_hats, main = "p = 0.1, n = 400", xlim = c(0, 0.18))
p <- 0.02
n <- 1040
p_hats <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats[i] <- sum(samp == "atheist")/n
}
hist(p_hats, main = "p = 0.02, n = 1040", xlim = c(0, 0.18))
p <- 0.02
n <- 400
p_hats <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats[i] <- sum(samp == "atheist")/n
}
hist(p_hats, main = "p = 0.02, n = 400", xlim = c(0, 0.18))
Exercise 11:
If you refer to Table 6, you’ll find that Australia has a sample proportion of 0.1 on a sample size of 1040, and that Ecuador has a sample proportion of 0.02 on 400 subjects. Let’s suppose for this exercise that these point estimates are actually the truth. Then given the shape of their respective sampling distributions, do you think it is sensible to proceed with inference and report margin of errors, as the reports does?
Both seems to be normal distribution and they follow the conditions of inference. So it would be sensible to proceed.
For sp12(Spain 2012): 95 % Confidence interval = ( 0.0734 , 0.1065 ) For sp05(Spain 2005): 95 % Confidence interval = ( 0.083 , 0.1177 )
THere’s overlap between the two CI, so there are no change from 2005 to 2012.
sp12<-subset(atheism, nationality=="Spain" & year=="2012")
sp05<-subset(atheism, nationality=="Spain" & year=="2005")
inference(sp12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.09 ; n = 1145
## Check conditions: number of successes = 103 ; number of failures = 1042
## Standard error = 0.0085
## 95 % Confidence interval = ( 0.0734 , 0.1065 )
inference(sp05$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.1003 ; n = 1146
## Check conditions: number of successes = 115 ; number of failures = 1031
## Standard error = 0.0089
## 95 % Confidence interval = ( 0.083 , 0.1177 )
There is no overlap between the two CI, so there are evidence that United States had a change in its atheism index between 2005 and 2012.
us12<-subset(atheism, nationality=="United States" & year=="2012")
us05<-subset(atheism, nationality=="United States" & year=="2005")
inference(us12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.0499 ; n = 1002
## Check conditions: number of successes = 50 ; number of failures = 952
## Standard error = 0.0069
## 95 % Confidence interval = ( 0.0364 , 0.0634 )
inference(us05$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.01 ; n = 1002
## Check conditions: number of successes = 10 ; number of failures = 992
## Standard error = 0.0031
## 95 % Confidence interval = ( 0.0038 , 0.0161 )
If in fact there has been no change in the atheism index in the countries listed in Table 4, in how many of those countries would you expect to detect a change (at a significance level of 0.05) simply by chance? Hint: Look in the textbook index under Type 1 error.
The significance level is 0.05. Table 4 shows 39 countries. 0.05 * 39= 1.95 About 2(1.95) countries will detect a change.
Suppose you’re hired by the local government to estimate the proportion of residents that attend a religious service on a weekly basis. According to the guidelines, the estimate must have a margin of error no greater than 1% with 95% confidence. You have no idea what to expect for \(p\). How many people would you have to sample to ensure that you are within the guidelines? Hint: Refer to your plot of the relationship between \(p\) and margin of error. Do not use the data set to answer this question.
The margin of error is at the maxuimum when p is equal to 0.5. Using the margin of error formula, we can find out the value of n.
We need to sample 9604 people.
n = (0.5*1.96/0.01)^2
n
## [1] 9604