[R] Unbalanced ANOVA in R?

John Fox jfox at mcmaster.ca
Fri Mar 8 14:05:06 CET 2002

Dear Fredrik,

At 06:56 AM 3/8/2002 +0100, Fredrik Karlsson wrote:
>Hi all
>I'm trying to complete a textbook example originally designed for SPSS
>in R, and I therefore need to find out how to compute an unbalanced
>ANOVA in R.
>I did a search on the mailinglist archives an found a post by Prof.
>Ripley saying one should use the lme function for (among other things)
>unbalanced ANOVAs, but I have not been able to use this object.
>My code gives me an error.. Why is that ?
> > aov(lme(DELAY ~ DOSE + TRIALS,data=epinuneq))
> > Error in getGroups.data.frame(dataMix, groups) :
> >       Invalid formula for groups
>Any ideas? How do I get an ANOVA computation that can handle uniqual
>sampe sizes in R?

The lme function is designed for linear mixed models, that is, models 
including both fixed and random effects. It's hard to tell from the 
information that you provide whether a mixed model is appropriate for your 
data -- in particular, is TRIALS a repeated-measures factor? If you indeed 
need to fit a mixed model, then you haven't specified it correctly. See the 
help page for lme for details.

On the other hand, if you just want to fit a fixed-effects Anova with no 
interactions, then you can use the aov or lm functions. Again, see 
help(aov) and help(lm). The summary method for aov and the anova method for 
both aov and lm objects will give you sequential ("type I") sums of 
squares. The Anova function in the car package can compute "Type II" and 
"III" sums of squares for lm objects. If you use the latter, be careful 
with the contrast coding.

I hope that this helps,

John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list