download.file("http://www.openintro.org/stat/data/atheism.RData", destfile = "atheism.RData")
load("atheism.RData")
us12 <- subset(atheism, nationality == "United States" & year == "2012")
us12A <- subset(atheism, nationality == "United States" & year == "2012" & response == "atheist")
nrow(us12A)/nrow(us12)
## [1] 0.0499002
inference(us12$response, est = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Warning: package 'BHH2' was built under R version 3.6.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 )
1.96*0.0069
## [1] 0.013524
Fran12 <- subset(atheism, nationality == "France" & year == "2012")
Fran12A <- subset(atheism, nationality == "France" & year == "2012" & response == "atheist")
nrow(Fran12A)/nrow(Fran12)
## [1] 0.2873223
inference(Fran12$response, est = "proportion", type = "ci", method = "theoretical",
success = "atheist")
## Single proportion -- success: atheist
## Summary statistics:
## p_hat = 0.2873 ; n = 1688
## Check conditions: number of successes = 485 ; number of failures = 1203
## Standard error = 0.011
## 95 % Confidence interval = ( 0.2657 , 0.3089 )
Germ12 <- subset(atheism, nationality == "Germany" & year == "2012")
Germ12A <- subset(atheism, nationality == "Germany" & year == "2012" & response == "atheist")
nrow(Germ12A)/nrow(Germ12)
## [1] 0.1494024
inference(Germ12$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.96*0.2873223
## [1] 0.5631517
1.96*0.1494024
## [1] 0.2928287
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 <- 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))
###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.
par(mfrow = c(2, 2))
hist(p_hats, main = "p = 0.1, n = 1040", xlim = c(0, 0.18))
p <- 0.1
n <- 400
p_hats2 <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats2[i] <- sum(samp == "atheist")/n
}
hist(p_hats2, main = "p = 0.1, n = 400", xlim = c(0, 0.18))
p <- 0.02
n <- 1040
p_hats3 <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats3[i] <- sum(samp == "atheist")/n
}
hist(p_hats3, main = "p = 0.02, n = 1040", xlim = c(0, 0.18))
p <- 0.02
n <- 400
p_hats4 <- rep(0, 5000)
for(i in 1:5000){
samp <- sample(c("atheist", "non_atheist"), n, replace = TRUE, prob = c(p, 1-p))
p_hats4[i] <- sum(samp == "atheist")/n
}
hist(p_hats4, main = "p = 0.02, n = 400", xlim = c(0, 0.18))