## Seminar 5. Confidence intervals ## ### Proportion ### # install library for confidence intervals install.packages("DescTools") # load 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 # look and interpret ci90 <- BinomCI(30, 100, conf.level = 0.90) ci90 # calculate 95% confidence interval & look ci95 <- BinomCI(30, 100, conf.level = 0.95) ci95 # calculate 99% confidence interval & look ci99 <- BinomCI(30, 100, conf.level = 0.99, method = "clopper-pearson") ci99 # calculate lengths of each interval and compare ci90[3] - ci90[2] ci95[3] - ci95[2] ci99[3] - ci99[2] # 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) ci90n # calculate its length and compare with 90% inteval for n = 100 ci90n[3] - ci90n[2] ci90[3] - ci90[2] # 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) # how many people for & against table(df$vote) yes <- 868 no <- 889 BinomCI(yes, yes + no, conf.level = 0.95) ### Mean ### # choose people who voted for voted <- df[df$vote == "Y", ] # calculate confidence interval for the age of these people MeanCI(voted$age)