我仍然不确定这是一个好主意,我认为对对比度的含义感到困惑的可能性太高了。不过,我要做的是创建一个新的 contrasts 函数,它计算总对比但将名称设置为等于处理对比中的默认名称。
set.seed(5)
n <- 5
y <- c(10 + rnorm(n, 0, 1), 20 + rnorm(n, 0, 1), 30 + rnorm(n, 0, 1))
wFactor <- as.factor(c(rep("A", n), rep("B", n), rep("C", n)))
contr.sumX <- function(...) {
conT <- contr.treatment(...)
conS <- contr.sum(...)
colnames(conS) <- colnames(conT)
conS
}
作为参考,这是通常的输出:
> m1 <- lm(y ~ wFactor, contrasts = list(wFactor=contr.sum(n = levels(wFactor))))
> coef(summary(m1))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 19.8218432 0.2481727 79.8711599 9.889455e-18
wFactor1 -9.6079241 0.3509692 -27.3754029 3.480430e-12
wFactor2 -0.1934654 0.3509692 -0.5512319 5.915907e-01
这是contr.sumX
函数的输出。
> m2 <- lm(y ~ wFactor, contrasts = list(wFactor=contr.sumX(n = levels(wFactor))))
> coef(summary(m2))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 19.8218432 0.2481727 79.8711599 9.889455e-18
wFactorB -9.6079241 0.3509692 -27.3754029 3.480430e-12
wFactorC -0.1934654 0.3509692 -0.5512319 5.915907e-01
或者,您可以提前为特定因素设置对比度:
contrasts(wFactor) <- "contr.sumX"
m3 <- lm(y ~ wFactor)
> coef(summary(m3))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 19.8218432 0.2481727 79.8711599 9.889455e-18
wFactorB -9.6079241 0.3509692 -27.3754029 3.480430e-12
wFactorC -0.1934654 0.3509692 -0.5512319 5.915907e-01