What you're fitting with aov
is called a strip plot, and it's tricky to fit with lme
because the subject:A
and subject:B
random effects are crossed.
Your first attempt is equivalent to aov(Y ~ A*B + Error(subject), data=d)
, which doesn't include all the random effects; your second attempt is the right idea, but the syntax for crossed random effects using lme is very tricky.
Using lme
from the nlme
package, the code would be
lme(Y ~ A*B, random=list(subject=pdBlocked(list(~1, pdIdent(~A-1), pdIdent(~B-1)))), data=d)
Using lmer
from the lme4
package, the code would be something like
lmer(Y ~ A*B + (1|subject) + (1|A:subject) + (1|B:subject), data=d)
These threads from R-help may be helpful (and to give credit, that's where I got the nlme
code from).
http://www.biostat.wustl.edu/archives/html/s-news/2005-01/msg00091.html
http://permalink.gmane.org/gmane.comp.lang.r.lme4.devel/3328
http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg10843.html
This last link refers to p.165 of Pinheiro/Bates; that may be helpful too.
EDIT: Also note that in the data set you have, some of variance components are negative, which is not allowed using random effects with lme, so the results differ. A data set with all positive variance components can be created using a seed of 8. The results then agree. See this answer for details.
Also note that lme
from nlme
does not compute the denominator degrees of freedom correctly, so the F-statistics agree but not the p-values, and lmer
from lme4
doesn't try too because it's very tricky in the presence of unbalanced crossed random effects, and may not even be a sensible thing to do. But that's more than I want to get into here.
This may be because your between-groups variable, NativeLanguage
, is unbalanced (12 English, 9 Other), in which case the type of Sums-of-Squares employed is going to affect the F values. By default, aov()
uses Type 1 sums of squares, which isn't recommended with unbalanced designs. Instead, use the ezANOVA()
function from the ez package:
my_anova = ezANOVA(
data = C1
, dv = .(RT)
, wid = .(Subject)
, within = .(Class)
, between = .(NativeLanguage)
, type = 3 #SPSS uses type 3 Sums-of-Squares
, observed = .(NativeLanguage) #ensures appropriate effect size is computed
)
#note warning about data imbalance
print(my_anova)
This yields the results table:
$ANOVA
Effect DFn DFd F p p<.05 ges
2 NativeLanguage 1 19 6.297322 0.021311506 * 0.2451177279
3 Class 1 19 1.976662 0.175885891 0.0009118545
4 NativeLanguage:Class 1 19 14.187926 0.001305329 * 0.0065510116
Which strangely still has a different report for the Class effect than what you're getting from SPSS/Statistica. Adding a detailed=TRUE
argument to the ezANOVA()
call above gives us a slightly more detailed results table (including the intercept and sums of squares):
$ANOVA
Effect DFn DFd SSn SSd F p p<.05 ges
1 (Intercept) 1 19 7717585.5514 245636.967 596.954632 8.136647e-16 * 0.9587389575
2 NativeLanguage 1 19 81413.4195 245636.967 6.297322 2.131151e-02 * 0.2451177279
3 Class 1 19 303.1399 2913.831 1.976662 1.758859e-01 0.0009118545
4 NativeLanguage:Class 1 19 2175.8535 2913.831 14.187926 1.305329e-03 * 0.0065510116
This shows that the mismatch lies in the SSn for the class effect; ezANOVA
(which uses car::Anova()
) obtains an SSn of 303ish whereas SPSS/Statistica obtain an SSn of 569.
Best Answer
Combined or separate ANOVAs
Fixed versus random