knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
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) pdens(sdens, 2) qdens(sdens, 0.977) rdens(sdens, 10L)
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()
. 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()
.].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.warning()
and just count on user being thoughtful.].smooth_density()
will not necessarily respect the from
and to
ranges set when creating density()
^[Again are just relying on user to be intelligent when using.].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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.