# R for homework 1 # Edps/Psyc/Soc 589 # Fall 2018 # C.J.Anderson ################################################## # Problem 1: 1.8 from Agresti (2007) # ################################################## # - input data yes <- 344 no <- 826 #- a few computations n <- yes + no p <- yes/n p0 <- .5 # - Observed proportion, exact test, & CI (binom.test(yes,n,alternative=c("two.sided"),conf.level=.99),correct=FALSE) # - Asymptotic test statistic. Use hypothesized value (asym <- prop.test(yes,n,p0, alternative=c("two.sided"),conf.level=.99,correct=FALSE)) # - The above gives X2, which is z**2. So to get z...Note that I attached # correction sign to z (i.e., negative becuase p < .5 names(asym) ( z<- -sqrt(asym$statistic)) # - Asymptotic 99% CI. Use data value for p: (asym <- prop.test(yes,n,p, alternative=c("two.sided"),conf.level=.99,correct=FALSE)) # - 1st principles....Since Asymptotic CI is different from SAS (zalpha.2 <- qnorm(.995)) (ase <- sqrt(p*(1-p)/n)) (lower <- p - zalpha.2*ase) (upper <- p + zalpha.2*ase) # Eight different methods to be CIs: library(binom) binom.confint(yes,n,conf.level=.99, methods="all") #################################################### # Problem 2: 2.6 from Agresti (2007) # #################################################### (p1 <- .001304) # conditional probability die lung cancer given current smoker (p2 <- .000121) # conditional probability die lung cancer given non-smoker # (a) Difference & relative risk (diff.p <- p1-p2) (rel.risk <- p1/p2) # (b) Odds ratio (theta <- (p1*(1-p2))/((1-p2)*p2)) (1-p2)/(1-p1) # odds equals rel.risk * (1-p2)/(1-p1) #################################################### # Problem 3: 2.7 from Agresti (2007) # # # # Requires a bit of algebra # #################################################### # (a) no computations # (b) # Given theta <- 11.4 # Odds ratio of survival for women vs males odds.f <- 2.9 # Odd female survives # Proportion of females who survived (p.f <- odds.f/(1+odds.f)) # Proportion of males who survived (odds.m <- odds.f/theta) (p.m <- odds.m/(1+odds.m)) # (c) # Relative risk female survives (relRisk.f <- p.f/p.m) #################################################### # Problem 4: 2.10 from Agresti (2007) # #################################################### # (a) Put data in tabular form (no computations) # (b) # Odds ratio of black slain by black vs black slain by white (odds.ratio <- (91*83)/(17*(9))) #################################################### # Problem 5: US presidential candidate 1932-2016 # #################################################### taller <- 16 n <- 20 p0 <- .5 #(a) Exact for Ho: pi=.50 # - Observed proportion, exact test, & CI (binom.test(taller,n,p0,alternative=c("two.sided"),conf.level=.95)) # (b) Asymptotic (asym5 <- prop.test(taller,n,p0,alternative=c("two.sided"),conf.level=.95,correct=FALSE)) (z <- sqrt(asym5$statistic)) # (c) Exact or asymptotic: check sample size 20 * .80 20 * .20 # (d) # - asympotic CI binom.confint(taller,n,conf.level=.95, methods="all")