# Different ANCOVA results in R using different functions

before my actual ANCOVA I would like to test whether there is a significant interaction between the IV and the CV as this is one assumption for ANCOVA. I found 3 different ways in R to perform an ANCOVA. However, the result for one solution differs from the other two and I do not understand why. Here is my working code snippet:

library(lattice)

data <- data.frame(group = c(rep("CTRL", 10), rep("P", 10)),

response = c(10,11,14,16,17,17,19,20,21,22, 10,11,11,11,12,13,14,14,15,16),

age = c(40,41,45,43,50,51,55,57,60,62, 30,32,34,35,40,41,42,44,43,46))

xyplot(response ~ age, data=data, groups=group, type=c("p","r"))

# 1. ANCOVA
anova(lm(response ~ group + age + group : age, data = data))

# 2. ANCOVA
summary(aov(response ~ group + age + group : age, data = data))

# 3. ANCOVA
summary(lm(response ~ group + age + group : age, data = data))


I don't understand why the two p-values for group and age are identical for ANCOVA 1. and 2. but different for 3. even though the interaction p-value (group : age) is the same for all three. Doing the same thing in SPSS results in exactly the same p-values as ANCOVA 3.

Now, as you can imagine, I am pretty unsure what's right and what's wrong or what's actually the difference between them? Can anyone help?

-

## 1 Answer

I think that you would get a better idea what is going on if you tried the following:

data <- data.frame(group = rep( c("G1", "G2", "G3", "G4"), each= 5),
response = c(10,11,14,16,17,17,19,20,21,22, 10,11,11,11,12,13,14,14,15,16),
age = c(40,41,45,43,50,51,55,57,60,62, 30,32,34,35,40,41,42,44,43,46))


You will see that the summary( lm( ... )) does not really give you overall factor effect, but instead effect for each factor level:

Call:
lm(formula = response ~ group + age + group:age, data = data)

Residuals:
Min       1Q   Median       3Q      Max
-1.14331 -0.29665 -0.07703  0.17162  2.91720

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -14.7166     5.6759  -2.593 0.023536 *
groupG2       9.0976     8.8559   1.027 0.324542
groupG3      19.6954     7.3485   2.680 0.020030 *
groupG4       6.9328    12.8274   0.540 0.598763
age           0.6465     0.1292   5.005 0.000307 ***
groupG2:age  -0.2006     0.1756  -1.142 0.275744
groupG3:age  -0.4704     0.1874  -2.510 0.027409 *
groupG4:age  -0.1330     0.2958  -0.450 0.660993
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.024 on 12 degrees of freedom
Multiple R-squared: 0.9524, Adjusted R-squared: 0.9247
F-statistic: 34.31 on 7 and 12 DF,  p-value: 5.218e-07

-
ah cool I see the difference between the 3 ways of doing it now. So even for only 2 groups the p-values differ because summary(lm(..)) gives you the p-value for intercept (i.e. group1) and the non-reference group (i.e. group2) whereas for the two other approaches for the two groups you get an overall factor effect. is that the right conclusion? And for two groups only which method would you use to choose the p-value from?? – Peter_F Nov 9 '12 at 12:01
Nice explanation. This seems to be a common confusion when using R. – Peter Flom Nov 9 '12 at 12:04
Sorry, just realised that I am still confused. If you run the following 3 lines with my original data set: summary(lm(response ~ group + age, data = data)); anova(lm(response ~ group + age, data = data)); summary(aov(response ~ group + age, data = data)) Then the output for age in all 3 cases says that age has a significant influence (p=1.55e-09) on group. However, from model 1 one would conclude that there is no difference in the group when accounting for age as the p-value is 0.1005 whereas in the 2nd and 3rd model the p-value for group is highly significant? I am confused about that :) – Peter_F Nov 9 '12 at 12:06