Compute various point-estimates, such as the mean, the median or the MAP, to describe posterior distributions.
Usage
point_estimate(x, ...)
# S3 method for class 'numeric'
point_estimate(x, centrality = "all", dispersion = FALSE, threshold = 0.1, ...)
# S3 method for class 'data.frame'
point_estimate(
x,
centrality = "all",
dispersion = FALSE,
threshold = 0.1,
rvar_col = NULL,
...
)
# S3 method for class 'stanreg'
point_estimate(
x,
centrality = "all",
dispersion = FALSE,
effects = c("fixed", "random", "all"),
component = c("location", "all", "conditional", "smooth_terms", "sigma",
"distributional", "auxiliary"),
parameters = NULL,
...
)
# S3 method for class 'brmsfit'
point_estimate(
x,
centrality = "all",
dispersion = FALSE,
effects = c("fixed", "random", "all"),
component = c("conditional", "zi", "zero_inflated", "all"),
parameters = NULL,
...
)
# S3 method for class 'BFBayesFactor'
point_estimate(x, centrality = "all", dispersion = FALSE, ...)
# S3 method for class 'get_predicted'
point_estimate(
x,
centrality = "all",
dispersion = FALSE,
use_iterations = FALSE,
verbose = TRUE,
...
)
Arguments
- 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,
methods("hdi")
) and not all of those are documented in the 'Usage' section, because methods for other classes mostly resemble the arguments of the.numeric
or.data.frame
methods.- ...
Additional arguments to be passed to or from methods.
- centrality
The point-estimates (centrality indices) to compute. Character (vector) or list with one or more of these options:
"median"
,"mean"
,"MAP"
(seemap_estimate()
),"trimmed"
(which is justmean(x, trim = threshold)
),"mode"
or"all"
.- dispersion
Logical, if
TRUE
, computes indices of dispersion related to the estimate(s) (SD
andMAD
formean
andmedian
, respectively). Dispersion is not available for"MAP"
or"mode"
centrality indices.- threshold
For
centrality = "trimmed"
(i.e. trimmed mean), indicates the fraction (0 to 0.5) of observations to be trimmed from each end of the vector before the mean is computed.- rvar_col
A single character - the name of an
rvar
column in the data frame to be processed. See example inp_direction()
.- effects
Should results for fixed effects, random effects or both be returned? Only applies to mixed models. May be abbreviated.
- 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.
- parameters
Regular expression pattern that describes the parameters that should be returned. Meta-parameters (like
lp__
orprior_
) are filtered by default, so only parameters that typically appear in thesummary()
are returned. Useparameters
to select specific parameters for the output.- use_iterations
Logical, if
TRUE
andx
is aget_predicted
object, (returned byinsight::get_predicted()
), the function is applied to the iterations instead of the predictions. This only applies to models that return iterations for predicted values (e.g.,brmsfit
models).- verbose
Toggle off warnings.
Note
There is also a plot()
-method implemented in the see-package.
References
Makowski, D., Ben-Shachar, M. S., Chen, S. H. A., and Lüdecke, D. (2019). Indices of Effect Existence and Significance in the Bayesian Framework. Frontiers in Psychology 2019;10:2767. doi:10.3389/fpsyg.2019.02767
Examples
library(bayestestR)
point_estimate(rnorm(1000))
#> Point Estimate
#>
#> Median | Mean | MAP
#> ----------------------
#> 5.58e-03 | 0.01 | 0.03
point_estimate(rnorm(1000), centrality = "all", dispersion = TRUE)
#> Point Estimate
#>
#> Median | MAD | Mean | SD | MAP
#> ---------------------------------------
#> -0.02 | 0.97 | -5.33e-03 | 1.00 | 0.05
point_estimate(rnorm(1000), centrality = c("median", "MAP"))
#> Point Estimate
#>
#> Median | MAP
#> --------------
#> 0.03 | -0.07
df <- data.frame(replicate(4, rnorm(100)))
point_estimate(df, centrality = "all", dispersion = TRUE)
#> Point Estimate
#>
#> Parameter | Median | MAD | Mean | SD | MAP
#> ------------------------------------------------
#> X1 | -0.02 | 1.21 | 0.02 | 1.12 | 0.85
#> X2 | -0.07 | 1.07 | -0.10 | 1.00 | -0.18
#> X3 | -0.29 | 1.02 | -0.33 | 0.89 | -0.10
#> X4 | -0.08 | 0.90 | -0.14 | 0.89 | 0.11
point_estimate(df, centrality = c("median", "MAP"))
#> Point Estimate
#>
#> Parameter | Median | MAP
#> --------------------------
#> X1 | -0.02 | 0.85
#> X2 | -0.07 | -0.18
#> X3 | -0.29 | -0.10
#> X4 | -0.08 | 0.11
# \donttest{
# rstanarm models
# -----------------------------------------------
model <- rstanarm::stan_glm(mpg ~ wt + cyl, data = mtcars)
#>
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 2.1e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.21 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.048 seconds (Warm-up)
#> Chain 1: 0.044 seconds (Sampling)
#> Chain 1: 0.092 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 9e-06 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 0.049 seconds (Warm-up)
#> Chain 2: 0.043 seconds (Sampling)
#> Chain 2: 0.092 seconds (Total)
#> Chain 2:
#>
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 3).
#> Chain 3:
#> Chain 3: Gradient evaluation took 9e-06 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3:
#> Chain 3:
#> Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 3:
#> Chain 3: Elapsed Time: 0.048 seconds (Warm-up)
#> Chain 3: 0.053 seconds (Sampling)
#> Chain 3: 0.101 seconds (Total)
#> Chain 3:
#>
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 4).
#> Chain 4:
#> Chain 4: Gradient evaluation took 9e-06 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.09 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4:
#> Chain 4:
#> Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 4:
#> Chain 4: Elapsed Time: 0.05 seconds (Warm-up)
#> Chain 4: 0.045 seconds (Sampling)
#> Chain 4: 0.095 seconds (Total)
#> Chain 4:
point_estimate(model, centrality = "all", dispersion = TRUE)
#> Point Estimate
#>
#> Parameter | Median | MAD | Mean | SD | MAP
#> --------------------------------------------------
#> (Intercept) | 39.65 | 1.86 | 39.69 | 1.84 | 39.57
#> wt | -3.19 | 0.81 | -3.18 | 0.82 | -3.19
#> cyl | -1.51 | 0.44 | -1.52 | 0.44 | -1.54
point_estimate(model, centrality = c("median", "MAP"))
#> Point Estimate
#>
#> Parameter | Median | MAP
#> ----------------------------
#> (Intercept) | 39.65 | 39.57
#> wt | -3.19 | -3.19
#> cyl | -1.51 | -1.54
# emmeans estimates
# -----------------------------------------------
point_estimate(
emmeans::emtrends(model, ~1, "wt", data = mtcars),
centrality = c("median", "MAP")
)
#> Point Estimate
#>
#> X1 | Median | MAP
#> ------------------------
#> overall | -3.19 | -3.19
# brms models
# -----------------------------------------------
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
#> Compiling Stan program...
#> Start sampling
#>
#> SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 7e-06 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.07 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.023 seconds (Warm-up)
#> Chain 1: 0.021 seconds (Sampling)
#> Chain 1: 0.044 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 3e-06 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2:
#> Chain 2:
#> Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 2:
#> Chain 2: Elapsed Time: 0.022 seconds (Warm-up)
#> Chain 2: 0.019 seconds (Sampling)
#> Chain 2: 0.041 seconds (Total)
#> Chain 2:
#>
#> SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3).
#> Chain 3:
#> Chain 3: Gradient evaluation took 3e-06 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
#> Chain 3: Adjust your expectations accordingly!
#> Chain 3:
#> Chain 3:
#> Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 3:
#> Chain 3: Elapsed Time: 0.023 seconds (Warm-up)
#> Chain 3: 0.021 seconds (Sampling)
#> Chain 3: 0.044 seconds (Total)
#> Chain 3:
#>
#> SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4).
#> Chain 4:
#> Chain 4: Gradient evaluation took 3e-06 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
#> Chain 4: Adjust your expectations accordingly!
#> Chain 4:
#> Chain 4:
#> Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 4:
#> Chain 4: Elapsed Time: 0.024 seconds (Warm-up)
#> Chain 4: 0.021 seconds (Sampling)
#> Chain 4: 0.045 seconds (Total)
#> Chain 4:
point_estimate(model, centrality = "all", dispersion = TRUE)
#> Point Estimate
#>
#> Parameter | Median | MAD | Mean | SD | MAP
#> --------------------------------------------------
#> (Intercept) | 39.67 | 1.71 | 39.67 | 1.78 | 39.86
#> wt | -3.22 | 0.78 | -3.20 | 0.80 | -3.32
#> cyl | -1.49 | 0.44 | -1.50 | 0.43 | -1.46
point_estimate(model, centrality = c("median", "MAP"))
#> Point Estimate
#>
#> Parameter | Median | MAP
#> ----------------------------
#> (Intercept) | 39.67 | 39.86
#> wt | -3.22 | -3.32
#> cyl | -1.49 | -1.46
# BayesFactor objects
# -----------------------------------------------
bf <- BayesFactor::ttestBF(x = rnorm(100, 1, 1))
point_estimate(bf, centrality = "all", dispersion = TRUE)
#> Point Estimate
#>
#> Parameter | Median | MAD | Mean | SD | MAP
#> -----------------------------------------------
#> Difference | 1.03 | 0.11 | 1.03 | 0.11 | 1.02
point_estimate(bf, centrality = c("median", "MAP"))
#> Point Estimate
#>
#> Parameter | Median | MAP
#> --------------------------
#> Difference | 1.03 | 1.04
# }