# R code to analyze a 1 by 2 contingency table # and make inference about the binomial probability # We look at the driver's test data set: # The raw data as 0's and 1's: results.01 <- c(1,0,1,0,1,1,1,0,1,1,0,0,1,0,1,0,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0) # labeling the raw data as "PASS" or "FAIL": drive.test.results <- rep("PASS", length=length(results.01)) drive.test.results[results.01 == 0] <- "FAIL" print(drive.test.results) # creating a 1 by 2 contingency table based on the raw data: table(drive.test.results) # Inference about pi: # Note the number of "successes" must be listed first, # however "successes" are defined: # 95% CI for pi (the probability of a random individual passing the test): # Copy the following function for the (Wald) large-sample CI into R: #### # Wald.CI <- function(succ, fail, conf.level=0.95){ alpha <- 1-conf.level pi.hat <- succ/(succ+fail) LL <- pi.hat - qnorm(1-alpha/2)*sqrt(pi.hat*(1-pi.hat)/(succ+fail)) UL <- pi.hat + qnorm(1-alpha/2)*sqrt(pi.hat*(1-pi.hat)/(succ+fail)) my.ci <- c(round(LL,4), round(UL,4)) return(my.ci) } # #### # Run this to get 95% CI: Wald.CI(33, 17, conf.level=0.95) # The built-in R function in prop.test gives the "score" CI: prop.test(matrix(c(33,17), nrow=1, ncol=2), alternative = "two.sided", conf.level = 0.95, correct = FALSE)$conf.int # Test of whether pi is greater than 0.6: prop.test(matrix(c(33,17), nrow=1, ncol=2), p = 0.6, alternative = "greater", correct = FALSE) ######################################################################################################## # Small-sample inference about pi: # Suppose a sample of 10 trees revealed two diseased trees. # Defining a diseased tree as a "success": # Exact 95% CI for the population proportion of trees that are diseased: binom.test(matrix(c(2,8), nrow=1, ncol=2), alternative = "two.sided", conf.level = 0.95)$conf.int # Test of whether pi is less than 0.45: binom.test(matrix(c(2,8), nrow=1, ncol=2), p = 0.45, alternative = "less")