density_unbounded {ggdist} | R Documentation |
Unbounded density estimator
Description
Unbounded density estimator using stats::density()
.
Supports automatic partial function application.
Usage
density_unbounded(
x,
weights = NULL,
n = 512,
bandwidth = "dpi",
adjust = 1,
kernel = "gaussian",
trim = FALSE,
adapt = 1,
na.rm = FALSE,
...,
range_only = FALSE
)
Arguments
x |
numeric vector containing a sample to compute a density estimate for. |
weights |
optional numeric vector of weights to apply to |
n |
numeric: the number of grid points to evaluate the density estimator at. |
bandwidth |
bandwidth of the density estimator. One of:
|
adjust |
numeric: the bandwidth for the density estimator is multiplied
by this value. See |
kernel |
string: the smoothing kernel to be used. This must partially
match one of |
trim |
Should the density estimate be trimmed to the bounds of the data? |
adapt |
(very experimental) The name and interpretation of this argument
are subject to change without notice. Positive integer. If |
na.rm |
Should missing ( |
... |
Additional arguments (ignored). |
range_only |
If |
Value
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 thex
input argument. -
call
: The call used to produce the result, as a quoted expression. -
data.name
: The deparsed name of thex
input argument. -
has.na
: AlwaysFALSE
(for compatibility). -
cdf
: Values of the (possibly weighted) empirical cumulative distribution function atx
. Seeweighted_ecdf()
.
This allows existing methods for density objects, 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.
See Also
Other density estimators:
density_bounded()
,
density_histogram()
Examples
library(distributional)
library(dplyr)
library(ggplot2)
# For compatibility with existing code, the return type of density_unbounded()
# is the same as stats::density(), ...
set.seed(123)
x = rbeta(5000, 1, 3)
d = density_unbounded(x)
d
# ... thus, while designed for use with the `density` argument of
# stat_slabinterval(), output from density_unbounded() 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()