[R-sig-ME] Make a 'between-and-within-factors' ANOVA with lmer function

Ben Bolker bbolker at gmail.com
Sun Mar 24 23:02:16 CET 2013


ian m s white <i.m.s.white at ...> writes:

> 
> I reckon lmer can figure out for itself what is between and what is within
subjects, so
> 
> lmer(DV ~ IV1*IV2*IV3*IV4 + (1|Subject))
> 
> should fit the same model as your ANOVA.

  If you want to allow for variation in the IV3 and IV4 effects among
subjects you might want

lmer(DV ~ IV1*IV2*IV3*IV4 + (IV3*IV4|Subject))

  You might want to use lme rather than lme4 for the purposes of
getting calculated denominator df and p-values, which lmer won't
give you ...
 
> On 24 Mar 2013, at 08:56, Vanni Rovera <vanni.rovera at ...> wrote:
> 
> > Hi there,
> > 
> > I'm trying to understand how to use the function lmer in order to do a
> > 'between-and-within-factors' ANOVA, but without any success. I know about
> > the usage of the function aov, but this holds only for balanced designs;
> > its documentation say to use lme function (package nlme) for unbalanced
> > designs. Furthermore I found the lmer function (package lme4) is an
> > evolution of lme, so I wish to use this last function in order to perform
> > my ANOVA. But I'm not able to understand how to do this.
> > 
> > More precisely, imagine you have a dependent variable DV and four
> > independent variables IV1, IV2, IV3, IV4, where IV1, IV2 are
> > between-factors and IV3, IV4 are within-factors. Moreover you have a
> > variable called Subject in order to identify the subject on which
> > measurements are done (like for example this dataset:
> > http://personality-project.org/r/datasets/R.appendix5.data). If I use the
> > aov function, my 'between-and-within-factors' ANOVA would stand as follows:
> > 
> > aov(DV~(IV1*IV2*IV3*IV4)+Error(Subject/(IV3*IV4))).
> > 
> > Now can you write me the precise syntax in order to obtain the same result
> > with the lmer function?

  [snip]

> > 
> > *Additional details:* The problem is that no one seems to be interested in
> > explain the relations of 'within-factor' and 'between-factor' concepts with
> > those of 'fixed-effect' and 'random-effect'. Textbooks and papers about
> > ANOVA talk about between and within factors, while documentations and
> > papers about lmer function talk about mixed-effects models, i.e. they talk
> > about fixed and random effects, without mentioning between and within
> > factors. *Thus I am not able to understand the relations between the two,
> > since I think they are completely uncorrelated each others, and hence I am
> > not able to use the syntax in lmer in order to distinguish between factors
> > from within factors.*

   I would like to understand this better too.  I started to work
on an example but haven't finished.

lmer/ANOVA comparison
========================================================

```{r}
dat <-
read.table(url("http://personality-project.org/r/datasets/R.appendix5.data"),header=TRUE)
```

```{r}
library("ggplot2")
ggplot(dat,aes(x=Valence,y=Recall,colour=Dosage))+geom_point()+
  facet_grid(Gender~Task,labeller=label_both)+geom_line(aes(group=Subject))
```

```{r}
a1 <- aov(Recall~Task*Gender*Valence*Dosage+
             Error(Subject/(Task*Valence)),
    data=dat)
summary(a1)
```


```{r}
with(dat,table(Task,Valence,Dosage,Subject))
```{r}
library("nlme")
dat <- transform(dat,TaskValence=interaction(Task,Valence))
anova(lme(Recall~Task*Gender*Valence*Dosage,
       random=~1|Subject/TaskValence,
       data=dat))
```



More information about the R-sig-mixed-models mailing list