1要因の分散分析 対応がない場合(CRデザイン)T [データ数が異なるケース]
プログラム
anova.CR2 <- function(dat){ p <- ncol(dat) Aj <- c() nj <- c() for(i in 1:p){ Aj[i] <- sum(dat[,i][!is.na(dat[,i])]) nj[i] <- length(dat[,i][!is.na(dat[,i])]) } bar.Aj <- apply(dat, 2, mean) G = sum(dat[!is.na(dat)]) N = sum(nj) X = G^2 / N AS = sum(dat[!is.na(dat)]^2) A = sum(Aj^2 / nj) SSa = A - X SSwc = AS - A SSt = AS - X DFa = p - 1 DFwc = N - p DFt = N - 1 MSa = SSa / DFa MSwc = SSwc / DFwc Fa = MSa / MSwc Pa = 1 - pf(Fa, DFa, DFwc) result <- matrix(c( SSa, DFa, MSa, Fa, Pa, SSwc, DFwc, MSwc, NA, NA, SSt, DFt, NA, NA, NA), ncol=5, byrow=T) rownames(result) <- c("主効果", "誤差", "総") colnames(result) <- c("SS", "DF", "MS", "F-value", "p-value") result }
使用法
データ形式は参考文献と同じにしてください。NAは欠損値を表しています。
> dat [,1] [,2] [,3] [,4] [1,] 9 5 13 11 [2,] 7 6 8 13 [3,] 8 3 13 14 [4,] 8 6 12 16 [5,] 12 7 14 12 [6,] 11 10 16 NA [7,] 8 NA 10 NA [8,] 13 NA NA NA > anova.CR2(dat) [1] 76 37 86 66 [1] 8 6 7 5 [1] 2700.962 [1] 2995 [1] 2877.938 SS DF MS F-value p-value 主効果 176.9766 3 58.992186 11.08668 0.0001227255 誤差 117.0619 22 5.320996 NA NA 総 294.0385 25 NA NA NA
参考文献
森・吉田『心理学のためのデータ解析テクニカルブック』北大路書房 p90-91