# [R] How do I compute interactions with anova.mlm ?

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue Sep 9 11:40:20 CEST 2008

```Schadwinkel, Stefan skrev:
> Hi,
>
> I wish to compute multivariate test statistics for a within-subjects repeated measures design with anova.mlm.
>
> This works great if I only have two factors, but I don't know how to compute interactions with more than two factors.
> I suspect, I have to create a new "grouping" factor and then test with this factor to get these interactions (as it is hinted in R News 2007/2),
> but I don't really know how to use this approach.
>
> Here is my current code:
>
> Two Factors: fac1, fac2
>
> mlmfit <- lm(mydata~1)
> mlmfit0 <- update(mlmfit, ~0)
>
> % test fac1, works, produces same output as SAS
> anova(mlmfit, mlmfit0, M = ~ fac1 + fac2, X = ~ fac2, idata = idata, test = "Wilks")
>
> % test fac1*fac2 interaction, also works, also the same output as SAS
> anova(mlmfit, mlmfit0, X = ~ fac1 + fac2, idata = idata, test = "Wilks")
>
>
>
> Three Factors: fac1, fac2, fac3
>
> mlmfit <- lm(mydata~1)
> mlmfit0 <- update(mlmfit, ~0)
>
> % test fac1, works, same as SAS
> anova(mlmfit, mlmfit0, M = ~ fac1 + fac2 + fac3, X = ~ fac2 + fac3, idata = idata, test = "Wilks")
>
>
>
> Now, I try to compute the interactions the same way, but this doesn't work:
>
> % fac1*fac2
> anova(mlmfit, mlmfit0, M = ~ fac1 + fac2 + fac3, X = ~ fac3, idata = idata, test = "Wilks")
>
> % fac1*fac2*fac3
> anova(mlmfit, mlmfit0, X = ~ fac1 + fac2 + fac3, idata = idata, test = "Wilks")
>
>
> Both of these above differ quite much from the SAS output and I suspect, my understanding of X and M is somewhat flawed.
>
> I would be very happy, if someone could tell me how to compute the two interactions above and an interaction of N factors in general.
>
>
You need to ensure that the difference between the X and M models is the
relevant interaction, so something like

M=~fac1*fac2*fac3
X=~fac1*fac2*fac3 - fac1:fac2:fac3

should test for fac1:fac2:fac3

If the within-subject design is fac1*fac2*fac3 with one observation per
cell (NB!), then you can omit M. X can also be written as
~fac1*fac2+fac2*fac3+fac1*fac3 or ~(fac1+fac2+fac3)^2.

For the next step, use, e.g.,

M=~fac1*fac2+fac2*fac3+fac1*fac3
X=~fac2*fac3+fac1*fac3

to test significance of fac1:fac2 (notice that the main effects are
still in X becaus of the meaning of the "*" operator in R).

> I would also be interested in computing linear contrasts using the T matrix and anova.mlm.
>
> Thank you very much,
>
> Stefan
>
>
>
> --
> Neurologische Klinik
> Sektion Biomagnetismus
> Universität Heidelberg
> Im Neuenheimer Feld 400
> 69120 Heidelberg
>
> Telefon:  06221 - 56 5196
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907

```