KDE for spatial data. The algorithm is heavily inspired by Heatmap tool in QGIS. The help for QGIS tools is provided at the QGIS website. The a tutorial is provided here.

kde(
  points,
  band_width,
  decay = 1,
  kernel = c("quartic", "uniform", "triweight", "epanechnikov", "triangular"),
  scaled = FALSE,
  weights = c(),
  grid,
  cell_size,
  quiet = FALSE
)

Arguments

points

sf data.frame containing only POINTS.

band_width

numeric specifying the band width for KDE.

decay

numeric specifying the decay parameter for "triangular" kernel. For other kernels besides "triangular" the parameter is not used.

kernel

character specifying type of kernel to use. Available implemented kernels are "uniform", "quartic", "triweight", "epanechnikov", "triangular". Default is "quartic" and if unknown kernel name is used it falls back to the default value.

scaled

logical specifying if the output values should be scaled. Default value is FALSE.

weights

numeric vector of weights for individual points.

grid

either sf data.frame (outcome of function create_grid_rectangular or create_grid_hexagonal) or Raster-class (outcome of function create_raster). Does not have to be specified if cell_size is set.

cell_size

numeric specifying the distance for equal spaced points. Must be higher than 0. Can be left out if grid is provided as grid is used instead. The code used to generate grid is create_grid_rectangular(points, cell_size, band_width).

quiet

Should printing of progress bar be suppressed? Default `FALSE`.

Value

either sf

data.frame or Raster-class

depending on class of grid parameter.

Details

grid parameter specifies output of the function. KDE is calculated on the specified grid. If grid is Raster-class then outcome is also Raster-class. If grid is sf data.frame then outcome is also sf data.frame.

Examples

library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>% st_transform(32031)
#> Reading layer `nc' from data source 
#>   `/home/runner/work/_temp/Library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
grid <- create_grid_hexagonal(nc, cell_size = 100000)
points <- st_sample(nc, 500) %>% st_as_sf()
kde_estimate_grid <- kde(points, band_width = 150000, grid = grid)
#> Using centroids instead of provided `grid` geometries to calculate KDE estimates.
raster <- create_raster(nc, cell_size = 100000)
kde_estimate_raster <- kde(points, band_width = 150000, grid = raster)