Skip to content

Applying functions to partitions

2 messages · Titus von der Malsburg, Charles C. Berry

#
On Mon, Feb 16, 2009 at 01:45:52PM -0500, Stavros Macrakis wrote:
Ok, I should have been more specific.

The blocks are defined by factors.  There's a factor for the columns and
a factor for the rows.  In the example below the column factor would be
c(1,1,1,2,3,3) and the row factor c(1,1,1,2,2,3).  In the particular
case I'm working on the matrix is square and symmetric and there's only
one factor for both.

I can figure out ways to subset the matrix, similar to what Jorge
proposed, but I'm looking for a way to get the means more or less at
once because the matrix is pretty large and doing it block-wise is too
slow.

Thanks again!

 Titus
#
On Mon, 16 Feb 2009, Titus von der Malsburg wrote:

            
If the number of factor levels is manageable and if as.numeric( 
your.factor ) orders the blocks properly something like this

mat <- model.matrix( ~ 0 + your.factor )

tab <- table( your.factor )

means <- (t(mat) %*% your.block.matrix %*% mat) / outer( tab, tab )

Alternatively, use

means <-
    sapply(split.data.frame(your.block.matrix,your.factor),function(x)
 		sapply(split(colMeans(x),your.factor),mean))


HTH,

Chuck
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901