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

Ross Boylan ross at biostat.ucsf.edu
Wed Sep 10 02:33:06 CEST 2003

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

```