How do I calculate the weighted average of the rasters using overlay?

I am trying to learn how to use feature overlay from a bitmap package. When I ran into the function to understand it better, I ran into this problem. Why don't these two lines give the same result? What would be the correct way to use overlay to get the result of a non-overlay approach?

library(raster)

# Create rasters

s <- raster(nrow = 45, ncol = 90)
s[] <- 0

r <- stack( mget( rep( "s" , 12 )))
r[] <- runif(ncell(r)*12, 0, 500)

w <- stack( mget( rep( "s" , 12 )))
w[] <- runif(ncell(r)*12, 0, 1)

# Get weighted average 

# without overlay

raster::weighted.mean(r, w)

# with overlay

overlay(r, w, fun = raster::weighted.mean)

      

Thank!

+3


source to share





All Articles