Kernels for everyone!
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
During my dissertation, I spent a lot of time working on spatial kernel estimates. Where spatial kernel estimates are defined as a convolution of a spatial suppport ,
The kernelDensityEstimates
package provides a means to apply these estimates to raster
S4 objects from the NA
values, and uses OpenMP for acceleration.
However, all processing is done in memory, so there is no support for extremely large images.
The rasterKernelEstimates R package is currently not on CRAN, but will be after the documentation is cleaned up. Furthermore, the implementation of the kernels is currently non-optimal, and a fair bit of work can be done to speed them up a bit more. For those interested in helping out, the code is on my github page.
A short example of each function is provided below with some basic performance metrics. All timings are done on a Late 2013 MacBook Pro 13″ with a dual-core 2.4Ghz i5 running R 3.3.1 compiled under gcc 6.1.0.
rasterLocalSums
The function rasterLocalSums
calculates a weighted sum of pixel values in a spatial neighborhood defined by the matrix W
,
focal
when fun is not specified.
In fact, the results are identical when padding is used in the focal
call.
library(raster) library(rasterKernelEstimates) set.seed(100) n <- 500 # create a raster object r <- raster::raster( matrix(rnorm(n^2),n,n)) # create a weight matrix W <- raster::focalWeight(r,c(1,0.04),type='Gauss') # apply the weight with rasterKernelEstimates run.time <- proc.time() rLocalKDE1 <- rasterLocalSums(r,W) print(proc.time() - run.time) ## user system elapsed ## 0.975 0.004 0.325 # apply the weight with the raster packages focal run.time <- proc.time() rLocalKDE2 <- raster::focal(r,W,pad=TRUE) print(proc.time() - run.time) ## user system elapsed ## 0.667 0.008 0.678 # plot original image plot(r) # plot the smoothed image plot(rLocalKDE1) # print out the max abs difference print( sprintf( "The maximum absolute difference = %f.", max(abs(values(rLocalKDE1) - values(rLocalKDE2) ),na.rm = T)) ) ## [1] "The maximum absolute difference = 0.000000."