0

Been searching around a bit, and haven't found an elegant solution to this issue yet.

I am trying to use the 'snowfall' package to run a simulation on multiple cores. This uses sfApply(), sfSapply, or sfLapply(). To get past the complications of using 'snowfall,' lets simplify for now and just consider the parallel apply(), sapply(), and lapply() functions.

I have a ecological simulation, the results of which are returned as a matrix. To be brief, I am simulating the effect of a disturbance event of different sizes and intensities on individuals spread over multiple patches. For example, the output of my simulation is something like

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

Where I have a mean, as well as lower and upper limits of a 95% confidence interval for the population size from a simulation run. Each run has length(DisturbancePatchesPercent) number of patches affected by the disturbance event. For a single run, the disturbance is of a uniform intensity.

I am trying to use the apply function to run this simulation under multiple different disturbance intensities. So I have put my simulation in a function, then pass that function a vector of the disturbance intensities I want to simulate under. The problem is, I can't figure out how to get the function to return me the results matrix (as simulated above) for each of the disturbance intensities. I was hoping I could figure out a way to make an array of the multiple matrixes that would be created, but have failed at figuring that out

Any ideas??

A reproducible example, which I have toyed with to figure out the general concept, is below. I got very close with this mock example, but the best I could do was to create a list of the different example matrixes. But the four elements in the list all have the same name, so I don't know how to access them individually

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!
user1399311
  • 271
  • 2
  • 12

0 Answers0