[R] recording and taking mean of a set of matrices

Adaikalavan RAMASAMY ramasamya at gis.a-star.edu.sg
Wed Sep 10 04:03:05 CEST 2003


" mean(list(m1, m2)) " will not work. 

mylist <- list(m1, m2)

sapply( mylist, FUN=mean ) gives will give you the mean of m1 and m2

sapply( mylist, FUN= function(x) apply(x, 2, mean) ) will give you the
column means of m1 and m2 in a matrix format. Double check the resulting
dimension.


Here are two ways to store results (say calculating quadratic of a
series) after each iteration : -

for(x in 1:100){

	# Option 1
	tmp[ ,x] <- x^2
	cat(x, "\t", tmp[ ,x], "\n", sep="", file="out.txt",
append=TRUE)

	# Option 2
	save(tmp, file="out.rda", compress=T)

Regards, Adai.




-----Original Message-----
From: Ross Boylan [mailto:ross at biostat.ucsf.edu] 
Sent: Wednesday, September 10, 2003 8:33 AM
To: R-help at stat.math.ethz.ch
Subject: [R] recording and taking mean of a set of matrices

I'm looking for a good form in which to store matrix results of a
simulation.

I am doing a simulation study.  Each simulation generates some data
and then analyzes it.  I want to record the results of many
simulations and analyze them.  Say r has the results of one
simulation, and I care about r$coefficients, a vector of coefficients,
and r$var, the estimated covariance matrix.

I'll do lots of simulations and then look at the results, computing
the mean of each value.

I'm looking for a good way to save and then analyze the results.  The
coefficients seem to fit well into a data frame, but I'm looking for a
good way to handle the matrix.

The only structure I've discovered that can even handle a set of
matrices is a list.  It also occurs to me the results could go to a 3
dimensional array; I suppose it would be good to make the last index
vary with the simulation.

Neither of these approaches seems ideal, because I would need to
handle the matrix separately from the other data I want to store.  I'm
hoping to do something like simresults <- rbind(simresults, r$coeff,
r$var).

The result also needs to be amenable to calculations.  If m1 and m2
are matrices (same dimension for each) mean(list(m1, m2)) doesn't
work, so even though list will record the data it isn't a great form
for analysis.  (But I suppose some apply variant would work with 3d
arrays). 

Any suggestions for good ways to approach this?  Again, the ideal
solution would have
* consistent handling of matrices and other data
* easy computation of (e.g.) means for the results.

Thanks.

P.S. I'm also aware I could accumulate means as I go, but I'm looking
for a more general solution.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list