pointKde2d: Estimates density at specified 2D points

View source: R/general.purpuse.utilities.R

pointKde2dR Documentation

Estimates density at specified 2D points

Description

similar to MASS::kde2d but calculates density for supplied points

Usage

pointKde2d(
  x,
  y,
  kernel = dnorm,
  approx = length(x) > 2000,
  random_seed = 123,
  k = min(length(x), 1000)
)

Arguments

x, y

data coordinates

kernel

kernel to be used (dnorm by default)

approx

logical, should density be estimated on subset of point. Approximate mode is much faster for large (length(x)>5000) datasets

random_seed

random seed to chose subset in approximate mode

k

subset size to use in approximate mode. 1000 is good starting point

Value

numeric vector with density estimates

Examples

x = rnorm(10000,mean = c(-300,300,300),sd=100)
y = rnorm(10000,x*c(-1,1),sd=100)
system.time(d1 <- pointKde2d(x,y,approx = F))
system.time(d2 <- pointKde2d(x,y,approx = T,k=100))
system.time(d3 <- pointKde2d(x,y,approx = T,k=1000))

par(mfrow=c(2,2))
plot(x,y,col=num2col(d1),pch=19,cex=0.2)
plot(x,y,col=num2col(d2),pch=19,cex=0.2)
plot(x,y,col=num2col(d3),pch=19,cex=0.2)

plot(d1,d3,pch=16,cex=0.2)
abline(a=0,b=1,col='red')

iaaka/visutils documentation built on Jan. 17, 2025, 11:29 p.m.