getwd()
## [1] "C:/Users/Jerome/Documents/0000_Work_Files/0000_Coursera/Statistics_with_R_Specialization/Course_2_Inferential_Stats/Week4"
library(statsr)
## Warning: package 'statsr' was built under R version 4.0.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.2
##
## 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
library(ggplot2)
library(tables)
## Warning: package 'tables' was built under R version 4.0.5
data(atheism)
write.csv (atheism, file = "atheism.csv", row.names = FALSE)
atheism <- read.csv("atheism.csv", header = TRUE)
us12 <- atheism %>%
filter(nationality == "United States" , atheism$year == "2012")
write.csv (us12, file = "us12.csv", row.names = FALSE)
us12 <- read.csv("us12.csv", header = TRUE)
table (us12$response)
##
## atheist non-atheist
## 50 952
data("atheism")
library("dplyr")
us12 <- atheism %>%
filter(nationality == "United States" , atheism$year == "2012")
inference(y = response, data = us12, statistic = "proportion",
type = "ci",
method = "theoretical",
success = "atheist")
## Single categorical variable, success: atheist
## n = 1002, p-hat = 0.0499
## 95% CI: (0.0364 , 0.0634)

inference(y = response, data = us12, statistic = "proportion", type = "ci", method = "theoretical", success = "atheist")
## Single categorical variable, success: atheist
## n = 1002, p-hat = 0.0499
## 95% CI: (0.0364 , 0.0634)

france12 <- atheism %>%
filter(nationality == "France" , atheism$year == "2012")
china12 <- atheism %>%
filter(nationality == "China" , atheism$year == "2012")
### ME and p
d <- data.frame(p <- seq(0, 1, 0.01))
n <- 1000
d <- d %>%
mutate(me = 1.96*sqrt(p*(1 - p)/n))
ggplot(d, aes(x = p, y = me)) +
geom_line()

spain <- atheism %>%
filter(nationality == "Spain")
write.csv (spain, file = "spain.csv", row.names = FALSE)
spain <- read.csv("spain.csv", header = TRUE)
table(spain$year)
##
## 2005 2012
## 1146 1145
prop.test(table(spain$year), correct=FALSE)
##
## 1-sample proportions test without continuity correction
##
## data: table(spain$year), null probability 0.5
## X-squared = 0.00043649, df = 1, p-value = 0.9833
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.4797609 0.5206749
## sample estimates:
## p
## 0.5002182
us_both <- atheism %>%
filter(nationality == "United States" )
write.csv (us_both, file = "us_both.csv", row.names = FALSE)
us_both <- read.csv("us_both.csv", header = TRUE)
table(us_both$year)
##
## 2005 2012
## 1002 1002
prop.test(table(us_both$year), correct=FALSE)
##
## 1-sample proportions test without continuity correction
##
## data: table(us_both$year), null probability 0.5
## X-squared = 0, df = 1, p-value = 1
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.4781298 0.5218702
## sample estimates:
## p
## 0.5