5
$\begingroup$

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.
$\endgroup$
1

2 Answers 2

8
$\begingroup$

Order matters whenever the predictors aren't independent. They're correlated in your first example, as they're continuous measurements on each of the diamonds, but not in the second, as those are assigned in a balanced way.

ANOVA/ANCOVA/regression are all names for linear models; they do exactly the same thing mathematically. The name ANOVA is usually used when the predictors are categorical, and the name regression is usually used when the predictors are continuous. ANCOVA and "regression with different slopes" are usually used when there are both continuous and categorical predictors.

There is a way to force the interaction to be first using terms, but this almost never makes sense.

$\endgroup$
3
$\begingroup$

Stumbled on this question a couple of years after it was posted while looking for some info on ANCOVA with R. Wound up writing a much longer example using the diamonds dataset I won't try and cram it in here but for future searchers it is located here: https://ibecav.github.io/ancova_example/ .

I would answer your 3 original questions this way:

  1. Technically, your first example is neither an ANOVA or an ANCOVA. price, table and depth are all numeric variables. You're just regressing. ANOVA and ANCOVA always involve at least one predictor that is ordinal or nominal in nature.
  2. As noted by others you got different answers because by default aov uses type I sums of squares and with that type order matters.
  3. As others have noted you have to attend to the difference between * and + in your formula whether you are using lm or aov

As I said I don't want to try and reproduce my example in it's entirety but here is a simple example of price by cut and color

noCOVmodel <- aov(price ~ cut * color, diamonds2) car::Anova(noCOVmodel, type = 2)

versus price by cut and color with carat as a covariate

COVmodel <- aov(price ~ cut * color + carat, diamonds2) car::Anova(COVmodel, type = 2)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.