##### CONFIDENCE INTERVALS #####
### Proportion ###
# install library for confidence intervals
install.packages("DescTools")
# call the library
library(DescTools)
# we asked 100 people and found that 30 people approved of the capital punishment
# n = 100, p = 0.3
# calculate 90% confidence interval
ci90 <- BinomCI(30, 100, conf.level = 0.90, method = "clopper-pearson")
# look and interpret
ci90
# calculate 95% confidence interval
ci95 <- BinomCI(30, 100, conf.level = 0.95, method = "clopper-pearson")
# look
ci95
# calculate 99% confidence interval
ci99 <- BinomCI(30, 100, conf.level = 0.99, method = "clopper-pearson")
# look
ci99
# calculate lengths of each interval and compare
l90 <- ci90[3] - ci90[2]
l90
l95 <- ci95[3] - ci95[2]
l95
l99 <- ci99[3] - ci99[2]
l99
# now we asked 200 people and found that 60 people approved of the capital punishment
# n = 200, p = 0.3
# calculate 90% confidence interval
ci90n <- BinomCI(60, 200, conf.level = 0.90, method = "clopper-pearson")
ci90n
# calculate its length and compare with 90% inteval for n = 100
l90n <- ci90n[3] - ci90n[2]
l90n
l90
# for real data - Chile
df <- read.csv("http://math-info.hse.ru/f/2017-18/ps-ms/Chile.csv")
df <- na.omit(df)
View(df)
table(df$vote)
yes = 868
no = 889
BinomCI(yes, yes + no, conf.level = 0.95, method = "clopper-pearson")
### Mean ###
# choose people ho voted for
voted <- df[df$vote == "Y", ]
# calculate confidence interval for the age of these people
MeanCI(voted$age)
# interpret
##### Hypotheses Testing #####
### proportion ###
prop.test(yes, yes+no, p = 0.5, alternative = "two.sided")
prop.test(yes, yes+no, p = 0.8, alternative = "two.sided")
prop.test(yes, yes+no, p = 0.8, alternative = "less")
prop.test(yes, yes+no, p = 0.8, alternative = "greater")
### mean ###
t.test(voted$age, mu = 25, alternative = "two.sided")
t.test(voted$age, mu = 40, alternative = "two.sided")