################### #### Chapter 2 #### ################### # ============================= ## Comparing two proportions ## # ============================= # Example (heart attack v. no attack) x<-c(104,189) # aspirin, placebo n<-c(11037,11034) # test H0:p1=p2 (equal probabilities of heart attack) prop.test(x,n,p=c(.5,.5)) prop.test(x,n)$p.value # Test H0:p1=p2 v. H1:p140000") jobsat<-c("VD","LD","MS","VS") table.2.8<-expand.grid(income=income,jobsat=jobsat) Data<-c(1,2,1,0,3,3,6,1,10,10,14,9,6,7,12,11) table.2.8<-cbind(table.2.8,count=Data) (temp<-xtabs(count~income+jobsat,table.2.8)) library(vcd) assocstats(temp) # ======================== ## Confidence intervals ## # ========================= library(methods) Drug<-c("Placebo","Aspirin") Infarction<-c("yes","no") table.3.1<-expand.grid(drug=Drug,infarction=Infarction) Data<-c(28,18,656,658) table.3.1<-cbind(table.3.1,count=Data) tapply(table.3.1$count,table.3.1[,1:2], sum) # wald confidence interval Wald.ci<-function(Table, aff.response, alpha=.05){ # Gives two-sided Wald CI's for odds ratio, difference in proportions and relative risk. # Table is a 2x2 table of counts with rows giving the treatment populations # aff.response is a string like "c(1,1)" giving the cell of the beneficial response and the treatment category # alpha is significance level pow<-function(x, a=-1) x^a z.alpha<-qnorm(1-alpha/2) if(is.character(aff.response)) where<-eval(parse(text=aff.response)) else where<-aff.response Next<-as.numeric(where==1) + 1 # OR odds.ratio<-Table[where[1],where[2]]*Table[Next[1],Next[2]]/(Table[where[1],Next[2]]*Table[Next[1],where[2]]) se.OR<-sqrt(sum(pow(Table))) ci.OR<-exp(log(odds.ratio) + c(-1,1)*z.alpha*se.OR) # difference of proportions p1<-Table[where[1],where[2]]/(n1<-Table[where[1],Next[2]] + Table[where[1],where[2]]) p2<-Table[Next[1],where[2]]/(n2<-Table[Next[1],where[2]]+Table[Next[1],Next[2]]) se.diff<-sqrt(p1*(1-p1)/n1 + p2*(1-p2)/n2) ci.diff<-(p1-p2) + c(-1,1)*z.alpha*se.diff # relative risk RR<-p1/p2 se.RR<-sqrt((1-p1)/(p1*n1) + (1-p2)/(p2*n2)) ci.RR<-exp(log(RR) + c(-1,1)*z.alpha*se.RR) list(OR=list(odds.ratio=odds.ratio, CI=ci.OR), proportion.difference=list(diff=p1-p2, CI=ci.diff), relative.risk=list(relative.risk=RR,CI=ci.RR)) } Wald.ci(temp, "c(1, 1)") # or use Wald.ci(temp, c(1, 1)) # ========================= ## Tests of Independence ## # ========================= # Table 3.2 (p.80) # set up data and expected counts religion.counts<-c(178,138,108,570,648,442,138,252,252) table.3.2<-cbind(expand.grid(list(Religious.Beliefs=c("Fund", "Mod", "Lib"), Highest.Degree=c("= S-PLUS 6.0) if(is.null(version$language) && inherits(x, "crosstabs")) { oldClass(x)<-NULL; attr(x, "marginals")<-NULL} n <- nrow(x) m <- ncol(x) pi.c<-pi.d<-matrix(0,nr=n,nc=m) row.x<-row(x) col.x<-col(x) for(i in 1:(n)){ for(j in 1:(m)){ pi.c[i, j]<-sum(x[row.xi & col.x>j]) pi.d[i, j]<-sum(x[row.xj]) + sum(x[row.x>i & col.x 2 && is.null(stratum)) stratum <- 3:l if (l - length(stratum) > 2) stop("All but 2 dimensions must be specified as strata.") if (l == 2 && dim(x) != c(2, 2)) stop("Not a 2 x 2 - table.") if (!is.null(stratum) && dim(x)[-stratum] != c(2, 2)) stop("Need strata of 2 x 2 - tables.") lor <- function(y, correct, Log) { if(correct) y<-y + 0.5 or <- y[1, 1] * y[2, 2]/y[1, 2]/y[2, 1] if (Log) log(or) else or } ase <- function(y,correct) sqrt(sum(1/(ifelse(correct,y + 0.5,y)))) if (is.null(stratum)) { LOR <- lor(x, correct) ASE <- ase(x) } else { LOR <- apply(x, stratum, lor, correct=correct, Log=Log) ASE <- apply(x, stratum, ase, correct=correct) } I <- ASE * qnorm((1 + conf.level)/2) Z <- LOR/ASE structure(LOR, ASE = if (Log) ASE, lwr = if (Log) LOR - I else exp(log(LOR) - I), upr = if (Log) LOR + I else exp(log(LOR) + I), Z = if (Log) Z, P = if (Log) 1 - pnorm(abs(Z)), log = Log, class = "oddsratio") } oddsratio.L(table.6.9.array, correct=F, Log=F) # CMH mantelhaen.test(table.6.9.array, correct=F) # ============================================ # McNemar Test - Compare Dependent Proportions # ============================================ ## Comparing dependent proportions table.10.1<-matrix(c(794,150,86,570),byrow=T,ncol=2) mcnemar.test(table.10.1,correct=F) #