您的示例导致不相等的像元大小,这意味着不同的“平方和类型”很重要,并且对主要效果的测试并不像您陈述的那么简单。Anova()
使用II型平方和。请参阅此问题以开始。
有多种测试对比的方法。请注意,SS类型无关紧要,因为我们最终要在相关的一因素设计中进行测试。我建议使用以下步骤:
# turn your 2x2 design into the corresponding 4x1 design using interaction()
> d$ab <- interaction(d$a, d$b) # creates new factor coding the 2*2 conditions
> levels(d$ab) # this is the order of the 4 conditions
[1] "a1.b1" "a2.b1" "a1.b2" "a2.b2"
> aovRes <- aov(y ~ ab, data=d) # oneway ANOVA using aov() with new factor
# specify the contrasts you want to test as a matrix (see above for order of cells)
> cntrMat <- rbind("contr 01"=c(1, -1, 0, 0), # coefficients for testing a within b1
+ "contr 02"=c(0, 0, 1, -1), # coefficients for testing a within b2
+ "contr 03"=c(1, -1, -1, 1)) # coefficients for interaction
# test contrasts without adjusting alpha, two-sided hypotheses
> library(multcomp) # for glht()
> summary(glht(aovRes, linfct=mcp(ab=cntrMat), alternative="two.sided"),
+ test=adjusted("none"))
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: User-defined Contrasts
Fit: aov(formula = y ~ ab, data = d)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
contr 01 == 0 -0.7704 0.7875 -0.978 0.330
contr 02 == 0 -1.0463 0.9067 -1.154 0.251
contr 03 == 0 0.2759 1.2009 0.230 0.819
(Adjusted p values reported -- none method)
现在,手动检查第一对比的结果。
> P <- 2 # number of levels factor a
> Q <- 2 # number of levels factor b
> Njk <- table(d$ab) # cell sizes
> Mjk <- tapply(d$y, d$ab, mean) # cell means
> dfSSE <- sum(Njk) - P*Q # degrees of freedom error SS
> SSE <- sum((d$y - ave(d$y, d$ab, FUN=mean))^2) # error SS
> MSE <- SSE / dfSSE # mean error SS
> (psiHat <- sum(cntrMat[1, ] * Mjk)) # contrast estimate
[1] -0.7703638
> lenSq <- sum(cntrMat[1, ]^2 / Njk) # squared length of contrast
> (SE <- sqrt(lenSq*MSE)) # standard error
[1] 0.7874602
> (tStat <- psiHat / SE) # t-statistic
[1] -0.9782893
> (pVal <- 2 * (1-pt(abs(tStat), dfSSE))) # p-value
[1] 0.3303902