[R] help about 2 way anova and tukey test

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 24 15:48:07 CEST 2007


The form of your data is termed "wide" and you want to reshape it to
"long" form and use aov with that.  This uses the reshape command
to produce the long form.  Alternately you could use cast and melt
in the reshape package to do that:

# read data
Lines <- "subj    therapy t0      t1      t2
1       a       80.5    82.2    54.23
2       a       84.9    85.6    56.83
3       a       81.5    81.4    54.30
1       b       83.8    95.2    59.67
2       b       83.3    94.3    59.20
3       b       86      91.5    59.17
1       c       80.7    80.2    53.63
2       c       89.4    80.1    56.50
3       c       91.8    86.4    59.40
"
DF <- read.table(textConnection(Lines), header = TRUE)

# reshape to long form
nm <- names(DF)[3:5]
long <- reshape(DF, dir = "long", varying = list(nm), times = nm,
  v.names = "value")
long$time <- factor(long$time)

# calculate
aov(value ~ therapy * time, data = long)

# ...etc

On 5/24/07, Sarti Maurizio <sarti.m at irea.cnr.it> wrote:
> Dears members of R list,
> I have a technical question about conducting 2 way analysis of the variance
> (ANOVA) for repeated measures followed tukey test using R.
> my data are:
> There were 3 subj in all and 3 repeated measures for every time and therapy
> therapy = a,b,c
> time    = t1,t2,t3
> subj    = 1,2,3
>
> subj    therapy t0      t1      t2
> 1       a       80.5    82.2    54.23
> 2       a       84.9    85.6    56.83
> 3       a       81.5    81.4    54.30
> 1       b       83.8    95.2    59.67
> 2       b       83.3    94.3    59.20
> 3       b       86      91.5    59.17
> 1       c       80.7    80.2    53.63
> 2       c       89.4    80.1    56.50
> 3       c       91.8    86.4    59.40
>
> the code that I use is:
>
> rm(list=ls(all=TRUE))
> dati<- read.table("dati.txt", T)
> attach(dati)
> subj<- c( 1: 9, 1: 9,1:9)
> therapy<- factor( c( rep(" a", 3), rep(" b", 3), rep(" c", 3),
>                     rep(" a", 3), rep(" b", 3), rep(" c", 3),
>                     rep(" a",3), rep(" b", 3), rep(" c", 3)))
>
> time<- factor( c( rep(" t0", 9), rep(" t1", 9),rep(" t2", 9)))
> weight<- c( t0,t1,t2)
>
> time <- factor( time)
> therapy <- factor( therapy)
> subj <- factor( subj)
> summary( fm1<-aov( weight~time*therapy))
> fm1Tukey=TukeyHSD(fm1,"therapy",ordered = TRUE) ; fm1Tukey
> fm1Tukey=TukeyHSD(fm1,"time",ordered = TRUE) ; fm1Tukey
> fm1Tukey=TukeyHSD(fm1,"time:therapy",ordered = TRUE) ; fm1Tukey
>
> My question is - is that the correct way to do it??
> Very much obliged for your kind response
> Maurizio
>
> ******************************************************************************
> Maurizio Sarti, PhD
> IREA - CNR
> via Diocleziano,328 I-80124 Napoli (Italy)
> tel:+39-(0)81-5707999-(0)81-5704945  fax:+39-(0)81-5705734
> cell:+39-3204397891
> ******************************************************************************
> e-mail: sarti.m at irea.cnr.it website: http://www.irea.cnr.it
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list