<<戻る


繰り返し観測のない二元配置分散分析を実行する関数anova.RBを定義する

anova.RB <- function(dat){
        r = nrow(dat)
        c = ncol(dat)
        col.sum = apply(dat, 2, sum)
        row.sum = apply(dat, 1, sum)

        X = sum(dat)^2 / (r*c)
        AS = sum(dat^2) 
        R = sum(row.sum^2 / c)
        C = sum(col.sum^2 / r)

        SSr = R - X
        SSc = C - X
        SSres = AS - C - R + X
        SSt = AS - X
        res1 <- c(SSr, SSc, SSres, SSt)

        DFr = r - 1
        DFc = c - 1
        DFres = (r - 1) * (c - 1)
        DFt = r*c - 1
        res2 <- c(DFr, DFc, DFres, DFt)

        MSr = SSr / DFr
        MSc = SSc / DFc
        MSres = SSres / DFres
        MSt = NA
        res3 <- c(MSr, MSc, MSres, MSt)
        
        Fr = MSr / MSres
        Fc = MSc / MSres
        Fres = NA
        Ft = NA
        res4 <- c(Fr, Fc, Fres, Ft)

        Pr <- 1 - pf(Fr, DFr, DFres)
        Pc <- 1 - pf(Fc, DFc, DFres)
        Pres <- NA
        Pt <- NA
        res5 <- c(Pr, Pc, Pres, Pt)

        aovt <- cbind(res1, res2, res3, res4, res5)
        rownames(aovt) <- c("行間", "列間", "誤差", "総")
        colnames(aovt) <- c("SS", "DF", "MS", "F", "P")
        aovt
}



使用方法

> dat
     [,1] [,2] [,3] [,4]
[1,]    9    6   10    9
[2,]    7    5   13   11
[3,]    8    6    8   13
[4,]    8    3   13   14
[5,]   12    6   12   16
[6,]   11    7   14   12
[7,]    8   10   14   15
[8,]   13    9   16   14
> anova.RB(dat)
        SS DF        MS         F            P
行間  77.0  7 11.000000  3.253521 1.681791e-02
列間 217.5  3 72.500000 21.443662 1.347309e-06
誤差  71.0 21  3.380952        NA           NA
総   365.5 31        NA        NA           NA



参考文献

森・吉田『心理学のためのデータ解析テクニカルブック』北大路書房 p91-93