# [R] Nested ANOVA with a random nested factor (how to use the lme function?)

Simon Blomberg blomsp at ozemail.com.au
Mon Jul 18 08:50:22 CEST 2005

```At 01:59 PM 18/07/2005, Addison, Prue wrote:
>Hi,
>
>I am having trouble using the lme function to perform a nested ANOVA
>with a random nested factor.
>
>My design is as follows:

>Location (n=6) (Random)
>
>Site nested within each Location (n=12) (2 Sites nested within each
>Location) (Random)
>
>Dependent variable: sp (species abundance)
>
>By using the aov function I can generate a nested ANOVA, however this
>assumes that my nested factor is fixed.
> > summary(aov(sp~Location/Site, data=mavric))
>
>Df Sum Sq Mean Sq F value Pr(>F)
>
>Location           4 112366   28092  1.2742 0.2962
>
>Location:Transect  5 121690   24338  1.1039 0.3736
>
>Residuals         40 881875   22047

Yes, this is equivalent to aov(sp ~ Location + Location:Site,...)

>I have tried the following lme function to specify that Site is random:
>
> > lme1 <- lme(sp~Location, random=~1|Site, data=mavric)

Here, Location is fixed, and Site is a grouping factor. There are fixed and
random components to the intercept.

> > lme2 <- lme(sp~Location, random=~1|Location/Site, data=mavric)

Here you have Location as a fixed effect, the intercept is random and the
grouping is Site %in % Location.

> > anova(lme1)
>
>             numDF denDF  F-value p-value
>
>(Intercept)     1    40 3.418077  0.0719
>
>Location        4     5 1.152505  0.4294

How can you have 6 sites, but only 4 df for Location (should be 5?)

>This gives me the correct F-value for Location from
>MSLocation/MSLocation:Transect, but the p-value doesn't seem to be
>correct (by my calculations in Microsoft Excel it should be 0.345)

I don't know your data, or your calculations. Microsoft Excel does not fill
me with confidence.

> > anova(lme2)
>
>Warning in pf(q, df1, df2, lower.tail, log.p) :
>
>          NaNs produced
>
>Warning: NAs introduced by coercion
>
>             numDF denDF  F-value p-value
>
>(Intercept)     1    40 0.459966  0.5015
>
>Location        4     0 0.155091     NaN
>
>? I don't know what this output means

Division by zero, somewhere? :-)

> > anova(lme1,lme2)
>
>      Model df      AIC      BIC    logLik   Test      L.Ratio p-value
>
>lme1     1  7 603.7534 616.4000 -294.8767
>
>lme2     2  8 605.7534 620.2067 -294.8767 1 vs 2 1.815674e-05  0.9966
>
>
>? I also don't know what this output means.

you are testing the difference between the models, using a likelihood ratio
test. The difference is not significant, so the conventional wisdom is to
choose the simpler model (lme1). Note that the AIC and BIC are lower
(better) for lme1, too.

>Can anyone tell me if there is a way to use the lme() function in order
>to obtain the same output as the aov() function (above), but so it
>correctly calculates the MS, F and p values for my main Location factor?

The following code shows agreement:

dat <- data.frame(Location=factor(rep(1:6, each=2)), Site=factor(rep(1:2,
12)), sp=rnorm(12))

fit <- aov(sp ~ Location + Error(Site), data=dat)
fit2 <- lme(sp ~ Location, random=~1|Site, data=dat)

summary(fit)
anova(fit2)

HTH,

Simon.

>Thanks,
>
>
>
>Prue
>
>This e-mail is solely for the named addressee and may be confidential.
>You should only read, disclose, transmit, copy, distribute, act in reliance
>on or commercialise the contents if you are authorised to do so.  If you
>are not the intended recipient of this e-mail, please notify
>postmaster at museum.vic.gov.au by e-mail immediately, or notify the sender
>and then destroy any copy of this message.  Views expressed in this e-mail
>are those of the individual sender, except where specifically stated to be
>those of an officer of Museum Victoria.  Museum Victoria does not represent,
>warrant or guarantee that the integrity of this communication has been
>maintained nor that it is free from errors, virus or interference.
>
>Museum Victoria
>+61 3 8341 7777
>11 Nicholson St
>Carlton
>Victoria
>www.museum.vic.gov.au
>
>
>         [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help

Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat.
Centre for Resource and Environmental Studies
The Australian National University
Canberra ACT 0200
Australia
T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au
F: +61 2 6125 0757
CRICOS Provider # 00120C

```