# Bayes Factors (BF) for a Single Parameter

Source:`R/bayesfactor_parameters.R`

`bayesfactor_parameters.Rd`

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 further 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).

Note that the `logspline`

package is used for estimating densities and
probabilities, and must be installed for the function to work.
`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). Aliases of the main
functions are prefixed with `bf_*`

, like `bf_parameters()`

or
`bf_pointnull()`

.
**For more info, in particular on specifying correct priors for factors
with more than 2 levels, see
the Bayes factors vignette.**

## Usage

```
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 = 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 = TRUE,
...
)
# S3 method for numeric
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...
)
# S3 method for stanreg
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
effects = c("fixed", "random", "all"),
component = c("conditional", "location", "smooth_terms", "sigma", "zi",
"zero_inflated", "all"),
parameters = NULL,
...
)
# S3 method for brmsfit
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
effects = c("fixed", "random", "all"),
component = c("conditional", "location", "smooth_terms", "sigma", "zi",
"zero_inflated", "all"),
parameters = NULL,
...
)
# S3 method for blavaan
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...
)
# S3 method for data.frame
bayesfactor_parameters(
posterior,
prior = NULL,
direction = "two-sided",
null = 0,
verbose = TRUE,
...
)
```

## Arguments

- posterior
A numerical vector,

`stanreg`

/`brmsfit`

object,`emmGrid`

or a data frame - representing a posterior distribution(s) from (see 'Details').- prior
An object representing a prior distribution (see 'Details').

- direction
Test type (see 'Details'). One of

`0`

,`"two-sided"`

(default, two tailed),`-1`

,`"left"`

(left tailed) or`1`

,`"right"`

(right tailed).- null
Value of the null, either a scalar (for point-null) or a range (for a interval-null).

- verbose
Toggle off warnings.

- ...
Arguments passed to and from other methods. (Can be used to pass arguments to internal

`logspline::logspline()`

.)- 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__`

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.

## Value

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).

## Details

This method is used to compute Bayes factors based on prior and posterior distributions.

### One-sided & Dividing Tests (setting an order restriction)

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))`

.

## Note

There is also a
`plot()`

-method
implemented in the
see-package.

## Setting the correct `prior`

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*); As the priors for the alternative get wider, the
likelihood of the null value(s) increases, to the extreme that for completely
flat priors the null is infinitely more favorable than the alternative (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.

(Note that by default, `brms::brm()`

uses flat priors for fixed-effects;
See example below.)

It is important to provide the correct `prior`

for meaningful results.

When

`posterior`

is a numerical vector,`prior`

should also be a numerical vector.When

`posterior`

is a`data.frame`

,`prior`

should also be a`data.frame`

, with matching column order.When

`posterior`

is a`stanreg`

,`brmsfit`

or other supported Bayesian model:`prior`

can be set to`NULL`

, in which case prior samples are drawn internally.`prior`

can also be a model equivalent to`posterior`

but with samples from the priors*only*. See`unupdate()`

.**Note:**When`posterior`

is a`brmsfit_multiple`

model,`prior`

**must**be provided.

When

`posterior`

is an`emmGrid`

/`emm_list`

object:`prior`

should also be an`emmGrid`

/`emm_list`

object equivalent to`posterior`

but created with a model of priors samples*only*. See`unupdate()`

.`prior`

can also be the original (posterior)*model*. If so, the function will try to update the`emmGrid`

/`emm_list`

to use the`unupdate()`

d prior-model. (*This cannot be done for*)`brmsfit`

models.**Note**: When the`emmGrid`

has undergone any transformations (`"log"`

,`"response"`

, etc.), or`regrid`

ing, then`prior`

must be an`emmGrid`

object, as stated above.

## Interpreting Bayes Factors

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) (Wetzels et al. 2011).

## References

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. doi:10.1177/1745691611406923

## Examples

```
library(bayestestR)
if (require("logspline")) {
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)
}
#> [1] 1.212843
# \dontrun{
# rstanarm models
# ---------------
if (require("rstanarm") && require("emmeans") && require("logspline")) {
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)
bayesfactor_parameters(stan_model, null = rope_range(stan_model))
# emmGrid objects
# ---------------
group_diff <- pairs(emmeans(stan_model, ~group))
bayesfactor_parameters(group_diff, prior = stan_model)
# Or
group_diff_prior <- pairs(emmeans(unupdate(stan_model), ~group))
bayesfactor_parameters(group_diff, prior = group_diff_prior)
}
#> Loading required package: emmeans
#> Sampling priors, please wait...
#> Warning: Bayes factors might not be precise.
#> For precise Bayes factors, sampling at least 40,000 posterior samples is
#> recommended.
#> Sampling priors, please wait...
#> Sampling priors, please wait...
#> Warning: Bayes factors might not be precise.
#> For precise Bayes factors, sampling at least 40,000 posterior samples is
#> recommended.
#> Sampling priors, please wait...
#> Warning: Bayes factors might not be precise.
#> For precise Bayes factors, sampling at least 40,000 posterior samples is
#> recommended.
#> Bayes Factor (Savage-Dickey density ratio)
#>
#> Parameter | BF
#> ----------------------
#> group1 - group2 | 3.37
#>
#> * Evidence Against The Null: 0
# brms models
# -----------
if (require("brms")) {
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 <- brm(extra ~ group + (1 | ID),
data = sleep,
prior = my_custom_priors
)
bayesfactor_parameters(brms_model)
}
#> Compiling Stan program...
#> Start sampling
#>
#> SAMPLING FOR MODEL 'ce26679fcbf57e343c7cfa84fdd25937' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 2.2e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.22 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.140954 seconds (Warm-up)
#> Chain 1: 0.138031 seconds (Sampling)
#> Chain 1: 0.278985 seconds (Total)
#> Chain 1:
#>
#> SAMPLING FOR MODEL 'ce26679fcbf57e343c7cfa84fdd25937' NOW (CHAIN 2).
#> Chain 2:
#> Chain 2: Gradient evaluation took 1.5e-05 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.15 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.155487 seconds (Warm-up)
#> Chain 2: 0.138783 seconds (Sampling)
#> Chain 2: 0.29427 seconds (Total)
#> Chain 2:
#>
#> SAMPLING FOR MODEL 'ce26679fcbf57e343c7cfa84fdd25937' NOW (CHAIN 3).
#> Chain 3:
#> Chain 3: Gradient evaluation took 1.4e-05 seconds
#> Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.14 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.140099 seconds (Warm-up)
#> Chain 3: 0.153389 seconds (Sampling)
#> Chain 3: 0.293488 seconds (Total)
#> Chain 3:
#>
#> SAMPLING FOR MODEL 'ce26679fcbf57e343c7cfa84fdd25937' NOW (CHAIN 4).
#> Chain 4:
#> Chain 4: Gradient evaluation took 1.5e-05 seconds
#> Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.15 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.134156 seconds (Warm-up)
#> Chain 4: 0.117644 seconds (Sampling)
#> Chain 4: 0.2518 seconds (Total)
#> Chain 4:
#> Sampling priors, please wait...
#> Warning: Bayes factors might not be precise.
#> For precise Bayes factors, sampling at least 40,000 posterior samples is
#> recommended.
#> Bayes Factor (Savage-Dickey density ratio)
#>
#> Parameter | BF
#> ------------------
#> (Intercept) | 3.97
#> group1 | 8.81
#>
#> * Evidence Against The Null: 0
# }
```