簡體   English   中英

ff:通過一個ffapply函數調用返回多個數組

[英]ff: returning multiple arrays with a single ffapply function call

我正在處理使用ff()加載到R中的3D成像數據的大型數據集。

require(ff)

nSubj <- 125
vol_dim <- c(139,137,87)
ff_qmap <- ff(0, dim=c(vol_dim,nSubj)

簡單的調用(例如獲取平均數組/“音量”)可以正常工作:

mean_qmap_vol <- ffapply(X=ff_qmap,MARGIN=c(1,2,3),AFUN=mean,RETURN=TRUE)

但是,在某些情況下,我想在單個ffapply調用中返回多個數組/“卷”。 例如,在進行一些基本的回歸分析(例如針對年齡)時:

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)

這適用於返回單個卷,即fe1_pval

有沒有一種方法可以在一次ffapply調用中同時返回fe1_estimatefe1_pval (也許還有更多估計)?

> 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 

我嘗試了多種解決方案,包括使用c()和列表返回組合向量。 但是,我找不到涉及有效的ffapply例程的解決方案。 我看過的一些關鍵參考資料在這里:

我找到了一種采用經典for循環方法並在3D數據集之間循環的權宜之計。 因為在這種情況下,數組的大小不會過大,所以可以使用。 我最終會喜歡使用ffapply()的解決方案,以便可以擴展為更高分辨率和更大的數據集。 並具有並行化的潛力。 歡迎提出建議!

事實證明, coef() stats函數是一種以標准方式提取所有模型系數的好方法。

testlist <- vector(mode="list", length=vol_dim[1]*vol_dim[2]*vol_dim[3])
i <- 1
for (x in 1:vol_dim[1]) {
  for (y in 1:vol_dim[2]) {
    for (z in 1:vol_dim[3]) {
      df$voxel <- ff_logjac[x,y,z,]
      fe1 <- lm(formula = voxel ~ age, df)
      testlist[[i]] <- coef(summary(fe1))
      i <- i + 1
    }
  }
}

這是在以下情況下訪問lm系數list()的方式:

> length(testlist)
[1] 1656741
> vol_dim[1]*vol_dim[2]*vol_dim[3]
[1] 1656741
> testlist[[1]]
                Estimate  Std. Error    t value  Pr(>|t|)
(Intercept)  0.061286603 0.168853045  0.3629582 0.7191810
age         -0.002272307 0.003510186 -0.6473466 0.5223308
> testlist[[1656741]]
                Estimate  Std. Error   t value   Pr(>|t|)
(Intercept) -0.444810783 0.192135240 -2.315092 0.02763245
age          0.007246639 0.003994186  1.814297 0.07964480
> testlist[[1]][1,1]
[1] 0.0612866

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM