library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
download.file("http://www.openintro.org/stat/data/atheism.RData", destfile = "atheism.RData")
load("atheism.RData")

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?

They are sample statistics since the poll the data was calculated from was answered by more or less 50,000 men and women (not the entire planet). In addition, these questions were asked in 57 countries, which are a sample of approximately 196 countries in 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?

We have to assume independence and that the sample sizes of each country is less than 10% of the population. I think it is reasonable to assume both since it is unlikely that one individual’s answer to the poll would affect another participant’s answer. Also, the approximate sample size of the poll was 50,000 people, which is less than 10% of the world population.

Exercise 3

What does each row of Table 6 correspond to? What does each row of atheism correspond to?

Each row is a country that was sampled and each row of atheism corresponds to the percent of each country’s sample that identified as a convinced 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")
nrow(filter(us12, us12$response == "atheist"))/nrow(us12)
## [1] 0.0499002

The proportion does agree with the percentage in Table 6 because even though it is not exactly 5%, it is very close.

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 samples are independent, shown through the random sample’s size for the US being 1,002 (which is less than 10% of the US population). Also, the success-failure condition (np >= 10) is met since 1002(.05)= 50.1. The conditions for inference are met.

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?

SE = 0.069
ME = 1.96 * SE
ME
## [1] 0.13524

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.

The countries chosen were Japan and Iceland. The conditions for inference (independence and the success-failure condition) were met. The sample size for Japan was 1,200 (which is less than 10% of the population). For Iceland, the sample size was 852 (also less than 10% of the population). The success-failure condition (np >= 10) for Japan was met since 1200(.05)= 60 and the success-failure condition for Iceland was also more than 10 since 852(.05)= 42.6 which is greater than 10.

Jap12 = subset(atheism, nationality == "Japan" & year == "2012")
inference(Jap12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist 
## Summary statistics:

## p_hat = 0.3069 ;  n = 1212 
## Check conditions: number of successes = 372 ; number of failures = 840 
## Standard error = 0.0132 
## 95 % Confidence interval = ( 0.281 , 0.3329 )
SE = 0.0132
ME = 1.96 * SE
ME
## [1] 0.025872

For Japan, the 95% confidence interval is (0.281 , 0.3329) with a margin of error of 0.0259.

Ice12 = subset(atheism, nationality == "Iceland" & year == "2012")
inference(Ice12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single proportion -- success: atheist 
## Summary statistics:

## p_hat = 0.0998 ;  n = 852 
## Check conditions: number of successes = 85 ; number of failures = 767 
## Standard error = 0.0103 
## 95 % Confidence interval = ( 0.0796 , 0.1199 )
SE = 0.0103
ME = 1.96 * SE
ME
## [1] 0.020188

For Iceland, the 95% confidence interval is (0.0796 , 0.1199) with a margin of error of 0.0202.

Exercise 8

Describe the relationship between p and me.

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")

The margin of error increases until the population proportion reaches 50%. At 50% the margin of error is at its maximum. After that, as the population proportion increases, the margin of error decreases.

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.

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
}

summary(p_hats)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.07019 0.09327 0.09904 0.09969 0.10580 0.12980
hist(p_hats, main = "p = 0.1, n = 1040", xlim = c(0, 0.18))

The sampling distribution appears to be normal and is unimodal with a mean of 0.1 (which is equal to the population mean of 0.1).

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. 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?

par(mfrow = c(2, 2))

## n = 1040, p = 0.1
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))



## n = 1040, p = 0.02
p= 0.02
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.02, n = 1040", xlim = c(0, 0.18))



## n = 400, p = 0.1
p= 0.1
n= 400
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 = 400", xlim = c(0, 0.18))



## n = 400, p = 0.02
p= 0.02
n= 400
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.02, n = 400", xlim = c(0, 0.18))

par(mfrow = c(1, 1))

The sample size and p appear to affect the spread. As sample size increases, the spread decreases. On the other hand, as p increases, so does the spread.

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? As far as distributions are concerned, both appear relatively normal and both samples are less than 10% of each country’s population, however where Australia passes the sucess-failure condition [1040(.1) = 104 >= 10], Ecuador does not [400(.02) = 8 which is not >= 10]. In that respect, I would not proceed as the report did.

ON YOUR OWN

1. Use the inference function, 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?

H0: proportion of Spain’s atheists in 2005 = proportion of Spain’s atheists in 2012

Ha: proportion of Spain’s atheists in 2005 != proportion of Spain’s atheists in 2012

Spain05 = filter(atheism, nationality == "Spain" & year == "2005")
Spain12 = filter(atheism, nationality == "Spain" & year == "2012")
nrow(Spain05)
## [1] 1146
nrow(Spain12)
## [1] 1145

Inference conditions:

  1. Independent. The sample sizes were 1145 and 1146 (less than 10% of the population).
  2. Success-failure condition. The success-failure condition (np >= 10) was met since 1146(.05)= 57.3 and 1145(.05)= 57.25
spain = atheism %>% 
  filter(year == 2005 || year == 2012) %>% 
  filter(nationality == "Spain")

inference(spain$response, x = spain$year, est = "proportion", type = "ht", null = 0,  alternative = "twosided",  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
## 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

Since the p-value is as high as 0.3966, we fail to reject the null hypothesis that the proportions of atheists to non-atheists is not different from 2005 to 2012. There is not enough convincing evidence to state that Spain has seen a change in its atheism index.

b. Is there convincing evidence that the United States has seen a change in its atheism index between 2005 and 2012?

H0: proportion of US’s atheists in 2005 = proportion of US’s atheists in 2012

Ha: proportion of US’s atheists in 2005 != proportion of US’s atheists in 2012

US05 = filter(atheism, nationality == "United States" & year == "2005")
US12 = filter(atheism, nationality == "United States" & year == "2012")
nrow(US05)
## [1] 1002
nrow(US12)
## [1] 1002

Inference conditions:

  1. Independent. The sample sizes were 1002 (less than 10% of the population).
  2. Success-failure condition. The success-failure condition (np >= 10) was met since 1002(.05)= 50.1
US = atheism %>% 
  filter(year == 2005 || year == 2012) %>% 
  filter(nationality == "United States")

inference(US$response, x = US$year, est = "proportion", type = "ht", null = 0,  alternative = "twosided",  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
## 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

With a p-value so low it appears as zero, we reject the null hypothesis that there is no difference between the proportions of atheists to non-atheists from 2005 to 2012. There is enough convincing evidence to state that the US has seen a change in its atheism index.

2. 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?

Detecting a change when there is not change is a Type I error (rejecting H0 when H0 is true). This is expected to occur but hopefully not surpass 5% of the time. With 39 countries in Table 4, we would expect to detect a change in [39*0.05 = 1.95] approximately 2 countries.

3. 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?

We assume that p = 0.5, since that is where the ME is maximized. Since “ME=1.96*SE" and “SE = sqrt((p*(1-p))/n)“, then the final formula is 0.01= 1.96 x sqrt((p*(1-p))/n). Finally, we solve for n.

p= 0.5
n = (p*(1-p))/((0.01/1.96)^2)
n
## [1] 9604

We would have to sample 9,604 individuals to be within the guidelines.