代表値の差の多重比較(対比較) Last modified: Nov 26, 2008
目的
pairwise.t.test を土台として,代表値の差の多重比較を行う
使用法
pairwise.wilcox.test <- function (x, g, p.adjust.method = p.adjust.methods, paired = FALSE, exact=TRUE, ...)
引数
exact 同順位がある場合に coin ライブラリの wilcox_tets で正確な P 値を得る(デフォルトで TRUE)
それ以外の引数は,pairwise.t.test の引数に準ずる
ソース
インストールは,以下の 1 行をコピーし,R コンソールにペーストする
source("http://aoki2.si.gunma-u.ac.jp/R/src/pairwise.wilcox.test.R", encoding="euc-jp")
# 代表値の差の多重比較(対比較)
pairwise.wilcox.test <- function (x, g, p.adjust.method = p.adjust.methods,
paired = FALSE, exact=TRUE, ...)
{
DNAME <- paste(deparse(substitute(x)), "and", deparse(substitute(g)))
g <- factor(g)
p.adjust.method <- match.arg(p.adjust.method)
METHOD <- if (paired) "paired wicoxon tests" else "wilcoxon tests"
compare.levels <- function(i, j) {
xi <- x[as.integer(g) == i]
xj <- x[as.integer(g) == j]
if (paired) {
ok <- complete.cases(xi, xj)
xi <- xi[ok]
xj <- xj[ok]
}
else {
xi <- xi[!is.na(xi)]
xj <- xj[!is.na(xj)]
}
if (exact && !paired) {
library(coin)
x <- c(xi, xj)
g <- factor(rep(1:2, c(length(xi), length(xj))))
pvalue(wilcox_test(x ~ g, distribution="exact"))
}
else
wilcox.test(xi, xj, paired = paired, ...)$p.value
}
PVAL <- pairwise.table(compare.levels, levels(g), p.adjust.method)
ans <- list(method = METHOD, data.name = DNAME, p.value = PVAL,
p.adjust.method = p.adjust.method)
class(ans) <- "pairwise.htest"
ans
}
使用例
> attach(airquality)
The following object(s) are masked _by_ .GlobalEnv :
Month
> Month <- factor(Month, labels = month.abb[5:9])
> pairwise.wilcox.test(Ozone, Month)
Pairwise comparisons using wilcoxon tests
data: Ozone and Month
May Jun Jul Aug
Jun 0.57884 - - -
Jul 0.00011 0.07103 - -
Aug 0.00055 0.11668 1.00000 -
Sep 0.47271 1.00000 0.00423 0.01900
P value adjustment method: holm
> pairwise.t.test(Ozone, Month)
Pairwise comparisons using t tests with pooled SD
data: Ozone and Month
May Jun Jul Aug
Jun 1.00000 - - -
Jul 0.00026 0.05113 - -
Aug 0.00019 0.04987 1.00000 -
Sep 1.00000 1.00000 0.00488 0.00388
P value adjustment method: holm
> pairwise.wilcox.test(Ozone, Month, p.adj = "bonf")
Pairwise comparisons using wilcoxon tests
data: Ozone and Month
May Jun Jul Aug
Jun 1.00000 - - -
Jul 0.00011 0.11838 - -
Aug 0.00061 0.23336 1.00000 -
Sep 1.00000 1.00000 0.00528 0.02715
P value adjustment method: bonferroni
> pairwise.t.test(Ozone, Month, p.adj = "bonf")
Pairwise comparisons using t tests with pooled SD
data: Ozone and Month
May Jun Jul Aug
Jun 1.00000 - - -
Jul 0.00029 0.10225 - -
Aug 0.00019 0.08312 1.00000 -
Sep 1.00000 1.00000 0.00697 0.00485
P value adjustment method: bonferroni
> detach()
直前のページへ戻る
E-mail to Shigenobu AOKI