View source: R/bayesfactor_parameters.R
| bayesfactor_parameters | R Documentation |
This method computes Bayes factors against the null (either a point or an
interval), based on prior and posterior samples of a single parameter. This
Bayes factor indicates the degree by which the mass of the posterior
distribution has shifted away from or closer to the null value(s)
(relative to the prior distribution), thus indicating if the null value has
become less or more likely given the observed data.
When the null is an interval, the Bayes factor is computed by comparing the
prior and posterior odds of the parameter falling within or outside the null
interval (Morey & Rouder, 2011; Liao et al., 2020); When the null is a point,
a Savage-Dickey density ratio is computed, which is also an approximation of
a Bayes factor comparing the marginal likelihoods of the model against a
model in which the tested parameter has been restricted to the point null
(Wagenmakers et al., 2010; Heck, 2019).
bayesfactor_pointnull() and bayesfactor_rope() are wrappers around
bayesfactor_parameters() with different defaults for the null to be tested
against (a point and a range, respectively; see details). The bf_*
functions are aliases of the main functions.
For more info, see the Bayes factors vignette.
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
...,
verbose = TRUE
)
bayesfactor_pointnull(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
...,
verbose = TRUE
)
bayesfactor_rope(
posterior,
prior = NULL,
direction = "two-sided",
null = rope_range(posterior, verbose = FALSE),
...,
verbose = TRUE
)
bf_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
...,
verbose = TRUE
)
bf_pointnull(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
...,
verbose = TRUE
)
bf_rope(
posterior,
prior = NULL,
direction = "two-sided",
null = rope_range(posterior, verbose = FALSE),
...,
verbose = TRUE
)
## S3 method for class 'numeric'
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
...,
verbose = TRUE
)
## S3 method for class 'stanreg'
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
effects = "fixed",
component = "conditional",
parameters = NULL,
...,
verbose = TRUE
)
## S3 method for class 'data.frame'
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
rvar_col = NULL,
...,
verbose = TRUE
)
posterior |
A numerical vector, |
prior |
An object representing a prior distribution (see 'Details'). |
direction |
Test type (see 'Details'). One of |
null |
Value of the null, either a scalar (for point-null) or a range (for a interval-null). |
... |
Arguments passed to and from other methods. (Can be used to pass
arguments to internal |
verbose |
Toggle off warnings. |
effects |
Should variables for fixed effects ( For models of from packages brms or rstanarm there are additional options:
|
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 |
rvar_col |
A single character - the name of an |
This method is used to compute Bayes factors based on prior and posterior
distributions.
Note that the logspline package is used for estimating densities and
probabilities, and must be installed for the function to work.
One sided tests (controlled by direction) are conducted by restricting
the prior and posterior of the non-null values (the "alternative") to one
side of the null only (Morey & Wagenmakers, 2014). For example, if we
have a prior hypothesis that the parameter should be positive, the
alternative will be restricted to the region to the right of the null (point
or interval). For example, for a Bayes factor comparing the "null" of 0-0.1
to the alternative >0.1, we would set
bayesfactor_parameters(null = c(0, 0.1), direction = ">").
It is also possible to compute a Bayes factor for dividing
hypotheses - that is, for a null and alternative that are complementary,
opposing one-sided hypotheses (Morey & Wagenmakers, 2014). For
example, for a Bayes factor comparing the "null" of <0 to the alternative
>0, we would set bayesfactor_parameters(null = c(-Inf, 0)).
The resulting output is supported by the following methods:
as.numeric(): Extract the (possibly log-)Bayes factor values.
See bayesfactor_methods.
A data frame containing the (log) Bayes factor representing evidence
against the null (Use as.numeric() to extract the non-log Bayes
factors; see examples).
It is important to provide the correct prior for meaningful results,
to match the posterior-type input:
A numeric vector - prior should also be a numeric vector, representing the prior-distribution
A data frame - prior should also be a data frame, representing the prior-estimates, in matching column order.
If rvar_col is specified, prior should be the name of an rvar column that represents the prior-estimates.
Supported Bayesian model (stanreg, brmsfit, etc.)
prior should be a model an equivalent model with MCMC samples from the priors only. See unupdate().
If prior is set to NULL, unupdate() is called internally (not supported for brmsfit_multiple model).
Output from a {marginaleffects} function - prior should also be an equivalent output from a {marginaleffects} function based on a prior-model
(See unupdate()).
Output from an {emmeans} function
prior should also be an equivalent output from an {emmeans} function based on a prior-model (See unupdate()).
prior can also be the original (posterior) model, in which case the function
will try to "unupdate" the estimates (not supported if the estimates have undergone
any transformations – "log", "response", etc. – or any regriding).
In order to correctly and precisely estimate Bayes factors, a rule of thumb
are the 4 P's: Proper Priors and Plentiful
Posteriors.
For the computation of Bayes factors, the model priors must be proper priors
(at the very least they should be not flat, and it is preferable that they
be informative) (Note that by default, brms::brm() uses flat priors for
fixed-effects); Wide priors result in smaller marginal likelihoods, and thus
models with wider priors are trivially less likely than models with narrower
priors - where, at the extreme, that a model with completely flat priors is
infinitely less favorable than a point null model (this is called the
Jeffreys-Lindley-Bartlett paradox). Thus, you should only ever try (or want)
to compute a Bayes factor when you have an informed prior.
Additionally, for models using MCMC estimation the number of posterior
samples needed for testing is substantially larger than for estimation (the
default of 4000 samples may not be enough in many cases). A conservative rule
of thumb is to obtain 10 times more samples than would be required for
estimation (Gronau, Singmann, & Wagenmakers, 2017). If less than 40,000
samples are detected, a warning is issued.
A Bayes factor greater than 1 can be interpreted as evidence against the
null, at which one convention is that a Bayes factor greater than 3 can be
considered as "substantial" evidence against the null (and vice versa, a
Bayes factor smaller than 1/3 indicates substantial evidence in favor of the
null-model). See also effectsize::interpret_bf().
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.
Mattan S. Ben-Shachar
Wagenmakers, E. J., Lodewyckx, T., Kuriyal, H., and Grasman, R. (2010). Bayesian hypothesis testing for psychologists: A tutorial on the Savage-Dickey method. Cognitive psychology, 60(3), 158-189.
Heck, D. W. (2019). A caveat on the Savage–Dickey density ratio: The case of computing Bayes factors for regression parameters. British Journal of Mathematical and Statistical Psychology, 72(2), 316-333.
Morey, R. D., & Wagenmakers, E. J. (2014). Simple relation between Bayesian order-restricted and point-null hypothesis tests. Statistics & Probability Letters, 92, 121-124.
Morey, R. D., & Rouder, J. N. (2011). Bayes factor approaches for testing interval null hypotheses. Psychological methods, 16(4), 406.
Liao, J. G., Midya, V., & Berg, A. (2020). Connecting and contrasting the Bayes factor and a modified ROPE procedure for testing interval null hypotheses. The American Statistician, 1-19.
Wetzels, R., Matzke, D., Lee, M. D., Rouder, J. N., Iverson, G. J., and Wagenmakers, E.-J. (2011). Statistical Evidence in Experimental Psychology: An Empirical Comparison Using 855 t Tests. Perspectives on Psychological Science, 6(3), 291–298. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1177/1745691611406923")}
Other Bayes factors:
bayesfactor_inclusion(),
bayesfactor_models(),
bayesfactor_restricted()
library(bayestestR)
prior <- distribution_normal(1000, mean = 0, sd = 1)
posterior <- distribution_normal(1000, mean = .5, sd = .3)
(BF_pars <- bayesfactor_parameters(posterior, prior, verbose = FALSE))
as.numeric(BF_pars)
# rstanarm models
# ---------------
contrasts(sleep$group) <- contr.equalprior_pairs # see vingette
stan_model <- suppressWarnings(stan_lmer(
extra ~ group + (1 | ID),
data = sleep,
refresh = 0
))
bayesfactor_parameters(stan_model, verbose = FALSE)
bayesfactor_parameters(stan_model, null = rope_range(stan_model))
# emmGrid objects
# ---------------
group_diff <- pairs(emmeans(stan_model, ~group, data = sleep))
bayesfactor_parameters(group_diff, prior = stan_model, verbose = FALSE)
# Or
# group_diff_prior <- pairs(emmeans(unupdate(stan_model), ~group))
# bayesfactor_parameters(group_diff, prior = group_diff_prior, verbose = FALSE)
# brms models
# -----------
## Not run:
contrasts(sleep$group) <- contr.equalprior_pairs # see vingette
my_custom_priors <-
set_prior("student_t(3, 0, 1)", class = "b") +
set_prior("student_t(3, 0, 1)", class = "sd", group = "ID")
brms_model <- suppressWarnings(brm(extra ~ group + (1 | ID),
data = sleep,
prior = my_custom_priors,
refresh = 0
))
bayesfactor_parameters(brms_model, verbose = FALSE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.