【问题标题】:Using "sample" within mclapply in R not working properly在 R 中的 mclapply 中使用“样本”无法正常工作
【发布时间】:2021-10-13 05:24:48
【问题描述】:

我正在尝试每次使用我的数据帧的不同子集运行函数的多次迭代。实际上该函数需要很长时间,所以我想使用mclapply 将迭代拆分到多个内核。对于每次迭代,我使用sample 随机选择数据帧的一个子集,这是我写给mclapply 的函数内部。但是,输出列表中每次迭代的结果都是相同的,这表明mclapply 不是每次都重新运行sample 行。这一定与我编写代码的方式有关,有什么想法我出错了吗?

这是一个快速运行的小型数据集的可重现示例。您会注意到d.val.all 输出列表中的 10 次迭代是相同的,这不是我想要的。

library(bipartite)
library(doBy)
library(parallel)

# create dummy data
ecto.matrix1=data.frame(replicate(10,sample(0:80,81,rep=TRUE)),Species.mix.90=c(sample(c("R","M","S","B"),81,rep=TRUE)))

# set up the function
funct.resample.d <- function(i) {
  RedSites <- row.names(ecto.matrix1)[ecto.matrix1$Species.mix.90=="R"]
  MountainSites <- row.names(ecto.matrix1)[ecto.matrix1$Species.mix.90=="M"]
  randomSilverSites <- sample(row.names(ecto.matrix1)[ecto.matrix1$Species.mix.90=="S"],8,replace=F)
  randomBlackSites <- sample(row.names(ecto.matrix1)[ecto.matrix1$Species.mix.90=="B"],8,replace=F)
  resampledSites <- c(RedSites,MountainSites,randomSilverSites,randomBlackSites) # make vector of the site names
  matrix=ecto.matrix1[resampledSites,] # select only those rows from the resampled row names
  matrix1 = matrix[,colSums(matrix[,-c(ncol(matrix))]) > 0] # drop cols that sum to 0
  matrix2=summaryBy(matrix1[,-c(ncol(matrix1))]~Species.mix.90,data=matrix1,FUN=sum)
  for (col in 1:ncol(matrix2)){
    colnames(matrix2)[col] <-  sub(".sum", "", colnames(matrix2)[col]) # remove the sum bit from the col names
  }
  row.names(matrix2)<-matrix2$Species.mix.90 # make row names
  matrix2=subset(matrix2, select=-c(Species.mix.90)) # drop host col
  d.val <- dfun(matrix2)$dprime
}

# run mclapply
reps=c(1:10)
d.val.all <- mclapply(reps, funct.resample.d, mc.cores = 10)

【问题讨论】:

    标签: r mclapply


    【解决方案1】:

    如果其他人遇到类似问题,我发现问题出在summaryBy 函数而不是sample。我用aggregate 替换了summaryBy,随机化效果很好。

    matrix2=aggregate(. ~ Species.mix.90, matrix1, sum)
    

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2021-04-08
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      相关资源
      最近更新 更多