tidyst_kdde: Tidy and geospatial kernel density derivative estimates

tidyst_kddeR Documentation

Tidy and geospatial kernel density derivative estimates

Description

Tidy and geospatial versions of kernel density derivative estimates for 1- and 2-dimensional data.

Usage

tidy_kdde(data, deriv_order=1, ...)
st_kdde(x, deriv_order=1, ...)

Arguments

data

data frame/tibble of data values

deriv_order

derivative order. Default is 1.

x

sf object with point geometry

...

other parameters in ks::kdde function

Details

The output from *_kdde have the same structure as the kernel density estimate from *_kde, except that estimate is the kernel density derivative values at the grid points, and the additional derived grouping variable deriv_group is the index of the partial derivative, e.g. "deriv (1,0)" and "deriv (0,1)" for a first order derivative for 2-d data. The output is a grouped tibble, grouped by the input grouping variable (if it exists) and by deriv_group.

For details of the computation of the kernel density derivative estimate and the bandwidth selector procedure, see ?ks::kdde.

Value

–For tidy_kdde, the output is an object of class tidy_ks, which is a tibble with columns:

x

evaluation points in x-axis (name is taken from 1st input variable in data)

y

evaluation points in y-axis (2-d) (name is taken from 2nd input variable in data)

estimate

kernel density derivative estimate values

deriv_order

derivative order (same as input)

deriv_ind

index of partial derivative

ks

first row (within each group) contains the untidy kernel estimate from ks::kde

tks

short object class label derived from the ks object class

label

long object class label

group

grouping variable (if grouped input) (name is taken from grouping variable in data)

deriv_group

additional derived grouping variable on partial derivative indices.

–For st_kdde, the output is an object of class st_ks, which is a list with fields:

tidy_ks

tibble of simplified output (deriv_ind, ks, tks, label, group, deriv_group) from tidy_kdde

grid

sf object of grid of kernel density derivative estimate values, as polygons, with attributes estimate, deriv_ind, group, deriv_group copied from the tidy_ks object

sf

sf object of 1% to 99% contour regions of the kernel density derivative estimate, as multipolygons, with attributes contlabel derived from the contour level; and estimate, deriv_ind, group, deriv_group copied from the tidy_ks object.

Examples

library(ggplot2)
data(crabs, package="MASS")
## 1-d density curvature estimate
crabs1 <- dplyr::select(crabs, FL)
t1 <- tidy_kdde(crabs1, deriv_order=2)
gt1 <- ggplot(t1, aes(x=FL))
gt1 + geom_line(colour=1) + geom_rug_ks(colour=4)

## 2-d density gradient estimate
crabs2 <- dplyr::select(crabs, FL, CW)
t2 <- tidy_kdde(crabs2, deriv_order=1) 
gt2 <- ggplot(t2, aes(x=FL, y=CW)) + 
    scale_transparent(colorspace::scale_fill_discrete_diverging())
gt2 + geom_contour_ks(aes(group=deriv_group, colour=after_stat(level))) +
    colorspace::scale_colour_discrete_diverging() + facet_wrap(~deriv_group)
gt2 + geom_contour_filled_ks(colour=1) + facet_wrap(~deriv_group)
## second partial derivative f^(0,1) only
gt2 + geom_contour_filled_ks(data=dplyr::filter(t2, deriv_ind==2), colour=1)

## geospatial density derivative estimate
data(wa)
data(grevilleasf)
hakeoides <- dplyr::filter(grevilleasf, species=="hakeoides")
s1 <- st_kdde(hakeoides, deriv_order=1)
s1_cont <- st_get_contour(s1, which_deriv_ind=1)
s1_cont2 <- st_get_contour(s1, which_deriv_ind=2, cont=c(25,50,75, 97.5))
s1_cont3 <- st_get_contour(s1, breaks=contour_breaks(s1))

## base R filled contour plot
xlim <- c(1.2e5, 1.1e6); ylim <- c(6.1e6, 7.2e6)
plot(wa, xlim=xlim, ylim=ylim)
plot(s1, add=TRUE, which_deriv_ind=1)

## geom_sf filled contour plot
gs <- ggplot(s1) + geom_sf(data=wa, fill=NA) +
    colorspace::scale_fill_discrete_diverging() + ggthemes::theme_map()
gs + geom_sf(data=s1_cont, aes(fill=label_percent(contlabel)))  +
    coord_sf(xlim=xlim, ylim=ylim)
gs + geom_sf(data=s1_cont2, aes(fill=label_percent(contlabel))) +
    coord_sf(xlim=xlim, ylim=ylim)

## facet wrapped geom_sf filled contour plot
## each facet = each partial derivative 
gs + geom_sf(data=s1_cont3, aes(fill=contlabel)) +
    coord_sf(xlim=xlim, ylim=ylim) + facet_wrap(~deriv_group)

eks documentation built on May 29, 2024, 1:17 a.m.