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.
To access the press release for the poll, conducted by WIN-Gallup International, click on the following link:
Take a moment to review the report then address the following questions.
# These data were based on a survay on people selected from different countries so they are based sample statistics.
# To generalize the report's findings to the global human population, we must assume the sample must be normal. The observations are from simple random sample within and across countries and the sample size must be < 10% of the population. In addition, each category will expect to see at least 10 successes and 10 failures in our sample.
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.
load("more/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
atheism
correspond to?# each row of Table 6 correspond to one observation, which is a country. each row of`atheism` correspond to a variable, which is percentage of athesim in each country.
To investigate the link between these two ways of organizing this data, take a look at the estimated proportion of atheists in the United States. Towards the bottom of Table 6, we see that this is 5%. We should be able to come to the same number using the atheism
data.
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")
us12_atheism <- by(us12$response,us12$response, length)
us12_atheism
## us12$response: atheist
## [1] 50
## --------------------------------------------------------
## us12$response: non-atheist
## [1] 952
paste("the proportion of atheist responses U.S.:", round(100*us12_atheism[1]/(us12_atheism[1]+us12_atheism[2]),2),"%")
## [1] "the proportion of atheist responses U.S.: 4.99 %"
The proportion of atheist responses in US agree with the percentage in Table 6
As was hinted at in Exercise 1, Table 6 provides statistics, that is, calculations made from the sample of 51,927 people. What we’d like, though, is insight into the population parameters. You answer the question, “What proportion of people in your sample reported being atheists?” with a statistic; while the question “What proportion of people on earth would report being atheists” is answered with an estimate of the parameter.
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.
# 1. The sample size of 952 +50 =1002, The poll is based on a simple random sample and consists of fewer than 10% of the U.S. population. So the observations are independence.
# 2. Success-failure condition. 50 atheism(success) respondents and 952 non-atheism(failed) respondents are both greater than 10.
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")
## Warning: package 'BHH2' was built under R version 3.3.3
## 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 \(\pm\) 3-5% at 95% confidence”.
# assuming the sample follow a normal distribution
# margin of error = z-star * SE
# for 95% power
z_star <- 1.96
SE <- 0.0069
z_star <- 1.96
ME = SE * z_star
paste("the margin of error for the estimate of the proportion of the proportion of atheists in US in 2012 is", ME)
## [1] "the margin of error for the estimate of the proportion of the proportion of atheists in US in 2012 is 0.013524"
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.levels(atheism$nationality)
## [1] "Afghanistan"
## [2] "Argentina"
## [3] "Armenia"
## [4] "Australia"
## [5] "Austria"
## [6] "Azerbaijan"
## [7] "Belgium"
## [8] "Bosnia and Herzegovina"
## [9] "Brazil"
## [10] "Bulgaria"
## [11] "Cameroon"
## [12] "Canada"
## [13] "China"
## [14] "Colombia"
## [15] "Czech Republic"
## [16] "Ecuador"
## [17] "Fiji"
## [18] "Finland"
## [19] "France"
## [20] "Georgia"
## [21] "Germany"
## [22] "Ghana"
## [23] "Hong Kong"
## [24] "Iceland"
## [25] "India"
## [26] "Iraq"
## [27] "Ireland"
## [28] "Italy"
## [29] "Japan"
## [30] "Kenya"
## [31] "Korea, Rep (South)"
## [32] "Lebanon"
## [33] "Lithuania"
## [34] "Macedonia"
## [35] "Malaysia"
## [36] "Moldova"
## [37] "Netherlands"
## [38] "Nigeria"
## [39] "Pakistan"
## [40] "Palestinian territories (West Bank and Gaza)"
## [41] "Peru"
## [42] "Poland"
## [43] "Romania"
## [44] "Russian Federation"
## [45] "Saudi Arabia"
## [46] "Serbia"
## [47] "South Africa"
## [48] "South Sudan"
## [49] "Spain"
## [50] "Sweden"
## [51] "Switzerland"
## [52] "Tunisia"
## [53] "Turkey"
## [54] "Ukraine"
## [55] "United States"
## [56] "Uzbekistan"
## [57] "Vietnam"
sw12 <- subset(atheism, nationality == "Switzerland" & year == "2012")
sw12_atheism <- by(sw12$response,sw12$response, length)
sw12_atheism
## sw12$response: atheist
## [1] 46
## --------------------------------------------------------
## sw12$response: non-atheist
## [1] 467
paste("the proportion of atheist responsesin Switzerland:", round(100*sw12_atheism[1]/(sw12_atheism[1]+sw12_atheism[2]),2),"%")
## [1] "the proportion of atheist responsesin Switzerland: 8.97 %"
# 1. The sample size of 46 +467 =513, The poll is based on a simple random sample and consists of fewer than 10% of the Switzerland population. So the observations are independence.
# 2. Success-failure condition. 46 atheism(success) respondents and 467 non-atheism(failed) respondents are both greater than 10.
inference(sw12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.0897 ; n = 513
## Check conditions: number of successes = 46 ; number of failures = 467
## Standard error = 0.0126
## 95 % Confidence interval = ( 0.0649 , 0.1144 )
95 % Confidence interval = ( 0.0649 , 0.1144 )
# assuming the sample follow a normal distribution
# margin of error = z-star * SE
# for 95% power
z_star <- 1.96
SE2 <- 0.0126
z_star <- 1.96
ME2 = SE2 * z_star
paste("the margin of error for the estimate of the proportion of the proportion of atheists in Switzerland in 2012 is", ME2)
## [1] "the margin of error for the estimate of the proportion of the proportion of atheists in Switzerland in 2012 is 0.024696"
ind12 <- subset(atheism, nationality == "India" & year == "2012")
ind12_atheism <- by(ind12$response,ind12$response, length)
ind12_atheism
## ind12$response: atheist
## [1] 33
## --------------------------------------------------------
## ind12$response: non-atheist
## [1] 1059
paste("the proportion of atheist responsesin Indea:", round(100*ind12_atheism[1]/(ind12_atheism[1]+ind12_atheism[2]),2),"%")
## [1] "the proportion of atheist responsesin Indea: 3.02 %"
# 1. The sample size of 33 +1059 =1092, The poll is based on a simple random sample and consists of fewer than 10% of the India population. So the observations are independence.
# 2. Success-failure condition. 33 atheism(success) respondents and 1059 non-atheism(failed) respondents are both greater than 10.
inference(ind12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.0302 ; n = 1092
## Check conditions: number of successes = 33 ; number of failures = 1059
## Standard error = 0.0052
## 95 % Confidence interval = ( 0.0201 , 0.0404 )
95 % Confidence interval = ( 0.0649 , 0.1144 )
# assuming the sample follow a normal distribution
# margin of error = z-star * SE
# for 95% power
z_star <- 1.96
SE3 <- 0.0052
z_star <- 1.96
ME3 = SE3 * z_star
paste("the margin of error for the estimate of the proportion of the proportion of atheists in India in 2012 is", ME3)
## [1] "the margin of error for the estimate of the proportion of the proportion of atheists in India in 2012 is 0.010192"
Imagine you’ve set out to survey 1000 people on two questions: are you female? and are you left-handed? Since both of these sample proportions were calculated from the same sample size, they should have the same margin of error, right? Wrong! While the margin of error does change with sample size, it is also affected by the proportion.
Think back to the formula for the standard error: \(SE = \sqrt{p(1-p)/n}\). This is then used in the formula for the margin of error for a 95% confidence interval: \(ME = 1.96\times SE = 1.96\times\sqrt{p(1-p)/n}\). Since the population proportion \(p\) is in this \(ME\) formula, it should make sense that the margin of error is in some way dependent on the population proportion. We can visualize this relationship by creating a plot of \(ME\) vs. \(p\).
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 \times 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")
p
and me
.# ME incrases with the increase of p when p < 50%, while decrases with the increase of p when p > 50%.
The textbook emphasizes that you must always check conditions before making inference. For inference on proportions, the sample proportion can be assumed to be nearly normal if it is based upon a random sample of independent observations and if both \(np \geq 10\) and \(n(1 - p) \geq 10\). This rule of thumb is easy enough to follow, but it makes one wonder: what’s so special about the number 10?
The short answer is: nothing. You could argue that we would be fine with 9 or that we really should be using 11. What is the “best” value for such a rule of thumb is, at least to some degree, arbitrary. However, 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 \(\hat{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 \(\hat{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.
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.10580 0.12980
##Center
mean(p_hats)
## [1] 0.09969
# shape
sd(p_hats)
## [1] 0.009287382
boxplot(p_hats,y_lab="p_hats",xlab="proportions")
The sampling distribution ins near normalwith the mean close to the population mean of 0.1. There are a few outliers on both tails but these are small compared to the total sample size.
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?# simulation 1
p1 <- 0.1
n1 <- 400
p_hats1 <- rep(0,5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n1, replace =T, prob = c(p1,(1-p1)))
p_hats1[i] <- sum(samp=="atheist")/n1
}
summary(p_hats1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.05250 0.09000 0.10000 0.09976 0.11000 0.15500
##Center
mean(p_hats1)
## [1] 0.099759
# shape
sd(p_hats1)
## [1] 0.01504642
boxplot(p_hats,y_lab="p_hats1",xlab="proportions")
p2 <- 0.02
n2 <- 1040
p_hats2 <- rep(0,5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n2, replace =T, prob = c(p2,(1-p2)))
p_hats2[i] <- sum(samp=="atheist")/n2
}
summary(p_hats2)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.005769 0.017310 0.020190 0.019950 0.023080 0.039420
##Center
mean(p_hats2)
## [1] 0.01995423
# shape
sd(p_hats2)
## [1] 0.004422505
boxplot(p_hats,y_lab="p_hats2",xlab="proportions")
# simulation 3
p3 <- 0.02
n3 <- 400
p_hats3 <- rep(0,5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n3, replace =T, prob = c(p3,(1-p3)))
p_hats3[i] <- sum(samp=="atheist")/n3
}
summary(p_hats3)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00000 0.01500 0.02000 0.01988 0.02500 0.04750
##Center
mean(p_hats3)
## [1] 0.0198785
# shape
sd(p_hats3)
## [1] 0.006976835
boxplot(p_hats,y_lab="p_hats3",xlab="proportions")
par(mfrow = c(2,2))
hist(p_hats, main = "p = 0.1, n = 1040", xlim = c(0, 0.18))
hist(p_hats1, main = "p = 0.1, n = 400", xlim = c(0, 0.18))
hist(p_hats2, main = "p = 0.02, n = 1040", xlim = c(0, 0.18))
hist(p_hats3, main = "p = 0.02, n = 400", xlim = c(0, 0.18))
increasing n decreased the spread and thus decreased the margin of error.
increasing p increased the spread and thus increased the margin of error until it reaches 0.5 then the opposite is true.
Once you’re done, you can reset the layout of the plotting window by using the command par(mfrow = c(1, 1))
command or clicking on “Clear All” above the plotting window (if using RStudio). Note that the latter will get rid of all your previous plots.
par(mfrow = c(1, 1))
# They both have a normal distribution with almost similar spreads, so as to their margin of errors. The atheism(success) respondents in australia is 0.1 x 1040 = 104 and in Ecuador is 0.02x400 = 8 people. The sample size of 10 for Ecuador poll is not met. It is probably not sensible to proceed with inference and report margin of errors for Ecuador poll but probably sensible to proceed with inference and report margin of errors for Austrlia.
The question of atheism was asked by WIN-Gallup International in a similar survey that was conducted in 2005. (We assume here that sample sizes have remained the same.) Table 4 on page 13 of the report summarizes survey results from 2005 and 2012 for 39 countries.
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.
a. Is there convincing evidence that Spain has seen a change in its atheism index 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.
spn05 <- subset(atheism, nationality == "Spain" & year == "2005")
spn12 <- subset(atheism, nationality == "Spain" & year == "2012")
n05 <- length(spn05$nationality)
n12 <- length(spn12$nationality)
paste("sample size of 2005:",n05)
## [1] "sample size of 2005: 1146"
paste("sample size of 2012:",n12)
## [1] "sample size of 2012: 1145"
# 2005
s_05 <- 1146 * .1
# 2012
s_12 <- 1145 *.09
paste("s_05:", s_05)
## [1] "s_05: 114.6"
paste("s_12:", s_12)
## [1] "s_12: 103.05"
# Observations are independent. The poll is based on a simple random sample and consists of fewer than 10% of the Spanish population in 2005 and 2012. The sample size for this study was 1146 in 2005 and 1145 in 2012.
# Success-failure condition. Success respondents in 2005 and 2012 are 115 and 103 respectively which are greater than 10.
# H_0: p12 - p05 = 0 There was no change in the atheism index in Spain from 2005 to 2012
# H_A: p12 - p05 != 0 There was a chnage in the atheism index in Spain from 2005 to 2012
spn_inf <- inference(spn05$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 )
spn_inf <- inference(spn12$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_spn05 = 0.1003
n_spn05 = 1146
p_spn12 = 0.09
n_spn12 = 1145
PE_spn = p_spn12 - p_spn05
SE_spn = sqrt((p_spn05*(1-p_spn05)/n_spn05)+(p_spn12*(1-p_spn12)/n_spn12))
paste("Control interval for difference between proportion in 2005 and 2012:",PE_spn - (1.96*SE_spn), "to", PE_spn + (1.96*SE_spn))
## [1] "Control interval for difference between proportion in 2005 and 2012: -0.0343267350672213 to 0.0137267350672213"
The confidence intervals do overlap so there is no meaningful difference. Also, The control interval includes 0 so we do not reject the null hypohtesis that p_2012 - p_2005 is zero.
**b.** Is there convincing evidence that the United States has seen 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")
n05 <- length(us05$nationality)
n12 <- length(us12$nationality)
paste("sample size of 2005:",n05)
## [1] "sample size of 2005: 1002"
paste("sample size of 2012:",n12)
## [1] "sample size of 2012: 1002"
# 2005
s_05 <- 1002 * .1
# 2012
s_12 <- 1002 *.09
paste("s_05:", s_05)
## [1] "s_05: 100.2"
paste("s_12:", s_12)
## [1] "s_12: 90.18"
# Observations are independent. The poll is based on a simple random sample and consists of fewer than 10% of the U.S. population in 2005 and 2012. The sample size for this study was 1146 in 2005 and 1145 in 2012.
# Success-failure condition. Success respondents in 2005 and 2012 are 100 and 90 respectively which are greater than 10.
# H_0: p12 - p05 = 0 There was no change in the atheism index in U.S. from 2005 to 2012
# H_A: p12 - p05 != 0 There was a chnage in the atheism index in U.S. from 2005 to 2012
us_inf <- 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 )
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 )
p_us05 = 0.01
n_us05 = 1002
p_us12 = 0.05
n_us12 = 1002
PE_us <- p_us12 - p_us05
SE_us <- sqrt(((p_us05*(1-p_us05))/n_us05)+((p_us12*(1-p_us12))/n_us12))
paste("Control interval for difference between proportion in 2005 and 2012:",PE_us - (1.96*SE_us),"to", PE_us + (1.96*SE_us))
## [1] "Control interval for difference between proportion in 2005 and 2012: 0.0251653208800858 to 0.0548346791199142"
The control interval does NOT include 0 so we reject the null hypohtesis that p_2012 - p_2005 is zero. There is evidence that there is a change in the atheism index in the USA from 2005 to 2012.
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.
Those with point difference between 2005 and 2012 proportions that are about +/-3% and a small margin of error will probably detect a change at the 95% confidnece. If there is no change in % of atheism, then these will beType 1 errors - rejecting the null hypothesis when in fact it is true.
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.
#SE = sqrt((p*(1-p))/n)
#based on table of the relation of p and ME, an ME of 1% corresponds to a p of #approximately<=0.018
#ME = z_star * SE
ME <- 0.01
z_star <- 1.96
SE <- ME /z_star
SE
## [1] 0.005102041
ME <- 2 * sqrt(p * (1 - p)/n)
# when ME < 0.01, SE < 0.005102041
# n = (p*(1-p))/SE^2
n <- (p*(1-p))/0.005102041^2
n <- (p*(1-p))/2.603082e-05
# when p < 0.5
# the small p, the small of n
# let
p <- 0.02
# then
n <- (p*(1-p))/2.603082e-05
n
## [1] 752.9536
This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was written for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel.