Learn R Programming

bayestestR (version 0.2.0)

map_estimate: Maximum A Posteriori (MAP) Estimate

Description

Find the Highest Maximum A Posteriori (MAP) estimate of a posterior, i.e., the most probable value. It corresponds to the "peak" (or the mode) of the posterior distribution. Note this function relies on estimate_density, which by default uses a different smoothing bandwidth ("SJ") from the legacy default implemented the base R density function ("nrd0").

Usage

map_estimate(x, ...)

# S3 method for numeric map_estimate(x, precision = 2^10, ...)

# S3 method for stanreg map_estimate(x, precision = 2^10, effects = c("fixed", "random", "all"), parameters = NULL, ...)

# S3 method for brmsfit map_estimate(x, precision = 2^10, effects = c("fixed", "random", "all"), component = c("conditional", "zi", "zero_inflated", "all"), parameters = NULL, ...)

Arguments

x

Vector representing a posterior distribution. Can also be a stanreg or brmsfit model.

...

Currently not used.

precision

Number of points of density data. See the n parameter in density.

effects

Should results for fixed effects, random effects or both be returned? Only applies to mixed models. May be abbreviated.

parameters

Regular expression pattern that describes the parameters that should be returned. Meta-parameters (like lp__ or prior_) are filtered by default, so only parameters that typically appear in the summary() are returned. Use parameters to select specific parameters for the output.

component

Should results for all parameters, parameters for the conditional model or the zero-inflated part of the model be returned? May be abbreviated. Only applies to brms-models.

Value

A numeric value if posterior is a vector. If density = TRUE, or if posterior is a model-object, returns a data frame with following columns:

  • Parameter The model parameter(s), if x is a model-object. If x is a vector, this column is missing.

  • MAP The MAP estimate for the posterior or each model parameter.

Examples

Run this code
# NOT RUN {
library(bayestestR)

posterior <- rnorm(10000)
map_estimate(posterior)

plot(density(posterior))
abline(v = map_estimate(posterior), col = "red")
# }
# NOT RUN {
library(rstanarm)
model <- rstanarm::stan_glm(mpg ~ wt + cyl, data = mtcars)
map_estimate(model)

library(brms)
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
map_estimate(model)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab