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 you’ll take a look at the atheism survey and explore what’s at play when making inference about population proportions using categorical data.
First, read the official press release for the poll, conducted by WIN-Gallup International link
Take a moment to review the report. You should keep it open during this lab for reference.
How many people were interviewed for this survey?
A poll conducted by WIN-Gallup International surveyed 51,927 people from 57 countries
Which of the following methods were used to gather information?
All of the above
In the first paragraph, several key findings are reported. These percentages appear to be sample statistics.
TRUE
The title of the report is “Global Index of Religiosity and Atheism”.
To generalize the report’s findings to the global human population, We must assume that the sample was a random sample from the entire population in order to be able to generalize the results to the global human population.
This does seem to be a reasonable assumption.
YES?
Mmmm, that’s not correct. Think again on the methods used for conducting the survey. Do these methods assure a representative sample?
NO
Turn your attention to Table 6 (pages 15 and 16 of the report), which reports the sample size and response percentages for all 57 countries.
While this is a useful format to summarize the data, you will base your analysis on the original data set of individual responses to the survey.
load(url('http://s3.amazonaws.com/assets.datacamp.com/course/dasi/atheism.RData'))
names(atheism)
## [1] "nationality" "response" "year"
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
tail(atheism)
## nationality response year
## 88027 Vietnam non-atheist 2005
## 88028 Vietnam non-atheist 2005
## 88029 Vietnam non-atheist 2005
## 88030 Vietnam non-atheist 2005
## 88031 Vietnam non-atheist 2005
## 88032 Vietnam non-atheist 2005
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
table(atheism$nationality)
##
## Afghanistan
## 1031
## Argentina
## 1993
## Armenia
## 495
## Australia
## 1039
## Austria
## 2005
## Azerbaijan
## 509
## Belgium
## 527
## Bosnia and Herzegovina
## 2000
## Brazil
## 2002
## Bulgaria
## 1997
## Cameroon
## 1008
## Canada
## 2005
## China
## 500
## Colombia
## 1212
## Czech Republic
## 2000
## Ecuador
## 804
## Fiji
## 1018
## Finland
## 1969
## France
## 3359
## Georgia
## 1000
## Germany
## 1004
## Ghana
## 2995
## Hong Kong
## 500
## Iceland
## 1704
## India
## 2183
## Iraq
## 1000
## Ireland
## 1010
## Italy
## 1974
## Japan
## 2412
## Kenya
## 2000
## Korea, Rep (South)
## 3047
## Lebanon
## 505
## Lithuania
## 2040
## Macedonia
## 2418
## Malaysia
## 1040
## Moldova
## 2170
## Netherlands
## 1014
## Nigeria
## 2098
## Pakistan
## 5409
## Palestinian territories (West Bank and Gaza)
## 627
## Peru
## 2414
## Poland
## 1045
## Romania
## 2089
## Russian Federation
## 2000
## Saudi Arabia
## 500
## Serbia
## 2073
## South Africa
## 402
## South Sudan
## 1020
## Spain
## 2291
## Sweden
## 495
## Switzerland
## 1020
## Tunisia
## 498
## Turkey
## 1032
## Ukraine
## 2026
## United States
## 2004
## Uzbekistan
## 500
## Vietnam
## 1000
What does each row of Table 6 in the report correspond to?
Countries
What does each row of atheism correspond to?
The rows here are indeed the individual persons.
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, you see that this is 5%. You should be able to come to the same number using the atheism data.
Here, you will work with the data set us12. This data set contains only the rows in atheism associated with respondents to the 2012 survey from the United States. You can check the code used to create this data set in the editor.
us12 = subset(atheism, atheism$nationality == "United States" & atheism$year == "2012")
sum(us12$response == 'atheist') / length(us12$response)
## [1] 0.0499
Does the percentage in proportion agree with the percentage in Table 6 of the report?
YES
As was hinted before, Table 6 of the report provides statistics, that is, calculations made from the sample of 51,927 people. What you would 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 previous lab: the confidence interval and the hypothesis test.
Instructions
Yes, sample size is big enough, the observations are independent, the proportion ensures we have at least 10 examples for each category
If you decided the conditions for inference are reasonable, you can now construct the interval by hand. It is possible to do this either by calculating the standard error and constructing the interval by hand, or by allowing the inference function to do it for us.
load(url("http://s3.amazonaws.com/assets.datacamp.com/course/dasi/inference.Rdata"))
inference(us12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Warning: package 'lmPerm' was built under R version 3.1.1
## Warning: package 'BHH2' was built under R version 3.1.1
## 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.”
Use the inference function in the console (it’s already loaded). Keep in mind that the inference function is a bit on the heavy side, it may take a while to run.
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 )
or manually
n = length(us12$response)
prop = sum(us12$response == 'atheist') / n
prop
## [1] 0.0499
se = sqrt(prop * (1 - prop) / n)
se
## [1] 0.006879
lower = prop - 1.96 * se
upper = prop + 1.96 * se
c(lower, upper)
## [1] 0.03642 0.06338
upper - lower
## [1] 0.02696
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 or error is equal to the radius (or half the width) of the confidence interval.)
(upper - lower) / 2
## [1] 0.01348
The margin of error for the estimate of the proportion of atheists in the US in 2012 is 0.0135.
Using the inference function, now calculate the confidence intervals for the proportion of atheists in 2012 in India.
First, make sure to note whether the conditions for inference are met.
india = subset(atheism, atheism$nationality == "India" & atheism$year == "2012")
inference(india$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 )
What’ll be the situation in China? Perform the same analysis for China.
china = subset(atheism, atheism$nationality == "China" & atheism$year == "2012")
inference(china$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 )
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{\cfrac{p (1 - p)}{n}}\)
This is then used in the formula for the margin of error for a 95% confidence interval:
\(\text{ME} = 1.96 \times \text{SE}\)
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\).
n = 1000
p = seq(0, 1, 0.01)
# We 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 X SE):
me = 2 * sqrt(p * (1 - p)/n)
# Finally, plot the two vectors against each other to reveal their
# relationship:
plot(me ~ p, type='l')
So it’s clear that ME depends on the proportion: the highest ME is when \(p = 0.5\)
Question 9
Which of the following is false about the relationship between p and me?
The most conservative estimate for calculating a confidence interval occurs when \(p\) is set to 1.
The question of atheism was asked by WIN-Gallup International in a similar survey that was conducted in 2005. Table 4 on page 13 of the report summarizes survey results from 2005 and 2012 for 39 countries.
Use the inference function to answer the following question. As always, write out the hypotheses for any tests you conduct and outline the status of the conditions for inference. The atheism data set is already loaded in the workspace.
spain = subset(atheism, atheism$nationality == "Spain")
proportion = sum(spain$response == 'atheist') / length(spain$response)
# 2nd argument - grouping
inference(spain$response, spain$year, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Warning: Explanatory variable was numerical, it has been converted to
## categorical. In order to avoid this warning, first convert your
## explanatory variable to a categorical variable using the as.factor()
## function.
## Response variable: categorical, Explanatory variable: categorical
## Difference between two proportions -- success: atheist
## Summary statistics:
## x
## y 2005 2012 Sum
## atheist 115 103 218
## non-atheist 1031 1042 2073
## Sum 1146 1145 2291
## Observed difference between proportions (2005-2012) = 0.0104
## Check conditions:
## 2005 : number of successes = 115 ; number of failures = 1031
## 2012 : number of successes = 103 ; number of failures = 1042
## Standard error = 0.0123
## 95 % Confidence interval = ( -0.0136 , 0.0344 )
The question of atheism was asked by WIN-Gallup International in a similar survey that was conducted in 2005. Table 4 on page 13 of the report summarizes survey results from 2005 and 2012 for 39 countries.
Use the inference function to answer the following question. As always, write out the hypotheses for any tests you conduct and outline the status of the conditions for inference. The atheism data set is already loaded in the workspace.
There is convincing evidence that Spain has seen a change in its atheism index between 2005 and 2012.
FALSE
95 % Confidence interval = ( -0.0136 , 0.0344 ) includes 0
Will this situation be the same in the United States?
Let’s perform the same analysis for the US.
us = subset(atheism, atheism$nationality == "United States")
proportion = sum(us$response == 'atheist') / length(us$response)
inference(us$response, us$year, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Warning: Explanatory variable was numerical, it has been converted to
## categorical. In order to avoid this warning, first convert your
## explanatory variable to a categorical variable using the as.factor()
## function.
## Response variable: categorical, Explanatory variable: categorical
## Difference between two proportions -- success: atheist
## Summary statistics:
## x
## y 2005 2012 Sum
## atheist 10 50 60
## non-atheist 992 952 1944
## Sum 1002 1002 2004
## Observed difference between proportions (2005-2012) = -0.0399
## Check conditions:
## 2005 : number of successes = 10 ; number of failures = 992
## 2012 : number of successes = 50 ; number of failures = 952
## Standard error = 0.0076
## 95 % Confidence interval = ( -0.0547 , -0.0251 )
There is convincing evidence that the United States has seen a change in its atheism index between 2005 and 2012.
TRUE
95 % Confidence interval = ( -0.0547 , -0.0251 )
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?
1.95
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
plot(me ~ p, type='l')
How many people would you have to sample to ensure that you are within the guidelines?
Refer to the plot of the relationship between \(p\) and margin of error \(\text{ME} = 1.96 \times \text{SE} = 1.96 \times \sqrt {p(1-p)/n}\). Do not use the data set to answer this question.
At least 9604 people
Good job! With this, you’ve successfully completed this lab! Looking forward seeing you again for the next lab!