Zachary Herold Novermber 5, 2018
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 3rd Edition. You can read this by typing
## vignette('os3') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
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.
Take a moment to review the report then address the following questions.
These findings are sample statistics. The population in question is the entire human population, and direct calculation of global population parameters are prohibitively expensive to measure.
To generalize the results, we must assume that the samples chosen are representative of the overall population and randomly selected. There are several forms of bias (including reporting bias) which make the results somewhat skeptical.
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.
atheism
correspond to?Each row represents a country in which the religiosity survey was taken.
## 'data.frame': 88032 obs. of 3 variables:
## $ nationality: Factor w/ 57 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ response : Factor w/ 2 levels "atheist","non-atheist": 2 2 2 2 2 2 2 2 2 2 ...
## $ year : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ...
In the atheism.Rdata dataset, there are 88032 rows, each an observation in a particular country, with a particular self-categorization, and a year.
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")
sum(us12$response == "atheist") / nrow(us12)
## [1] 0.0499002
Table 6 shows the proportion of self-proclaimed atheists to be 5%, matching the result we derived from the dataframe after rounding.
Conditions for inference are as follows:
(1.) Random sample or randomized experiment (2.) ONE of the following: a) population is normally distributed b) sample size is large for CLT c) sample data shows evidence of normality with no outliers (boxplot, normal probability plot) (3.) Independent (N > 10n)
## [1] 2004
We assume that the methodology of the survey was such to provide a random sample. The sample size is more than sufficient for the Central Limit Theorm to apply. Also independence is maintained with the sample size of 2004 constituting less than 10% of the overall population of atheists.
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.
## Warning: package 'BHH2' was built under R version 3.5.1
## Warning: package 'lmPerm' was built under R version 3.5.1
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 )
## [1] 0.0135
It is 1.35%.
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.## [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
## 57 Levels: Afghanistan Argentina Armenia Australia Austria ... Vietnam
India12 <- subset(atheism, nationality == "India" & year == "2012")
inference(India12$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 )
## [1] 0.0101
For India, the 95 % Confidence interval is ( 0.0201 , 0.0404 ), with a margin of error of 0.0101. Conditions of inference are met.
Germany12 <- subset(atheism, nationality == "Germany" & year == "2012")
inference(Germany12$response, est = "proportion", type = "ci", method = "theoretical",
success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.1494 ; n = 502
## Check conditions: number of successes = 75 ; number of failures = 427
## Standard error = 0.0159
## 95 % Confidence interval = ( 0.1182 , 0.1806 )
## [1] 0.0312
For Germany, the 95 % Confidence interval is ( 0.1182 , 0.1806 ), with a margin of error of 0.0312. Conditions of inference are met.
How does the proportion affect the margin of error?
We plot the two vectors against each other to reveal the relationship between probability and 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")
p
and me
.ME is at its max when p * (1-p) is at its max.
The first derivative of p * p^2 is equal to 1 - 2p, which equals 0 when maximized. This can be calculated when p = 0.5. As the proportion deviates from 50%, the Margin of Error diminishes.
Success-failure condition
Here we 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))
mean
to calculate summary statistics.## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.07019 0.09327 0.09904 0.09969 0.10577 0.12981
## [1] 0.009287382
Both the median and mean are close to the point estimate of 0.10. The curve seems to be normal, as the min and max are slightly over 3 standard deviations from the mean. The shape is unimodal and nearly symmetrical.
## [1] 3.242861
## [1] -3.176104
p2 <- 0.1
n2 <- 400
p_hats2 <- rep(0, 5000)
for(i in 1:5000){
samp2 <- sample(c("atheist", "non_atheist"), n2, replace = TRUE, prob = c(p2, 1-p2))
p_hats2[i] <- sum(samp2 == "atheist")/n2
}
p3 <- 0.2
n3 <- 1040
p_hats3 <- rep(0, 5000)
for(i in 1:5000){
samp3 <- sample(c("atheist", "non_atheist"), n3, replace = TRUE, prob = c(p3, 1-p3))
p_hats3[i] <- sum(samp3 == "atheist")/n3
}
p4 <- 0.2
n4 <- 400
p_hats4 <- rep(0, 5000)
for(i in 1:5000){
samp4 <- sample(c("atheist", "non_atheist"), n4, replace = TRUE, prob = c(p4, 1-p4))
p_hats4[i] <- sum(samp4 == "atheist")/n4
}
Describe the three new sampling distributions.
par(mfrow = c(2, 2))
hist(p_hats, main = "p = 0.1, n = 1040", xlim = c(0, 0.18), ylim = c(0,1500))
hist(p_hats2, main = "p = 0.1, n = 400", xlim = c(0, 0.18), ylim = c(0,1500))
hist(p_hats3, main = "p = 0.2, n = 1040", xlim = c(0, 0.40), ylim = c(0,1500))
hist(p_hats4, main = "p = 0.2, n = 400", xlim = c(0, 0.40), ylim = c(0,1500))
Based on these limited plots, how does \(n\) appear to affect the distribution of \(\hat{p}\)? How does \(p\) affect the sampling distribution?
As sample size (n) increases, the tightness about the sample mean increases. The spread increases as n decreases. The probability largely determines the sample mean, and thus the point estimate.
## [1] 1039
## [1] 804
Yes. The sample size is large enough for the CLT to apply, but not too large that independence no longer holds. Randomness is assumed.
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.
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 )
## [1] 0.0166
atheism05 <- read.csv("C:/Users/ZacharyHerold/Documents/DATA606/Lab6/atheism05.csv")
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 )
The inference function gives the 95 % Confidence interval of Spain’s level of atheism in 2012 as ( 0.0734 , 0.1065 ).
In 2005, the 95 % Confidence interval was ( 0.083 , 0.1177 ).
The two ranges overlap from 0.083 to 0.1065.
Due to the significant overlap there is not sufficient evidence that Spain’s religiosity has changed.
**b.** Is there convincing evidence that the United States has seen a
change in its atheism index between 2005 and 2012?
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 )
95 % Confidence interval (2012) = ( 0.0364 , 0.0634 )
95 % Confidence interval (2005) = ( 0.0038 , 0.0161 )
These confidence intervals do not intersect, suggesting that there was been a qualitative change in the religiosity of Americans.
A Type 1 error occurs when the null hypothesis is incorrectly rejected. If the atheism index is unchanged, it is still possible that we would see confidence intervals that do not overlap, though they should. This would likely occur less than 2.5% of the time by chance alone, with upper bounds failing to cross lower bounds.
## [1] 9604
A sample size of 9604 residents would be required to ensure the 1% Margin of Error. This is calculated conservatively after maximizing p(1-p), with p of 50%.