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.
stats::density()
on your data to get an estimate for a
density function.densdpr::smooth_density()
to smooth your density function and
create the needed distribution functions for ddens()
, pdens()
,
qdens()
and rdens()
.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
smooth_density()
uses a separate smoothing spline to build the
requisite functions for ddens()
, pdens()
and qdens()
. This is
inefficient and means it is possible for the different
distribution functions to be slightly inconsistent with one another.
What likely should be done is…ddens()
should serve as the foundation.pdens()
would be the integral of ddens()
.qdens()
would be the inverse of pdens()
.qdens()
should not accept values outside of 0 and 1, but it
currently does; pdens()
should not return values outside of 0 to
1, but it can.smooth_density()
will not necessarily
respect the from
and to
ranges set when creating density()
[4].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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.