# [R] ANCOVA

Matt Oliver oliver at imcs.marine.rutgers.edu
Fri Aug 27 23:10:11 CEST 2004

```Dear R-help list,

I am attempting to understand the proper formulation of ANCOVA's in R. I
would like to test both parallelism and intercept equality for some data
sets, so I have generated an artificial data set to ease my understanding.

This is what I have done

#Limits of random error added to vectors
min <- -0.1
max <- 0.1

x <- c(c(1:10), c(1:10))+runif(20, min, max)
x1 <- c(c(1:10), c(1:10))+runif(20, min, max)
y <- c(c(1:10), c(10:1))+runif(20, min, max)
z <- c(c(1:10), c(11:20))+runif(20, min, max)
g <- as.factor(c(rep(1, 10), rep(2, 10)))

#x and x1 have similar slopes and have the similar intercepts,
#x and y have different slopes and different intercepts
#x and z have similar slopes with different intercepts

#These are my full effects models

fitx1x <- lm(x1~g+x+x:g)
fityx <- lm(y~g+x+x:g)
fitzx <- lm(z~g+x+x:g)

anova(fitx1x)

Analysis of Variance Table

Response: x1
Df       Sum Sq      Mean Sq         F
value           Pr(>F)
g               1         0.002           0.002              0.3348
0.5709
x               1         163.927      163.927          23456.8319
<2e-16 ***
g:x            1          0.002          0.002               0.2671
0.6123
Residuals 16          0.112          0.007
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1

These results confirm that x and x1 do not have significantly different
means with respect to g;
There is a significant linear relationship between x and x1 independent of g
There is no evidence that the slopes of x and x1 in the two g groups is
different

anova(fityx)

Analysis of Variance Table

Response: y
Df       Sum Sq      Mean Sq         F
value      Pr(>F)
g                   1           0.012          0.012            1.7344
0.2064
x                   1           0.003          0.003            0.4399
0.5166
g:x                 1          164.947       164.947      24274.4246 <2e-16 ***
Residuals       16            0.109        0.007
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1

These results confirm that x and y do not have significantly different
means with respect to g;
There is a not a significant linear relationship between x and y
independent of g
There is evidence that the slopes of x and y in the two g groups is
significantly different.

anova(fitzx)

Analysis of Variance Table

Response: z
Df        Sum Sq      Mean Sq      F value      Pr(>F)
g                  1          501.07        501.07    52709.9073  <2e-16 ***
x                  1          165.39        165.39    17398.4057  <2e-16 ***
g:x                1            0.02          0.02         1.7472
0.2048
Residuals     16            0.15          0.01
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1

These results confirm that x and z  have significantly different means with
respect to g;
There is a a significant linear relationship between x and z independent of g
There is no evidence that the slopes of x and z in the two g groups is
significantly different.

What I don't understand is how to formulate the model so that I can tell if
the intercepts between the g groups are different.
Also, how would I formulate an ANCOVA if I am dealing with Model II
regressions?

Any help would be greatly appreciated.

Matt

==============================
When you reach an equilibrium in biology,