0

搜索了一下,还没有找到解决这个问题的优雅方法。

我正在尝试使用“降雪”包在多个内核上运行模拟。这使用 sfApply()、sfSapply 或 sfLapply()。为了克服使用“降雪”的复杂性,现在让我们简化一下,只考虑并行的 apply()、sapply() 和 lapply() 函数。

我有一个生态模拟,其结果以矩阵形式返回。简而言之,我正在模拟不同大小和强度的干扰事件对分布在多个斑块上的个体的影响。例如,我的模拟输出类似于

DisturbancePatchesPercent<-c(.1,.3,.5,.7,.9) #the size (number of patches affected) by a disturbance
PopSize<-matrix(NA,nrow=length(DisturbancePatchesPercent),ncol=5) #results I want outputted
attributes(PopSize)$dimnames<-list(c(),c("Mean","Lower95","Upper95","Disturbance Patches","Disturbance Intensity"))
PopSize[,1]<-rpois(5,6) #making up output for the mean population size
PopSize[,2]<-rpois(5,1) #making up an output for the lower 95% CI
PopSize[,3]<-rpois(5,12) #making up an output for the upper 95% CI
PopSize[,4]<-DisturbancePatchesPercent #Disturbance size
PopSize[,5]<-rep(.3,5) #A uniform disturbance intensity for a given simulation
PopSize

我有一个平均值,以及模拟运行的总体规模的 95% 置信区间的下限和上限。每次运行都有长度(DisturbancePatchesPercent)受干扰事件影响的补丁数量。对于单次运行,干扰具有均匀的强度。

我正在尝试使用 apply 函数在多种不同的干扰强度下运行此模拟。所以我把我的模拟放在一个函数中,然后将我想要模拟的干扰强度的向量传递给该函数。问题是,我不知道如何让函数返回每个干扰强度的结果矩阵(如上模拟)。我希望我能找到一种方法来制作将要创建的多个矩阵的数组,但未能弄清楚这一点

有任何想法吗??

下面是一个可重现的示例,我已经玩弄了它以弄清楚一般概念。我非常接近这个模拟示例,但我能做的最好的事情就是创建一个不同示例矩阵的列表。但是列表中的四个元素都具有相同的名称,所以我不知道如何单独访问它们

func<-function(x){
mat<-matrix(1:20,4,5) #fake results matrix
mat.x<-mat*x #multiplying results matrix by given value of x (simulated disturbance intensity)

return(list(mat.x=mat.x))
                            }
x<-c(1,10,25,66) #simulated vector of disturbance intensities
results<-sapply(x,func) #using apply function to pass the vector to the function
results #the results, but all the elements of the list are named identically so I can't call them individually!
4

0 回答 0