# [R] ANOVA in R

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Sep 14 11:25:32 CEST 2006

```Andrew Robinson <A.Robinson at ms.unimelb.edu.au> writes:

> Try
>
> test <- data.frame(day.1=c(2,3,3,6,1),
>      day.4=c(7,2,4,6,3),
>      day.8=c(2,8,7,8,4))
>
> test
>
> test.long <- reshape(test, direction="long",
> 	  varying=c("day.1","day.4","day.8"),
> 	  v.names="response",
> 	  timevar="day",
> 	  times=names(test))
>
> test.long\$day <- factor(test.long\$day)
>
> test.long
>
> aov(response ~ day, data=test.long)

Was a one-way ANOVA intended? He never said.

On a more elementary level,

y <- with(test, c(day.1,day.4,day.8))
day <- factor(rep(c(1,4,8),each=5)) # or gl(3,5,labels=c(1,4,8))
sub <- factor(rep(1:5,3))           # or gl(5,1,15)

print(data.frame(y,day,sub)) # just to show the point

anova(lm(y~day))        # 1-way
anova(lm(y~day+sub))    # 2-way

# This could be better for unbalanced designs:

drop1(lm(y~day+sub),test="F")

>
> I hope that this helps,
>
> Andrew
>
>
> On Thu, Sep 14, 2006 at 09:23:13AM +0100, Russell Compton wrote:
> > Despite having used R on a daily basis for the past two years, I'm
> > encountering some difficulty performing an ANOVA on my data. What I'm trying
> > to do is the following:
> >
> >
> >
> > Given data such as:
> >
> >
> >
> > Day 1    Day 4    Day 8
> >
> > 2          7          2
> >
> > 3          2          8
> >
> > 3          4          7
> >
> > 6          6          8
> >
> > 1          3          4
> >
> >
> >
> > I want to use ANOVA to determine if there is a significant change over the
> > three days. In other stats packages I have used, I can just select this data
> > and run the ANOVA function and get the F and p values. However in R, the
> > anova function seems to only work with a fitted model, eg. Linear
> > regression. This function seems to assume there is a relationship such as
> > day1~ day 4 + day 8, but in my case there isn't - I just want to perform an
> > ANOVA without regression. If anyone could point me in the right direction
> > I'd greatly appreciate it,
> >
> >
> >
> > Thanks
> >
> >
> > 	[[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Andrew Robinson
> Department of Mathematics and Statistics            Tel: +61-3-8344-9763
> University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
> Email: a.robinson at ms.unimelb.edu.au         http://www.ms.unimelb.edu.au
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help