Bounded density estimator using the reflection method. Supports automatic partial function application.
density_bounded(
x,
weights = NULL,
n = 512,
bandwidth = "nrd0",
adjust = 1,
kernel = "gaussian",
trim = TRUE,
bounds = c(NA, NA)
)
numeric vector containing a sample to compute a density estimate for.
optional numeric vector of weights to apply to x
.
numeric: the number of grid points to evaluate the density estimator at.
bandwidth of the density estimator. One of:
a numeric: the bandwidth, as the standard deviation of the kernel
a function: a function taking x
(the sample) and returning the bandwidth
a string: the suffix of the name of a function starting with "bw."
that
will be used to determine the bandwidth. See bw.nrd0()
for a list.
numeric: the bandwidth for the density estimator is multiplied
by this value. See stats::density()
.
string: the smoothing kernel to be used. This must partially
match one of "gaussian"
, "rectangular"
, "triangular"
, "epanechnikov"
,
"biweight"
, "cosine"
, or "optcosine"
. See stats::density()
.
ignored; the unbounded density estimator always uses trim = FALSE
internally before trimming to bounds
.
length-2 vector of min and max bounds. If a bound is NA
, then
that bound is replaced with min(x)
or max(x)
. Thus, the default,
c(NA, NA)
, means that the bounds used are range(x)
.
An object of class "density"
, mimicking the output format of
stats:density()
, with the following components:
x
: The grid of points at which the density was estimated.
y
: The estimated density values.
bw
: The bandwidth.
n
: The sample size of the x
input argument.
call
: The call used to produce the result, as a quoted expression.
data.name
: The deparsed name of the x
input argument.
has.na
: Always FALSE
(for compatibility).
This allows existing methods (like print()
and plot()
) to work if desired.
This output format (and in particular, the x
and y
components) is also
the format expected by the density
argument of the stat_slabinterval()
and the smooth_
family of functions.
Other density estimators:
density_auto()
,
density_unbounded()
library(distributional)
library(dplyr)
library(ggplot2)
# For compatibility with existing code, the return type of density_bounded()
# is the same as stats::density(), ...
set.seed(123)
x = rbeta(5000, 1, 3)
d = density_bounded(x)
d
#>
#> Call:
#> density_bounded(x = x)
#>
#> Data: x (5000 obs.); Bandwidth 'bw' = 0.03141
#>
#> x y
#> Min. :0.0000338 Min. :0.02507
#> 1st Qu.:0.2368066 1st Qu.:0.27277
#> Median :0.4735795 Median :0.86689
#> Mean :0.4735795 Mean :1.05688
#> 3rd Qu.:0.7103524 3rd Qu.:1.71379
#> Max. :0.9471253 Max. :2.86597
# ... thus, while designed for use with the `density` argument of
# stat_slabinterval(), output from density_bounded() can also be used with
# base::plot():
plot(d)
# here we'll use the same data as above, but pick either density_bounded()
# or density_unbounded() (which is equivalent to stats::density()). Notice
# how the bounded density (green) is biased near the boundary of the support,
# while the unbounded density is not.
data.frame(x) %>%
ggplot() +
stat_slab(
aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)),
alpha = 0.25
) +
stat_slab(aes(x), density = "bounded", fill = NA, color = "#d95f02", alpha = 0.5) +
stat_slab(aes(x), density = "unbounded", fill = NA, color = "#1b9e77", alpha = 0.5) +
scale_thickness_shared() +
theme_ggdist()
# We can also supply arguments to the density estimators by using their
# full function names instead of the string suffix; e.g. we can supply
# the exact bounds of c(0,1) rather than using the bounds of the data.
data.frame(x) %>%
ggplot() +
stat_slab(
aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)),
alpha = 0.25
) +
stat_slab(
aes(x), fill = NA, color = "#d95f02", alpha = 0.5,
density = density_bounded(bounds = c(0,1))
) +
scale_thickness_shared() +
theme_ggdist()