split_reduce-methods: Generic function to summarize subsets of an object by first... In bbuchsbaum/neuroim2: Data Structures and Handling for Neuroimaging Data

Description

Generic function to summarize subsets of an object by first splitting by row and then "reducing" by a summary function

Usage

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 split_reduce(x, fac, FUN) ## S4 method for signature 'matrix,integer,'function'' split_reduce(x, fac, FUN) ## S4 method for signature 'matrix,integer,missing' split_reduce(x, fac) ## S4 method for signature 'matrix,factor,missing' split_reduce(x, fac) ## S4 method for signature 'matrix,factor,'function'' split_reduce(x, fac, FUN) ## S4 method for signature 'NeuroVec,factor,'function'' split_reduce(x, fac, FUN) ## S4 method for signature 'NeuroVec,factor,missing' split_reduce(x, fac, FUN)

Arguments

 x a numeric matrix(like) object fac the factor to define subsets of the object FUN the function to apply to each subset. if FUN is missing, than the mean of each sub-matrix column is computed.

Details

if FUN is supplied it must take a vector and return a single scalar value. If it returns more than one value, an error will occur.

if x is a NeuroVec instance then voxels (dims 1:3) are treated as columns and time-series (dim 4) as rows. The summary function then is applied to groups of voxels. However, if the goal is to apply a function to groups of time-points, then this can be achieved as follows:

split_reduce(t(as.matrix(bvec)), fac)

Value

a new matrix where the original values have been "reduced" by the supplied function.

Examples

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mat = matrix(rnorm(100*100), 100, 100) fac = factor(sample(1:3, nrow(mat), replace=TRUE)) ## compute column means of each sub-matrix ms <- split_reduce(mat, fac) all.equal(row.names(ms), levels(fac)) ## compute column medians of each sub-matrix ms <- split_reduce(mat, fac, median) ## compute time-series means grouped over voxels. ## Here, \code{length(fac)} must equal the number of voxels: \code{prod(dim(bvec)[1:3]} bvec <- NeuroVec(array(rnorm(24*24*24*24), c(24,24,24,24)), NeuroSpace(c(24,24,24,24), c(1,1,1))) fac <- factor(sample(1:3, prod(dim(bvec)[1:3]), replace=TRUE)) ms <- split_reduce(bvec, fac) ms2 <- split_reduce(bvec, fac, mean) all.equal(row.names(ms), levels(fac)) all.equal(ms,ms2)

bbuchsbaum/neuroim2 documentation built on Oct. 15, 2019, 2:55 p.m.