# [R] anova table

Douglas Bates bates at stat.wisc.edu
Wed Apr 4 14:22:00 CEST 2001

```Kaspar Pflugshaupt <pflugshaupt at geobot.umnw.ethz.ch> writes:

> On Wednesday 04 April 2001 07:20, George A. Dowding wrote:
> > I am taking a stats class and need to do a single factor anova table
> > with four treatments for an assignment.  The instructor showed the
> > class how to do it with minitab, and I am unsure how to proceede in
> > R.
> >
> > Here is the data:
> >
> > I.   .8 .2 1.4 .9 .9 1.0 1.0 1.1 1.4 1.2 1.0
> > II.  1.6 1.1 .2 1.3 1.5 .7 .5 1.3 1.3 .4
> > III  1.8 .3 1.1 2.0 2.8 1.5 1.3 2.9 1.7 .8
> > IV.  2.4 2.6 2.82.9 1.7 2.6 2.7 2.1 2.1 2.3
> >
> > I have been reading the various help files and can't quite finde the
> > right example to get started from.
>
> Try help(aov). Basically, you will have to  bring your data into a form like
>
> treatment	response
> 1		.8
> 1		.2
> 2		1.6
> 2		1.1
> ...		...
>
> and then use something like
>
> model <- aov(response~treatment)
> summary(model)
>
> This will give you a standard ANOVA table.

If the data are in the form of separate rows for each group, you could
read the data in as one column and use the gl function to generate the
treatment column.  If the data are stored as four columns in a table
then you could use read.table to read the data into a data frame and
the stack function to create one long response vector and the
corresponding treatment column.  (The name of the stack function is
taken from the similarly named command in Minitab.)

The simplest way of doing this is to bring in each level's data as a
separate vector and use stack.  That would look like

> grp1 <- c(.8, .2, 1.4, .9, .9, 1.0, 1.0, 1.1, 1.4, 1.2, 1.0)
> grp2 <- c(1.6, 1.1, .2, 1.3, 1.5, .7, .5, 1.3, 1.3, .4)
> grp3 <- c(1.8, .3, 1.1, 2.0, 2.8, 1.5, 1.3, 2.9, 1.7, .8)
> grp4 <- c(2.4, 2.6, 2.8, 2.9, 1.7, 2.6, 2.7, 2.1, 2.1, 2.3)
> dat <- stack(list(grp1, grp2, grp3, grp4))
Error in data.frame(values = unlist(unname(x)), ind = factor(rep(names(x),  :
arguments imply differing number of rows: 41, 0
> dat <- stack(list(I. = grp1, II. = grp2, III. = grp3, IV. = grp4))
> dat[1:10, ]
values ind
1     0.8  I.
2     0.2  I.
3     1.4  I.
4     0.9  I.
5     0.9  I.
6     1.0  I.
7     1.0  I.
8     1.1  I.
9     1.4  I.
10    1.2  I.
> aov(values ~ ind, data = dat)
Call:
aov(formula = values ~ ind, data = dat)

Terms:
ind Residuals
Sum of Squares  14.05771  10.57009
Deg. of Freedom        3        37

Residual standard error: 0.5344887
Estimated effects may be unbalanced

Notice that you can save the result of the aov function
> fm1 <- aov(values ~ ind, data = dat)
and do things like plot it to get diagnostic plots.
> plot(fm1)

If you need to do multiple comparisons of means, use the Tukey
function from the Devore5 package.  That package provides the data and
sample analyses from an introductory text for Engineering Statistics.

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

```