View source: R/estimate_density.R
estimate_density | R Documentation |
This function is a wrapper over different methods of density estimation. By
default, it uses the base R density
with by default uses a different smoothing
bandwidth ("SJ"
) from the legacy default implemented the base R density
function ("nrd0"
). However, Deng and Wickham suggest that method = "KernSmooth"
is the fastest and the most accurate.
estimate_density(x, ...)
## S3 method for class 'data.frame'
estimate_density(
x,
method = "kernel",
precision = 2^10,
extend = FALSE,
extend_scale = 0.1,
bw = "SJ",
ci = NULL,
select = NULL,
by = NULL,
rvar_col = NULL,
...
)
## S3 method for class 'brmsfit'
estimate_density(
x,
method = "kernel",
precision = 2^10,
extend = FALSE,
extend_scale = 0.1,
bw = "SJ",
effects = "fixed",
component = "conditional",
parameters = NULL,
...
)
x |
Vector representing a posterior distribution, or a data frame of such
vectors. Can also be a Bayesian model. bayestestR supports a wide range
of models (see, for example, |
... |
Currently not used. |
method |
Density estimation method. Can be |
precision |
Number of points of density data. See the |
extend |
Extend the range of the x axis by a factor of |
extend_scale |
Ratio of range by which to extend the x axis. A value of |
bw |
See the eponymous argument in |
ci |
The confidence interval threshold. Only used when |
select |
Character vector of column names. If |
by |
Optional character vector. If not |
rvar_col |
A single character - the name of an |
effects |
Should results for fixed effects ( |
component |
Which type of parameters to return, such as parameters for the conditional model, the zero-inflated part of the model, the dispersion term, etc. See details in section Model Components. May be abbreviated. Note that the conditional component also refers to the count or mean component - names may differ, depending on the modeling package. There are three convenient shortcuts (not applicable to all model classes):
|
parameters |
Regular expression pattern that describes the parameters
that should be returned. Meta-parameters (like |
Possible values for the component
argument depend on the model class.
Following are valid options:
"all"
: returns all model components, applies to all models, but will only
have an effect for models with more than just the conditional model
component.
"conditional"
: only returns the conditional component, i.e. "fixed
effects" terms from the model. Will only have an effect for models with
more than just the conditional model component.
"smooth_terms"
: returns smooth terms, only applies to GAMs (or similar
models that may contain smooth terms).
"zero_inflated"
(or "zi"
): returns the zero-inflation component.
"location"
: returns location parameters such as conditional
,
zero_inflated
, or smooth_terms
(everything that are fixed or random
effects - depending on the effects
argument - but no auxiliary
parameters).
"distributional"
(or "auxiliary"
): components like sigma
,
dispersion
, beta
or precision
(and other auxiliary parameters) are
returned.
For models of class brmsfit
(package brms), even more options are
possible for the component
argument, which are not all documented in detail
here. See also ?insight::find_parameters
.
There is also a plot()
-method implemented in the see-package.
Deng, H., & Wickham, H. (2011). Density estimation in R. Electronic publication.
library(bayestestR)
set.seed(1)
x <- rnorm(250, mean = 1)
# Basic usage
density_kernel <- estimate_density(x) # default method is "kernel"
hist(x, prob = TRUE)
lines(density_kernel$x, density_kernel$y, col = "black", lwd = 2)
lines(density_kernel$x, density_kernel$CI_low, col = "gray", lty = 2)
lines(density_kernel$x, density_kernel$CI_high, col = "gray", lty = 2)
legend("topright",
legend = c("Estimate", "95% CI"),
col = c("black", "gray"), lwd = 2, lty = c(1, 2)
)
# Other Methods
density_logspline <- estimate_density(x, method = "logspline")
density_KernSmooth <- estimate_density(x, method = "KernSmooth")
density_mixture <- estimate_density(x, method = "mixture")
hist(x, prob = TRUE)
lines(density_kernel$x, density_kernel$y, col = "black", lwd = 2)
lines(density_logspline$x, density_logspline$y, col = "red", lwd = 2)
lines(density_KernSmooth$x, density_KernSmooth$y, col = "blue", lwd = 2)
lines(density_mixture$x, density_mixture$y, col = "green", lwd = 2)
# Extension
density_extended <- estimate_density(x, extend = TRUE)
density_default <- estimate_density(x, extend = FALSE)
hist(x, prob = TRUE)
lines(density_extended$x, density_extended$y, col = "red", lwd = 3)
lines(density_default$x, density_default$y, col = "black", lwd = 3)
# Multiple columns
head(estimate_density(iris))
head(estimate_density(iris, select = "Sepal.Width"))
# Grouped data
head(estimate_density(iris, by = "Species"))
head(estimate_density(iris$Petal.Width, by = iris$Species))
# rstanarm models
# -----------------------------------------------
library(rstanarm)
model <- suppressWarnings(
stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)
)
head(estimate_density(model))
library(emmeans)
head(estimate_density(emtrends(model, ~1, "wt", data = mtcars)))
# brms models
# -----------------------------------------------
library(brms)
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
estimate_density(model)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.