In August of 2012, news outlets ranging from the Washington Post to the Huffington Post ran a story about the rise of atheism in America. The source for the story was a poll that asked people, “Irrespective of whether you attend a place of worship or not, would you say you are a religious person, not a religious person or a convinced atheist?” This type of question, which asks people to classify themselves in one way or another, is common in polling and generates categorical data. In this lab we take a look at the atheism survey and explore what’s at play when making inference about population proportions using categorical data.
https://sidmennt.is/wp-content/uploads/Gallup-International-um-tr%C3%BA-og-tr%C3%BAleysi-2012.pdf
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 are sample statistics, because they come from a poll done in 57 countries.
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?
We must assume the sampling method was random, observations are independent and the sample is representative of the population. The report´s findings cannot be generalize, because they are not representative of the global population. In this report, Europe and America are heavily over represented at the expense of heavily under representing Asia and Africa.
Turn your attention to Table 6 (pages 15 and 16), which reports the sample size and response percentages for all 57 countries. While this is a useful format to summarize the data, we will base our analysis on the original data set of individual responses to the survey. Load this data set into R with the following command.
download.file("http://www.openintro.org/stat/data/atheism.RData", destfile = "atheism.RData")
load("atheism.RData")
summary(atheism)
## nationality response year
## Pakistan : 5409 atheist : 5498 Min. :2005
## France : 3359 non-atheist:82534 1st Qu.:2005
## Korea, Rep (South): 3047 Median :2012
## Ghana : 2995 Mean :2009
## Macedonia : 2418 3rd Qu.:2012
## Peru : 2414 Max. :2012
## (Other) :68390
What does each row of Table 6 correspond to? What does each row of atheism correspond to?
Each row of Table 6 correspond to an individual country. Each row of atheism correspond to an individual respondent.
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)
## nationality response year
## United States:1002 atheist : 50 Min. :2012
## Afghanistan : 0 non-atheist:952 1st Qu.:2012
## Argentina : 0 Median :2012
## Armenia : 0 Mean :2012
## Australia : 0 3rd Qu.:2012
## Austria : 0 Max. :2012
## (Other) : 0
table(us12$response)/1002
##
## atheist non-atheist
## 0.0499002 0.9500998
Yes,it does agree. The Table 6 proportion is 5% atheist and the calculated proportion is 4.99%.
The inferential tools for estimating population proportion are analogous to those used for means in the last chapter: the confidence interval and the hypothesis test.
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 conditions for inference are: 1) the data is a random sample from the population, 2) the observations were chosen independently, and 3) all the items in the population have an equal chance to be sampled. The coverage of the poll was at the national level, but the mode of interview was online, therefore we cannot be confident that all the conditions are met.
If the conditions for inference are reasonable, we can either calculate the standard error and construct the interval by hand, or allow the inference function to do it for us.
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 )
Note that since the goal is to construct an interval estimate for a proportion, it’s necessary to specify what constitutes a “success”, which here is a response of “atheist”.
Although formal confidence intervals and hypothesis tests don’t show up in the report, suggestions of inference appear at the bottom of page 7: “In general, the error margin for surveys of this kind is ±3-5% at 95% confidence”.
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?
The margin of error is ±1.96XSE, therefore 1.96*0.0069 = ±0.0135.
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.
bra12 <- subset(atheism, nationality == "Brazil" & year == "2012")
inference(bra12$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 )
The 95% confidence interval for Brazil is (0.0056 , 0.0143) and the margins of error are ± 0.004312.
pak12 <- subset(atheism, nationality == "Pakistan" & year == "2012")
inference(pak12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.02 ; n = 2704
## Check conditions: number of successes = 54 ; number of failures = 2650
## Standard error = 0.0027
## 95 % Confidence interval = ( 0.0147 , 0.0252 )
The 95% confidence interval for Pakistan is (0.0147 , 0.0252) and the margins of error are ± 0.005292.
In both countries the conditions for inference are met; both have at least 10 successes and 10 failures, and both countries have a national coverage.
The first step is to make a vector p that is a sequence from 0 to 1 with each number separated by 0.01. We can then create a vector of the margin of error (me) associated with each of these values of p using the familiar approximate formula (ME=2×SE). Lastly, we plot the two vectors against each other to reveal their relationship.
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")
Describe the relationship between p and me.
The relationship between p and me is that of an “inverted-U” or “bell-shaped curve” in which the margin of error reaches a maximum value when p=0.5 and the margin of error decreases as p reaches it´s lowest or highest values.
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))
These commands build up the sampling distribution of p̂ using the familiar for loop. You can read the sampling procedure for the first line of code inside the for loop as, “take a sample of size n with replacement from the choices of atheist and non-atheist with probabilities p and 1−p, respectively.” The second line in the loop says, “calculate the proportion of atheists in this sample and record this value.” The loop allows us to repeat this process 5,000 times to build a good representation of the sampling distribution.
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
The sampling distribution has a center close to 0.1 mean, with a spread of 0.009 standard deviations and a shape of a normal distribution curve.
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_hats2 <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats2[i] <- sum(samp == "atheist")/n}
p <- 0.02
n <- 1040
p_hats3 <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats3[i] <- sum(samp == "atheist")/n}
p <- 0.02
n <- 400
p_hats4 <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats4[i] <- sum(samp == "atheist")/n}
par(mfrow = c(2, 2))
hist(p_hats, main = "p = 0.1, n = 1040", xlim = c(0, 0.18))
hist(p_hats2, main = "p = 0.1, n = 400", xlim = c(0, 0.18))
hist(p_hats3, main = "p = 0.02, n = 1040", xlim = c(0, 0.18))
hist(p_hats4, main = "p = 0.02, n = 400", xlim = c(0, 0.18))
Based on these limited plots, as n increases, the spread of of the sampling distribution of p decreases, and as p decreases the mean of the sampling distribution decreases.
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?
It should be reasonable in the case of Australia since 0.1X1040 > 10 successes, therefore meeting the success-failure condition. But not in the case of Ecuador since 0.02X400 < 10 successes, therefore not meeting the success-failure condition.
Answer:
spain05 <- subset(atheism, nationality == "Spain" & year == "2005")
inference(spain05$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 )
spain12 <- subset(atheism, nationality == "Spain" & year == "2012")
inference(spain12$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 )
The 95% confidence for the proportion of atheism in Spain in 2005 is (0.083, 0.1177) and in 2012 is (0.0734, 0.1065). Since there is overlap between the two confidence intervals, there is convincing evidence that Spain has not seen a change in its atheism index between 2005 and 2012.
Answer:
us05 <- subset(atheism, nationality == "United States" & year == "2005")
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 )
The 95% confidence for the proportion of atheism in the United States in 2005 is (0.0038, 0.0161) and in 2012 is (0.0364, 0.0634). Since there is no overlap between the two confidence intervals, there is convincing evidence that the United States has seen a change in its atheism index between 2005 and 2012.
Answer:
Table 4 has 39 countries in total and with a significance level of 0.05: 39X0.05 = 1.95 countries we would expect to detect a change simply by chance.
Answer:
At p = 0.5, the margin of error has its maximum value, therefore:
0.01 = 1.96 X (square root(((0.5*0.5)/n)))
n= (0.5*0.5)/(0.01/1.96)^2
(.5*.5)/(.01/1.96)^2
## [1] 9604
We would need to sample at least 9604 residents to ensure that the margin of error is no greater than 1% with 95% confidence.