README.md

densdpqr

Lifecycle:
experimental

The goal of densdpqr is to make a minimal package for applying the[dpqr]*() distribution functions (e.g. dnorm(), pnorm(), qnorm(), rnorm()), for any arbitrary output from the stats::density() function.

Example

  1. Call stats::density() on your data to get an estimate for a density function.
  2. Call densdpr::smooth_density() to smooth your density function and create the needed distribution functions for ddens(), pdens(), qdens() and rdens().
  3. Pass output from smooth_density() and other required parameter into [dpqr]dens() function(s).
# devtools::install_github("brshallo/densdpqr")
library(densdpqr)

set.seed(123)
data <- rnorm(10000)
dens <- density(data)
sdens <- smooth_density(dens)

ddens(sdens, 2)
#> [1] 0.05513055
pdens(sdens, 2)
#> [1] 0.9770511
qdens(sdens, 0.977)
#> [1] 1.999054
rdens(sdens, 10L)
#>  [1]  2.3939920 -0.5273786 -0.1777766 -1.0160578  0.9251596 -0.8309481
#>  [7] -0.5785291  0.1021182  0.2127245 -0.1567640

Problems

Inspiration & Other Resources

Thank you to spatstat authors as well as William Huber whose comment on How to find probability density function from density function… inspired the approach in densdpqr.

In most simple univariate cases could just use the logspline::[dpqr]logspline() functions which are set-up the same way as densdpqr::[dpqr]dens() but without the same Problems. The advantages with densdpqr are it allows the use of the base stats::density() function, it has fewer dependencies, and is likely faster.

[1] When I tried to pass lower = -Inf into integrate() I got errors that the function was divergent… so would need to change the smoothing method in some way to prevent this. I also wasn’t sure how to efficiently return the inverse of pdens().

[2] Current method may be biased towards smoothing density rather than the inverse, the quantiles.

[3] In the interim, maybe should at least at a warning() and just count on user being thoughtful.

[4] Again are just relying on user to be intelligent when using.



brshallo/densdpqr documentation built on Dec. 19, 2021, 11:49 a.m.