> ############### Testing a single proportion > > n <- 992; y <- 534; pi <- 0.5238 > > 1-pbinom(y-1,n,pi) # 1-P(Y<=533|Y~Bin(n=992,pi=0.5238) [1] 0.1886412 > > > ################ Comparing 2 Proportions - Independent/Large Samples > > prop.test(c(29,13),c(224,352),correct=F) # Inputs: (y1,y2),(n1,n2) 2-sample test for equality of proportions without continuity correction data: c(29, 13) out of c(224, 352) X-squared = 17.3385, df = 1, p-value = 3.128e-05 alternative hypothesis: two.sided 95 percent confidence interval: 0.04435622 0.14070871 sample estimates: prop 1 prop 2 0.12946429 0.03693182 > > > ################ Comparing 2 Proportions - Independent/Small Samples - Fisher's Exact Test > > (lister <- matrix(c(6,6,1,11),byrow=T,ncol=2)) [,1] [,2] [1,] 6 6 [2,] 1 11 > > fisher.test(lister,alt="g") Fisher's Exact Test for Count Data data: lister p-value = 0.03432 alternative hypothesis: true odds ratio is greater than 1 95 percent confidence interval: 1.16424 Inf sample estimates: odds ratio 9.910647 > > ################# Comparing 2 Proportions - Paired Samples - McNemar's Test > > (bet <- matrix(c(16,110,4,20),byrow=T,ncol=2)) [,1] [,2] [1,] 16 110 [2,] 4 20 > > mcnemar.test(bet,correct=F) McNemar's Chi-squared test data: bet McNemar's chi-squared = 98.5614, df = 1, p-value < 2.2e-16 > > ################## Goodness of Fit for Multinomial Distribution > > chisq.test(c(16,24,34,46),p=c(0.25,0.25,0.25,0.25)) ### Default probs are 1/#categories Chi-squared test for given probabilities data: c(16, 24, 34, 46) X-squared = 16.8, df = 3, p-value = 0.0007769 > > ################## Chi-Square Test > > (choice <- matrix(c(141,67,22,91,85,62),byrow=T,ncol=3)) [,1] [,2] [,3] [1,] 141 67 22 [2,] 91 85 62 > > chisq.test(choice, correct=F) Pearson's Chi-squared test data: choice X-squared = 31.8276, df = 2, p-value = 1.227e-07 > > > ################### Ordinal Association - Gamma > ### Must have set a cran mirror > > library(vcdExtra) Loading required package: vcd Loading required package: grid Loading required package: gnm > > (beer <- matrix(c(4,1,4,20,21,8,23,22,26,9,12,5),byrow=T,ncol=3)) [,1] [,2] [,3] [1,] 4 1 4 [2,] 20 21 8 [3,] 23 22 26 [4,] 9 12 5 > GKgamma(beer) gamma : 0.084 std. error : 0.105 CI : -0.121 0.289 > > ######### Equivalence of Large-Sample Z Test and Chi-Square for 2x2 Tables > > prop.test(c(59,21),c(70,68),correct=F) # Inputs: (y1,y2),(n1,n2) 2-sample test for equality of proportions without continuity correction data: c(59, 21) out of c(70, 68) X-squared = 40.3743, df = 1, p-value = 2.097e-10 alternative hypothesis: two.sided 95 percent confidence interval: 0.3950126 0.6730546 sample estimates: prop 1 prop 2 0.8428571 0.3088235 > > (gamble <- matrix(c(59,11,21,47), byrow=T, ncol=2)) [,1] [,2] [1,] 59 11 [2,] 21 47 > > chisq.test(gamble, correct=F) Pearson's Chi-squared test data: gamble X-squared = 40.3743, df = 1, p-value = 2.097e-10 > > ######### Odds Ratio > > (chips_gi <- matrix(c(89,474,93,436),byrow=T,ncol=2)) [,1] [,2] [1,] 89 474 [2,] 93 436 > > (chips_gi_or <- oddsratio(chips_gi, log=F)) [1] 0.8802686 > confint(chips_gi_or, log=F) lwr upr [1,] 0.6402248 1.210314 > > > ################# Mantel-Haenszel Test > ### Enter array as: Col 1 Table 1, Column 2 Table 1,.... > > > (sleep <- array(c(7,7,2,4,36,19,16,43,38,21,37,58),dim=c(2,2,3))) , , 1 [,1] [,2] [1,] 7 2 [2,] 7 4 , , 2 [,1] [,2] [1,] 36 16 [2,] 19 43 , , 3 [,1] [,2] [1,] 38 37 [2,] 21 58 > mantelhaen.test(sleep,exact=F,correct=F,alternative="two.sided") Mantel-Haenszel chi-squared test without continuity correction data: sleep Mantel-Haenszel X-squared = 25.095, df = 1, p-value = 5.457e-07 alternative hypothesis: true common odds ratio is not equal to 1 95 percent confidence interval: 2.119418 5.720445 sample estimates: common odds ratio 3.481956 >