###### Code from examples worked in class ###### ## California governor X^2 example recall <- c(46.7, 20.1, 8, 25.2) seats <- c(45.8, 33.1, 10.2, 10.9) seats.prop <- seats / sum(seats) expected <- 100 * seats.prop 1 - pchisq(chi2, df=4-1) 1 - pchisq(chi2, df=4-1) # compare to: chisq.test(recall, p=seats.prop, correct=FALSE) chisq.test(recall, p=seats.prop) cbind(poll/787*100, actual) ## Democracy and war example x <- matrix(c(40,74,3,11), nrow=2, byrow=T) colnames(x) <- c("Democracy", "Dictatorship") rownames(x) <- c("Peace", "War") chisq.test(x) ## Fisher exact test on Dem & War example fisher.test(x) # compare to: chisq.test(x, simulate=TRUE) ## Democraticness pre- v. post-1945 free <- matrix(byrow=T, nrow=2, c(17, 8, 6, 6, 11, 9, 3, 1, 6, 9, 8, 17, 21, 6)) rownames(free) <- c("Pre45", "Post45") colnames(free) <- 1:7 free chisq.test(free) ## Convert previous example to a dataset format yearf <- factor(year, labels=c("Pre","post")) freedom <- c(rep(1,17), rep(2,8), rep(3,6), rep(4,6),rep(5,11),rep(6,9),rep(7,3), rep(1,1), rep(2,6), rep(3,9), rep(4,8),rep(5,17),rep(6,21),rep(7,6)) df <- data.frame(yearf,freedom) table(df) t.test(freedom ~ yearf) # violates the t-test assumptions! wilcox.test(freedom ~ yearf) median(df$freedom[df$yearf=="Pre"]) median(df$freedom[df$yearf=="post"])