Following this and other sources of information on how to perform ANOVA and ANCOVA in R, I got very confused on the difference between the two on how to compute this difference. Please consider the following two examples
ANCOVA
require(ggplot2)
> anova(lm(price~table+depth, data = diamonds))
Response: price
Df Sum Sq Mean Sq F value Pr(>F)
depth 1 9.7323e+07 9.7323e+07 6.2202 0.01263 *
table 1 1.4462e+10 1.4462e+10 924.2957 < 2e-16 ***
Residuals 53937 8.4391e+11 1.5646e+07
and
> anova(lm(price~depth+table, data = diamonds))
Response: price
Df Sum Sq Mean Sq F value Pr(>F)
table 1 1.3876e+10 1.3876e+10 886.825 < 2.2e-16 ***
depth 1 6.8360e+08 6.8360e+08 43.691 3.882e-11 ***
Residuals 53937 8.4391e+11 1.5646e+07
The sum of squares, pvalues and other values all changed depending on the order. This lead me to think that I just performed an ANCOVA.
ANOVA
The example comes form here
delivery.df = data.frame(
Service = c(rep("Carrier 1", 15), rep("Carrier 2", 15),
rep("Carrier 3", 15)),
Destination = c(rep(c("Office 1", "Office 2", "Office 3",
"Office 4", "Office 5"), 9)),
Time = c(15.23, 14.32, 14.77, 15.12, 14.05,
15.48, 14.13, 14.46, 15.62, 14.23, 15.19, 14.67, 14.48, 15.34, 14.22,
16.66, 16.27, 16.35, 16.93, 15.05, 16.98, 16.43, 15.95, 16.73, 15.62,
16.53, 16.26, 15.69, 16.97, 15.37, 17.12, 16.65, 15.73, 17.77, 15.52,
16.15, 16.86, 15.18, 17.96, 15.26, 16.36, 16.44, 14.82, 17.62, 15.04)
)
> anova(lm(Time ~ Service*Destination, data = delivery.df))
Response: Time
Df Sum Sq Mean Sq F value Pr(>F)
Service 2 23.1706 11.5853 161.5599 < 2.2e-16 ***
Destination 4 17.5415 4.3854 61.1553 5.408e-14 ***
Service:Destination 8 4.1888 0.5236 7.3018 2.360e-05 ***
Residuals 30 2.1513 0.0717
and
> anova(lm(Time ~Destination*Service, data = delivery.df))
Response: Time
Df Sum Sq Mean Sq F value Pr(>F)
Destination 4 17.5415 4.3854 61.1553 5.408e-14 ***
Service 2 23.1706 11.5853 161.5599 < 2.2e-16 ***
Destination:Service 8 4.1888 0.5236 7.3018 2.360e-05 ***
Residuals 30 2.1513 0.0717
Here the values do not depend on the order suggesting that I did an ANOVA
Questions
- Am I right to think that I first did an ANCOVA and then an ANOVA?
- Where did the code differ to cause one analysis to be an ANOVA and the other an ANCOVA?
- Because, the order matters in ANCOVA, I would have thought I would be able to compute the interaction before the main effect if I desire. I tried
anova(lm(Time ~ Destination:Service+Destination+Service, data = delivery.df))
but the interaction remains at the end.