フリードマン検定(plus 多重比較) Last modified: Dec 19, 2018
目的
R には,friedman.test 関数が用意されている。
ここで定義する関数は,フリードマン検定に引き続いて,多重比較(対比較)を行う
使用法
friedman(dat)
引数
dat データ行列(一行に複数条件下の測定値)
ソース
インストールは,以下の 1 行をコピーし,R コンソールにペーストする
source("http://aoki2.si.gunma-u.ac.jp/R/src/friedman.R", encoding="euc-jp")
# フリードマン検定(plus 多重比較)
friedman <- function(dat) # データ行列
{
method <- "フリードマン検定(plus 多重比較)"
data.name <- deparse(substitute(dat))
dat <- subset(dat, complete.cases(dat)) # 欠損値を持つケースを除く
row <- nrow(dat) # ケース数
col <- ncol(dat) # 条件数
df <- col-1 # 自由度
o <- t(apply(dat, 1, rank)) # ケースごとに順位付けする
R <- colSums(o) # 条件ごとに順位の和をとる
# chi <- 12*sum(R^2)/(row*col*(col+1))-3*row*(col+1) # 検定統計量
tie <- sum(apply(o, 1, function(x) {y <- table(x); sum(y^3-y)}))
chi <- 12*sum((R-row*(col+1)/2)^2) / (row*col*(col+1)-tie/(col-1))
p <- pchisq(chi, df, lower.tail=FALSE) # P 値
result1 <- structure(list(statistic=c("chi squared"=chi), # 検定結果のまとめ
parameter=c(df=df), p.value=p, method=method,
data.name=data.name), class="htest")
R.m <- R/row # 条件ごとの平均順位
V <- sum((o-(col+1)/2)^2) # 分散
S <- combn(col, 2, function(ij) row^2*df*diff(R.m[ij])^2/(2*V)) # 対比較の検定統計量
p <- pchisq(S, df, lower.tail=FALSE) # P 値
result2 <- cbind("chi sa."=S, "p-value"=p)
rownames(result2) <- combn(col, 2, paste, collapse=":")
return(structure(list(result1=result1, result2=result2), class="friedman"))
}
# print メソッド
print.friedman <- function( obj, # friedman が返すオブジェクト
digits=4) # 結果の表示桁数
{
print(obj$result1, digits=digits) # 全体として差があるかの検定結果
cat("多重比較の結果\n\n")
print(obj$result2, digits=digits) # 多重比較の結果
}
使用例
> dat <- matrix(c( # 8行5列(5条件,8ケース)のデータ行列の例(ファイルから読んでも良い)
+ 5, 60, 35, 62, 76,
+ 24, 44, 74, 63, 76,
+ 56, 57, 70, 74, 79,
+ 44, 51, 55, 23, 84,
+ 8, 68, 50, 24, 64,
+ 32, 66, 45, 63, 46,
+ 25, 38, 70, 58, 77,
+ 48, 24, 40, 80, 72
+ ), byrow=TRUE, ncol=5)
> friedman(dat)
フリードマン検定(plus 多重比較)
data: dat
chi squared = 15.9, df = 4, p-value = 0.003156
多重比較の結果
chi sa. p-value
1:2 3.600 0.462837
1:3 4.225 0.376411
1:4 5.625 0.228958
1:5 15.625 0.003566
2:3 0.025 0.999923
2:4 0.225 0.994127
2:5 4.225 0.376411
3:4 0.100 0.998791
3:5 3.600 0.462837
4:5 2.500 0.644636
解説ページ
直前のページへ戻る
E-mail to Shigenobu AOKI