# [R] How to get Greenhouse-Geisser epsilons from anova?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Dec 8 19:11:17 CET 2008

```Skotara wrote:
> Thank you for your help!
> Sorry, for bothering you again..
> I still have trouble combining within and between subject factors.
> Interactions of within factors and D having only 2 levels work well.
>
> How can I get the main effect of D? I have tried anova(mlmfitD, mlmfit).
> With D having 3 levels I would expect the dfs to be 2 and 33. However,
> the output states 84,24??

That's not a main effect, it's a simultaneous test of all 28 components.
What you need is an analysis of the average of all
within-measurements. In principle that is obtained by X=~0, M=~1 or
T=matrix(1,1,28) but there's a bug that prevents it from working (fixed
in R-patched a couple of days ago).

>
> As long as the between factor has only 2 levels the between/within
> interactions fit well with SPSS, but if D has 3 levels, the mismatch is
> immense.
> If I calculate the within effects with myma having  not 12 subjects from
> one group but for example 24 from 2 groups, the output treats it as if
> all subjects came from the same group, for example for main effect A the
> dfs are 1 and 35. SPSS puts out 1 and 33 which is what I would have
> expected.. ..

Hmm, there's a generic problem in that you can't get some of the
traditional ANOVA table F tests by comparing two models, and in your
case, SPSS is de facto using the residuals from a model with A:D
interaction when testing for A. It might help if you try

anova(mlmfitD, X=~..., M=~...)

Look at the (Intercept) line.

>
>
> Peter Dalgaard schrieb:
>> Nils Skotara wrote:
>>> Thank you, this helped me a lot!
>>> All within effects and interactions work well!
>>>
>>> Sorry, but I still could not get how to include the between factor..
>>> If I include D with 2 levels, then myma is 24 by 28. (another 12 by
>>> 28 for the
>>> second group of subjects.)
>>> mlmfitD <- lm(myma~D) is no problem, but whatever I tried afterwards
>>> did not seem logical to me.
>>> I am afraid I do not understand how to include the between factor. I
>>> cannot include ~D into M or X because it has length 24 whereas the other
>>> factors have 28...
>>
>> Just do the same as before, but comparing mlmfitD to mlmfit:
>>
>> anova(mlmfitD, mlmfit, X=~A+B, M=~A+B+C)
>> # or anova(mlmfitD, mlmfit, X=~1, M=~C), as long as things are balanced
>>
>>
>> gives the D:C interaction test (by testing whether the C contrasts
>> depend on D). The four-factor interaction is
>>
>> anova(mlmfitD, mlmfit, X=~(A+B+C)^2, M=~A*B*C)
>>
>>
>>>
>>>
>>> Zitat von Peter Dalgaard <p.dalgaard at biostat.ku.dk>:
>>>
>>>> Skotara wrote:
>>>>> Dear Mr. Daalgard.
>>>>>
>>>>> thank you very much for your reply, it helped me to progress a bit.
>>>>>
>>>>> The following works fine:
>>>>> dd <- expand.grid(C = 1:7, B= c("r", "l"), A= c("c", "f"))
>>>>> myma <- as.matrix(myma) #myma is a 12 by 28 list
>>>>> mlmfit <- lm(myma~1)
>>>>> mlmfit0 <- update(mlmfit, ~0)
>>>>> anova(mlmfit, mlmfit0, X= ~C+B, M = ~A+C+B, idata = dd,
>>>>> test="Spherical"), which tests the main effect of A.
>>>>> anova(mlmfit, mlmfit0, X= ~A+C,  M = ~A+C+B, idata = dd,
>>>>> test="Spherical"), which tests the main effect of B.
>>>>>
>>>>>
>>>>> However, I can not figure out how this works for the other effects.
>>>>> If I try:
>>>>> anova(mlmfit, mlmfit0, X= ~A+B,  M = ~A+C+B, idata = dd,
>>>>> test="Spherical")
>>>>>
>>>>> I get:
>>>>> Fehler in function (object, ..., test = c("Pillai", "Wilks",
>>>>> "Hotelling-Lawley",  :
>>>>>        residuals have rank 1 < 4
>>>> dd\$C is not a factor with that construction. It works for me after
>>>>
>>>> dd\$C <- factor(dd\$C)
>>>>
>>>> (The other message is nasty, though. It's slightly different in
>>>> R-patched:
>>>>
>>>>  > anova(mlmfit, mlmfit0, X= ~A+B, M = ~A+C+B, idata = dd,
>>>> test="Spherical")
>>>> Error in solve.default(Psi, B) :
>>>>    system is computationally singular: reciprocal condition number =
>>>> 2.17955e-34
>>>>
>>>> but it shouldn't happen...
>>>> Looks like it is a failure of the internal Thin.row function. Ick!
>>>> )
>>>>
>>>>> I also don't know how I can calculate the various interactions..
>>>>> My read is I should change the second argument mlmfit0, too, but I
>>>>> can't
>>>>> figure out how...
>>>>
>>>> The "within" interactions should be straightforward, e.g.
>>>>
>>>> M=~A*B*C
>>>> X=~A*B*C-A:B:C
>>>>
>>>> etc.
>>>>
>>>> The within/between interactions are otained from the similar tests of
>>>> the between factor(s)
>>>>
>>>> e.g.
>>>>
>>>> mlmfitD <- lm(myma~D)
>>>>
>>>> and then
>>>>
>>>> anova(mlmfitD, mlmfit,....)
>>>>
>>>>
>>>>
>>
>>

--
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

```