odds.ratio <- function(t, alternative="two.sided", conf.level=0.95, mu=1) { null.value <- mu if(mu <= 0) { stop("The null.value must be positive."); } res <- list(method="Odds Ratio test") OR <- ( t[1,1]*t[2,2] )/( t[1,2]*t[2,1] ) lOR <- log(OR) vOR <- sum(1/t) TS <- (lOR-log(mu)) / sqrt(vOR) if(alternative=="two.sided") { res$alternative <- "two.sided" p <- pnorm(-abs(TS))*2 ucl <- round(exp( lOR - qnorm( (1-conf.level)/2 )*sqrt(vOR) ),4) lcl <- round(exp( lOR + qnorm( (1-conf.level)/2 )*sqrt(vOR) ),4) } if(alternative=="less") { res$alternative <- "less" p <- pnorm(-abs(TS)) ucl <- "+ Inf" lcl <- round(exp( lOR + qnorm( (1-conf.level) )*sqrt(vOR) ),4) } if(alternative=="greater") { res$alternative <- "greater" p <- pnorm(abs(TS)) ucl <- round( exp( lOR - qnorm( (1-conf.level) )*sqrt(vOR) ),4) lcl <- "- Inf" } res$odds.ratio <- OR res$method <- "Odds Ratio Test" res$data.name <- deparse(substitute(y)) res$statistic <- TS names(res$statistic) <- "Z" res$estimate <- OR names(res$estimate) <- "Odds Ratio" res$p.value <- p res$conf.int <- c(lcl,ucl) attr(res$conf.int,"conf.level") <- conf.level res$null.value <- mu class(res) <- "htest" return(res) }