pop <- c(30,28,29,30) flu <- c(10,5,4,1) names(pop) <- paste(1:4,"組",sep="") fisher.test(cbind(flu,pop-flu)) # // 28 December 2009 // # 青木繁伸先生からいただいた,pairwise.prop.testの改変によるコード # 自作のものより表示が見やすいし,検定の多重性の調整をするときに # 有意水準を超えた場合の扱いが妥当なので,こちらをお薦めする。 pairwise.fisher.test <- function (x, n, p.adjust.method = p.adjust.methods, ...) { p.adjust.method <- match.arg(p.adjust.method) METHOD <- "Pairwise comparison of proportions (Fisher)" DNAME <- deparse(substitute(x)) if (is.matrix(x)) { if (ncol(x) != 2) stop("'x' must have 2 columns") n <- rowSums(x) x <- x[, 1] } else { DNAME <- paste(DNAME, "out of", deparse(substitute(n))) if (length(x) != length(n)) stop("'x' and 'n' must have the same length") } OK <- complete.cases(x, n) x <- x[OK] n <- n[OK] if (length(x) < 2) stop("too few groups") compare.levels <- function(i, j) { fisher.test(cbind(x[c(i, j)], n[c(i, j)]-x[c(i, j)]), ...)$p.value } level.names <- names(x) if (is.null(level.names)) level.names <- seq_along(x) PVAL <- pairwise.table(compare.levels, level.names, p.adjust.method) ans <- list(method = METHOD, data.name = DNAME, p.value = PVAL, p.adjust.method = p.adjust.method) class(ans) <- "pairwise.htest" ans } pairwise.fisher.test(flu,pop)