I am dealing with a large dataset of 3D imaging data that I have loaded in to R using ff().
require(ff)
nSubj <- 125
vol_dim <- c(139,137,87)
ff_qmap <- ff(0, dim=c(vol_dim,nSubj)
Simple calls like getting an average array/"volume" back work fine:
mean_qmap_vol <- ffapply(X=ff_qmap,MARGIN=c(1,2,3),AFUN=mean,RETURN=TRUE)
However, in some instances I would like to return more than one array/"volume" back in a single ffapply call; for instance, when performing some basic regression e.g. against age:
pval_vol <- ffapply( AFUN=f <- function(x) {
df$voxel <- x
fe1 <- lm(formula = voxel ~ age, df)
summary_fe1 <- summary(fe1)
fe1_estimate <- summary_fe1$coefficients[2,1]
fe1_pval <- summary_fe1$coefficients[2,4]
return(fe1_pval)
}, X = ff_qmap, MARGIN = c(1,2,3), RETURN = TRUE)
This works for returning a single volume back, i.e. fe1_pval.
Is there a way to return both the fe1_estimate and fe1_pval (and perhaps more estimates) in one ffapply call?
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
...
other attached packages:
[1] ff_2.2-13 bit_1.1-12 lme4_1.1-17 Matrix_1.2-8 ggplot2_2.2.1 fslr_2.12 neurobase_1.13.2
[8] oro.nifti_0.9.1
I tried a number of solutions including returning a combined vector with c() and alternatively a list. However, I could not find a solution involving the ffapply routine that would work. Some key references I looked at are here:
I found a stop-gap solution taking a classic for loop approach and cycling through the 3D dataset. Because the size of my array is not prohibitively large in this case, it works. I would ultimately prefer a solution using ffapply() so that it is extensible for higher resolution and larger datasets; and has the potential for parallelization. Open to suggestions!
The
coef()stats function turned out to be a great way of extracting all the model coefficients in a standard way.This is how the list() of lm coefficients is accessed after: