[R] How to transform matrices to ANOVA input datasets?

Bojanowski, M.J. (Michal) M.J.Bojanowski at uu.nl
Wed Mar 14 20:11:36 CET 2007


Hi Josh,

Consider what follows to convert your data and estimate the models.
I am not sure however, what do you want to do after the models are
estimated, so my suggestions stop at this point.

HTH,
Michal


# -b-e-g-i-n---R---c-o-d-e-

# first i make some data that is similar to yours so i can practice on
it
m <- matrix(rnorm(7*6), ncol=6, nrow=7, 
	dimnames=list(1:7,  paste( rep(c("col", "trt"), each=3), 1:3,
sep="")) )
dataHP <- list(m, m, m)

# the first 7x6 matrix in the list
dataHP[[1]]

# function that converts a 7x6 matrix 'x' to a data frame just as you
want it
f <- function(x)
{
	data.frame( yield=as.numeric(x),
		block=rep( c(1:3, 1:3), each=7 ),
		treat=rep( c("col", "trt"), each=7*3 ),
		position=rep(1:7, 6) )
}

# the data frame
f(dataHP[[1]])

# process the list of matrices by applying 'f' to every component
# consequently, 'd' is a list of data frames
d <- lapply(dataHP, f)


# now, to calculate AOV for every data set you could again use 'lapply',
e.g.:
models <- lapply(d, function(x)  aov( yield ~ treat*position, data=x) )
# the result is a list of models
models[[1]]

# -e-n-d---R---c-o-d-e-



*** Note that my e-mail address has changed to m.j.bojanowski at uu.nl
*** Please update your address books accordingly. Thank you!

_________________________________________
Michal Bojanowski
ICS / Sociology
Utrecht University
Heidelberglaan 2; 3584 CS Utrecht
Room 1428
m.j.bojanowski at uu.nl
http://www.fss.uu.nl/soc/bojanowski
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of szhan at uoguelph.ca
Sent: Wednesday, March 14, 2007 7:12 PM
To: r-help at stat.math.ethz.ch
Subject: [R] How to transform matrices to ANOVA input datasets?

Hello, R experts,
I have a list called dataHP which has 30 elements (m1, m2, ..., m30).  
Each element is a 7x6 matrix holding yield data from two factors
experimental design, with treatment in column, position in row. For
instance, the element 20 is:
dataHP[[20]]
           col1     col2     col3      trt1     trt2    trt3
  [1,]     22.0     20.3     29.7      63.3     78.5    76.4
  [2,]    102.4     92.3     72.2     199.2    201.1    218.9
  [3,]     18.8     20.8     22.9     106.2    148.4    147.6
  [4,]     14.5     17.2     15.6     120.1    115.8    124.6
  [5,]     31.9     28.3     22.8     157.9    192.3    160.6
  [6,]     98.2    147.3    102.5     628.8    577.0    643.0
  [7,]    174.9    217.5   188.66     453.5    491.1    409.8

My goal is to find which element in the list has significant yield
difference among the position. So my first question is how to transform
the matrix to ANOVA input dataset which is:
yield	block	treat	position
22	1	col	1
102.4	1	col	2
18.8	1	col	3
14.5	1	col	4
31.9	1	col	5
98.2	1	col	6
174.9	1	col	7
20.3	2	col	1
92.3	2	col	2
20.8	2	col	3
17.2	2	col	4
28.3	2	col	5
147.3	2	col	6
217.5	2	col	7
29.7	3	col	1
72.2	3	col	2
22.9	3	col	3
15.6	3	col	4
22.8	3	col	5
102.5	3	col	6
188.66	3	col	7
63.3	1	trt	1
199.2	1	trt	2
106.2	1	trt	3
120.1	1	trt	4
157.9	1	trt	5
628.8	1	trt	6
453.5	1	trt	7
78.5	2	trt	1
201.1	2	trt	2
148.4	2	trt	3
115.8	2	trt	4
192.3	2	trt	5
577	2	trt	6
491.1	2	trt	7
76.4	3	trt	1
218.9	3	trt	2
147.6	3	trt	3
124.6	3	trt	4
160.6	3	trt	5
643	3	trt	6
409.8	3	trt	7
So I can contrasts(position) and do ANOVA like this:
fit1<-aov(yield~treat*position)
summary(fit1, split=list(position=1:10), expand.split= T) Finally I can
find the significant element in the list if there is any significant
contrast among the position. So my second question is how to apply this
ANOVA to each element in the list?
Your help will be highly appreciated!!

Josh

______________________________________________
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