Exercise 1 : 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 derived from all the interviews conducted accross the world.
Exercise 2 : 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?
load("more/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
We must assume that the interviewees were selected randomly. It is very important to the accuracy of sample statistics that one group is not overly selected vs. the others. Only simple random sampling method can truly represent the different segments of human beliefs close to the appropriate proportions of the populatioin. Sample size almost is of importance.
Exercise 3 :What does each row of Table 6 correspond to? What does each row of atheism correspond to?
Each row of Table 6 correspond to a country with response percentages for all groups, whereas each row of atheism corresponds a country with responses only related to atheist and non-atheist.
Exercise 4 : 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")
by(us12$nationality, us12$response, length)
## us12$response: atheist
## [1] 50
## --------------------------------------------------------
## us12$response: non-atheist
## [1] 952
cat(round(50/nrow(us12)*100),'%')
## 5 %
Yes It matches with the percentage for atheists in United States in Table 6.
Exercise 5 : 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 are:
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 )
Exercise 6 : 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?
In a confidence interval, z * SE is called the margin of error.
Margin_Of_Error <- 1.96* 0.0069
Margin_Of_Error
## [1] 0.013524
Exercise 7 :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.
ind12 <- subset(atheism, nationality == "India" & year == "2012")
Brz12 <- subset(atheism, nationality == "Brazil" & year == "2012")
Checking COnditions for Ind12 :
by(ind12$nationality,ind12$response,length)
## ind12$response: atheist
## [1] 33
## --------------------------------------------------------
## ind12$response: non-atheist
## [1] 1059
cat(round(33/nrow(ind12)*100),'%')
## 3 %
Sample Size is less than 10%. and The success-failure conditons requirs np >= 10 and n(1-p)>=10. np = 10920.03 = 33, n(1-p)=10920.97 = 1059 so this condition is met.
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 )
Margin_Of_Error_Ind <- 1.96* 0.0052
Margin_Of_Error_Ind
## [1] 0.010192
Checking COnditions for Brz12 :
by(Brz12$nationality,Brz12$response,length)
## Brz12$response: atheist
## [1] 20
## --------------------------------------------------------
## Brz12$response: non-atheist
## [1] 1982
cat(round(20/nrow(Brz12)*100),'%')
## 1 %
nrow(Brz12)
## [1] 2002
Sample Size is less than 10%. and The success-failure conditons requirs np >= 10 and n(1-p)>=10. np = 20020.01 = 20, n(1-p)=20020.99 = 1982 so this condition is met.
inference(Brz12$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 )
Margin_Of_Error_Brz <- 1.96* 0.0022
Margin_Of_Error_Brz
## [1] 0.004312
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.
Based on the graph above the proportion of 0.50 is the proportion with the largest margin of error possible. When the proportions move away from 0.50 and get closer to the extremes of 0.00 or 1.00, the margin of error decreases. There is an inverse Correlation between p and me as they move in opposite directions.
Exercise 9 : 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.
p <- 0.1
n <- 1040
p_hats <- rep(0, 5000)
for(i in 1:length(p_hats)){
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))
The simulation to produce 5000 samples of size 1040 sample proportions follow normal distribution with some outliers. The median and mean of the distribution are near identical at 0.1, which is also the population proportion. The range of the distribution is 0.0668 and the IQR is .0118.
summary(p_hats)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.07019 0.09327 0.09904 0.09969 0.10580 0.12980
Exercise 10 : 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 nn appear to affect the distribution of pp? How does pp affect the sampling distribution?
Since n is the denominator of p the bigger the n the smaller the p. n does not affect the center but affects the spread and shape of the distribution of sampling proportions. The spread (variability) decreases as the sample size increases. So larger samples usually give closer estimates of the population proportion p. As the sample sizes get larger the shape of the distribution also follows more the shape of normal distribtuion.
p <- 0.1
n <- 400
p_hats <- rep(0, 5000)
for (i in 1:length(p_hats)) {
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:length(p_hats)) {
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:length(p_hats)) {
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.02, n = 1040", xlim = c(0.0001, 0.05))
hist(p_hats, main = "p = 0.1, n = 400", xlim = c(0, 0.06))
hist(p_hats, main = "p = 0.02, n = 400", xlim = c(0.0001, 0.06))
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?
Aus12 <- subset(atheism, nationality == "Australia" & year == "2012")
by(Aus12$nationality, Aus12$response, length)
## Aus12$response: atheist
## [1] 104
## --------------------------------------------------------
## Aus12$response: non-atheist
## [1] 935
nrow(Aus12)
## [1] 1039
Ecu12 <- subset(atheism, nationality == "Ecuador" & year == "2012")
by(Ecu12$nationality, Ecu12$response, length)
## Ecu12$response: atheist
## [1] 8
## --------------------------------------------------------
## Ecu12$response: non-atheist
## [1] 396
nrow(Ecu12)
## [1] 404
I would feel comfortable proceeding with the inference and me for Australia as the data met the conditions for the sampling distribution of p. However, Ecuador data did not meet the success-failure condition as np is only 8, which is smaller than the 10 needed.
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.
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.
Is there convincing evidence that the United States has seen a change in its atheism index between 2005 and 2012?
Answer a.
H0: There is no convincing evidence that Spain has seen a change in its atheism index between 2005 and 2012 p2005 = p2012
HA: There is convincing evidence that Spain has seen a change in its atheism index between 2005 and 2012 p2005 is not equal to p2012
atheism$year = gsub(2005, "2005", atheism$year)
atheism$year = gsub(2012, "2012", atheism$year)
spain <- subset(atheism, atheism$nationality == "Spain")
inference(spain$response, spain$year, est = "proportion",type = "ht",null = 0, alternative = "twosided", method = "theoretical", success = "atheist")
## Response variable: categorical, Explanatory variable: categorical
## Two categorical variables
## 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
##
## H0: p_2005 - p_2012 = 0
## HA: p_2005 - p_2012 != 0
## Pooled proportion = 0.0952
## Check conditions:
## 2005 : number of expected successes = 109 ; number of expected failures = 1037
## 2012 : number of expected successes = 109 ; number of expected failures = 1036
## Standard error = 0.012
## Test statistic: Z = 0.848
## p-value = 0.3966
H0: There is no convincing evidence that US has seen a change in its atheism index between 2005 and 2012 p2005 = p2012
HA: There is convincing evidence that US has seen a change in its atheism index between 2005 and 2012 p2005 is not equal to p2012
US <- subset(atheism, atheism$nationality == "United States")
inference(US$response, US$year, est = "proportion",type = "ht",null = 0, alternative = "twosided", method = "theoretical", success = "atheist")
## Response variable: categorical, Explanatory variable: categorical
## Two categorical variables
## 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
##
## H0: p_2005 - p_2012 = 0
## HA: p_2005 - p_2012 != 0
## Pooled proportion = 0.0299
## Check conditions:
## 2005 : number of expected successes = 30 ; number of expected failures = 972
## 2012 : number of expected successes = 30 ; number of expected failures = 972
## Standard error = 0.008
## Test statistic: Z = -5.243
## p-value = 0
Answer
A type 1 error is rejecting the null hypothesis when H0 is actually true. We typically do not want to incorrectly reject H0 more than 5% of the time. This corresponds to a significance level of 0.05. Since there are 39 countries in Table 4 that summarizes survey results from 2005 to 2012 we will need to multiply 0.05 by 39 to estimate how many countries we would expect to detect a change in the atheism index simply by chance. the result is 1.95, or 2 countries.
.05*39
## [1] 1.95
Answer
There are two unknown variablies in this question: p and n. When we do not have and estimate for p we follow the guideline that the margin of error is largest when p is 0.5. So we typically use this worst case estimate if no other estimate is available. The estimate must have a margin of error no greater than 1%. We use the formula ME = zSE = 1,96 sqrt(p(1-p)/n)) <=0.01. Based on the calculation we would need at least 9604 participants to ensure the sample proportion is within 0.01 of the true porportion with 95% confidence.
P <-0.5
Z.alpha <-1.96
ME <-0.01
N<- Z.alpha^2*P*(1-P)/ME^2
N
## [1] 9604