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?
Based on the first paragraph it seems that the data appear to be sample statistics stemming from a data sample. With inaccuracies in local census data it is far-fetched that data could be collected accurately across the globe in terms of religion.
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?
One can assume the findings stemmed from sampling without replacement.
download.file("http://www.openintro.org/stat/data/atheism.RData", destfile="atheism.RData")
load("atheism.RData")
What does each row of Table 6 correspond to? What does each row of atheism correspond to?
Each row corresponds to a country along with their sample size, percentages of religious/non-religious, also if they are a convinced atheist or if they don’t know.
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?
us12 <- subset(atheism, nationality == "United States" & year =="2012")
summary(us12$response)
## atheist non-atheist
## 50 952
#Take the number of atheist and divide by non-athest + atheist
50/1002
## [1] 0.0499002
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 samples are independent and is less than 10% of the population and are selected randomly.
inference(us12$response, est="proportion", type="ci", method="theoretical", success="atheist")
## Warning: package 'BHH2' was built under R version 3.4.2
## 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 )
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?
#standard or error = 0.0069
margin_of_error <- 1.96 * 0.0069
margin_of_error
## [1] 0.013524
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.
#brazil
br12 <- subset(atheism, nationality == "Brazil" & year =="2012")
inference(br12$response, est="proportion", type="ci", method="theoretical", success="atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.01 ; n = 2002
## Check conditions: number of successes = 20 ; number of failures = 1982
## Standard error = 0.0022
## 95 % Confidence interval = ( 0.0056 , 0.0143 )
#standard error brazil 0.0022
moe_br <- 1.96 * 0.0022
moe_br
## [1] 0.004312
#china
ch12 <- subset(atheism, nationality =="Ghana" & year =="2012")
inference(ch12$response, est="proportion", type="ci", method="simulation", success="atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0 ; n = 1490
## 95 % Bootstrap interval = ( 0 , 0 )
While the margin of error does change with sample size, it is also affected by the proportion.
We can visualize this relationship by creating a plot of ME vs. p.
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.
The plot resembles a bell curve
When np and n(1???p) reaches 10 the sampling distribution is sufficiently normal to use confidence intervals and hypothesis tests that are based on that approximation.
We can investigate the interplay between n and p and the shape of the sampling distribution by using simulations. To start off, we simulate the process of drawing 5000 samples of size 1040 from a population with a true atheist proportion of 0.1. For each of the 5000 samples we compute p^ and then plot a histogram to visualize their distribution.
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))
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.07115 0.09327 0.10000 0.10011 0.10673 0.13654
boxplot(p_hats)
Repeat 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 p^? How does p affect the sampling distribution?
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
}
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
}
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
}
par(mfrow = c(2,2))
hist(p_hats, main ="p=0.1,n=400")
hist(p_hats, main ="p=0.02,n=1040")
hist(p_hats, main ="p=0.02,n=400")
##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?
Ecuadors data does not meet the success-failure condition because np is less than 10
Answer the following two questions using the inference function. As always, write out the hypotheses for any tests you conduct and outline the status of the conditions for inference.
H0: No undenible evidence of Spain seeing a change in the number of atheist residents between 2005 and 2012 H1: Undeniable evidence that Spain has seen a change in the number of atheist residents between 2005 and 2012.
Hint: Create a new data set for respondents from Spain. Form confidence intervals for the true proportion of athiests in both years, and determine whether they overlap.
spain_05 <- subset(atheism, nationality =="Spain" & year =="2005")
inference(spain_05$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 )
spain_12 <- subset(atheism, nationality =="Spain" & year =="2012")
inference(spain_12$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 )
p_sp_05 = 0.1003
n_sp_05 = 1146
p_sp_12 = 0.09
n_sp_12 = 1145
PE_sp = p_sp_12 - p_sp_05
SE_sp = sqrt(((p_sp_05/n_sp_05)+(p_sp_12*(1-p_sp_12))/n_sp_12))
SE_sp
## [1] 0.01261151
PE_sp + (1.96*SE_sp)
## [1] 0.01441856
us_05 <- subset(atheism, nationality =="United States" & year =="2005")
inference(us_05$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 )
us_12 <- subset(atheism, nationality =="United States" & year =="2012")
inference(us_12$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 )
p_us_05 = 0.01
n_us_05 = 1002
p_us_12 = 0.0499
n_us_12 = 1002
PE_us = p_us_12 - p_us_05
SE_us = sqrt(((p_us_05/n_us_05)+(p_us_12*(1-p_us_12))/n_us_12))
SE_us
## [1] 0.007569372
PE_us + (1.96*SE_us)
## [1] 0.05473597
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.
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 pp. 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 pp and margin of error. Do not use the data set to answer this question.
se = 0.01/1.96
p=0.018
n = (p*(1-p))/se^2
ceiling(n)
## [1] 680