Velox的加权均值

时间:2018-08-04 19:54:37

标签: r raster velox

我想根据一组多边形上的栅格计算人口加权平均度量。出于性能方面的考虑,我更愿意使用velox,但是我不知道如何将权重合并到多边形平均中。以下是MWE,展示了使用raster的加权平均。

library(raster)
library(sf)

rm(list = ls())

## Make matrix
dim <- c(5, 5)
set.seed(0)
data.mat <- matrix(runif(prod(dim), 0, 100), dim[1], dim[2])
extent <- c(0,1,0,1)
res <- 1/dim
vx <- velox(data.mat, extent, res, crs="")
rast <- vx$as.RasterLayer() # Save rast for comparison to raster::extract() and plotting

## Create sf polygon
pol <-
  st_sfc(st_polygon(list(cbind(
    c(.1, .4, .7, .1), c(.1, .8, .1, .1)
  ))))

## Weighted extract using raster
pol_sp <- as(pol, "Spatial")
wts <- raster::extract(rast, pol_sp, weights = T, normalizeWeights = T, cellnumbers = T, df = T)
weighted.mean(wts$layer, wts$weight) # Weighted average
# [1] 60.43645

1 个答案:

答案 0 :(得分:0)

下面的代码将在运行上面的代码后从velox返回加权平均值。对于大型多边形,我认为这可能比raster::extract快许多倍。这个答案的灵感来自https://github.com/hunzikp/velox/issues/16

## Weighted extract using velox
vx_get_weights <- function(rast, poly, normalizeWeights = T) {
  rast$cell <- 1:ncell(rast)
  brk_100 <- disaggregate(rast, fact = 10) 
  brk_100_vx <- velox(brk_100) 
  vx_raw_dt <- setDT(brk_100_vx$extract(poly, fun = NULL, df = TRUE))
  setnames(vx_raw_dt, c("poly_id", "x", "cell"))

  weights <- vx_raw_dt[, .(w = .N / 100), by = .(poly_id, cell, x)]
  if (normalizeWeights) {
    weights[, w := w / sum(w), by = poly_id]
  }
  setorder(weights, poly_id, cell)
  weights
}
weights <- vx_get_weights(rast, pol, normalizeWeights = T)
weighted.mean(rast[weights$cell], weights$w)
# [1] 60.43645