简体   繁体   中英

Different Contrasts Results for Aov and lm in R

I'm having trouble getting my contrasts from aov() and lm() to match up in R. I'm pretty sure this is because I don't fully understand what's going on or how to specify the appropriate contrasts, but I thought I'd ask anyway.

R uses treatment contrasts by default for both lm() and aov(), which means that it contrasts each level of a factor against the baseline level. I can see this in the results of lm():

data(InsectSprays)

lmMod <- lm(count ~ spray, data=InsectSprays)
summary(lmMod)

Adding the intercept to each of the coefficients gives the same mean as calculated by tapply(). However, trying to reproduce these contrasts with aov() gives different results.

model1 <- aov(count ~ spray, data = InsectSprays)

summary(model1, split=list(spray=list("Cont1"=1, "Cont2"=2, 
        "Cont3" = 3,  "Cont4" = 4, "Cont5" = 5)))

Here, the last p-value is the same as the one for the contrast in lm (p = 0.181), but the aov() contrast suggests that spray B is different from spray A (p < 0.0001) whereas lm says that they are not different (p = 0.604).

I've tried recoding the contrasts myself using sum-to-zero effects:

c1 <- c(-1, 1, 0, 0, 0, 0)
c2 <- c(-1, 0, 1, 0, 0, 0)
c3 <- c(-1, 0, 0, 1, 0, 0)
c4 <- c(-1, 0, 0, 0, 1, 0)
c5 <- c(-1, 0, 0, 0, 0, 1)

contMat <- cbind(c1, c2, c3, c4, c5)
contrasts(InsectSprays$spray) <- contMat

model2 <- aov(count ~ spray, data = InsectSprays)

summary(model2, split=list(spray=list("Cont1"=1, "Cont2"=2, 
        "Cont3" = 3,  "Cont4" = 4, "Cont5" = 5)))

Now, the first contrast gives the same p-value as lm (p = 0.604), but the last contrast says that treatment F is significantly different from A (p < 0.0001), whereas lm says it is not (p = 0.181).

I feel like I'm missing something fundamental, but I haven't been able to figure it out. Any help would be appreciated.

aov is doing sequential sum of squares (Type I); lm is doing adjusted sum of squares (Type II).

To compare, use lm with the dummy variables manually computed.

d2 <- data.frame(cbind(count=InsectSprays$count, model.matrix(~spray, data=InsectSprays)))
m2 <- lm(count~0+.,d2)
anova(m2)
summary(m2)

The anova gives you the same results as your aov result, while the summary gives you the same results as your lm result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM