[R-sig-Geo] Reproducible results with overlay and clusterR

Hugo Costa hugo@gco@t@ @end|ng |rom gm@||@com
Mon May 27 22:59:49 CEST 2019


Dear all
I need to implement a raster::overlay operation with clusterR (which
works), but don't figure out how to do it reproducible. How to set seeds in
the cluster? Please find a example below.
Thank you
Hugo

## Rasters
library(raster)
r1 <- r2 <- raster()
r1[]<-runif(ncell(r1), 1, 100)
r2[]<-1
r2[1:30000]<-2
r2[30001:35000]<-NA
plot(stack(r1,r2))


## Function
prob<-data.frame(value=1:100, r1=log(1:100), r2=max(log(1:100))-log(1:100))
f <- function(x,y,...){
  ifelse(is.na(y),NA,rbinom(n=1, size=1, prob=prob[[y]][x]))
}
f <- Vectorize(f)

## Overlay (reproducible)
set.seed(0)
result1<-overlay(stack(r1,r2), fun=f)
set.seed(0)
result2<-overlay(stack(r1,r2), fun=f)
compareRaster(result1,result2,values=TRUE)

## Cluster overlay (Not reproducible)
set.seed((0))
beginCluster(2)
result3<-clusterR(stack(r1,r2),overlay,args=list(fun=f), export="prob")
endCluster()

set.seed((0))
beginCluster(2)
result4<-clusterR(stack(r1,r2),overlay,args=list(fun=f), export="prob")
endCluster()

compareRaster(result3,result4,values=TRUE)

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list