Title: | The Bayes Factor Playground |
---|---|
Description: | A lightweight modelling syntax for defining likelihoods and priors and for computing Bayes factors for simple one parameter models. It includes functionality for computing and plotting priors, likelihoods, and model predictions. Additional functionality is included for computing and plotting posteriors. |
Authors: | Lincoln John Colling [aut, cre] |
Maintainer: | Lincoln John Colling <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.9.4 |
Built: | 2024-11-12 05:12:20 UTC |
Source: | https://github.com/bayesplay/bayesplay |
Perform a robustness analysis by systematically varying the values of the alternative prior
bfrr( likelihood, alternative_prior, null_prior, parameters, steps = 100L, cutoff = 3L, multicore = TRUE )
bfrr( likelihood, alternative_prior, null_prior, parameters, steps = 100L, cutoff = 3L, multicore = TRUE )
likelihood |
|
alternative_prior |
|
null_prior |
|
parameters |
|
steps |
|
cutoff |
Minimum Bayes factor value for evidence for the hypothesis (Default: 3) |
multicore |
Run robustness analysis across multiple cores (Default: TRUE if available) |
A robustness
object
# define a likelihood data_model <- likelihood(family = "normal", mean = 1, sd = 5) # define the alternative prior alternative_prior <- prior(family = "normal", mean = 5, sd = 4) # define the null prior null_prior <- prior(family = "point", point = 0L) # set the parameters to vary and the range to vary them across # vary the mean from -10 to 5 # vary the sd from 1 to 5 parameters <- list(mean = c(-10, 5), sd = c(1, 5)) # mark all Bayes factors larger/smaller than 3/.3 as evidence for the # alternative / null cutoff <- 3 bfrr(data_model, alternative_prior, null_prior, parameters, steps = 10, cutoff, multicore = FALSE )
# define a likelihood data_model <- likelihood(family = "normal", mean = 1, sd = 5) # define the alternative prior alternative_prior <- prior(family = "normal", mean = 5, sd = 4) # define the null prior null_prior <- prior(family = "point", point = 0L) # set the parameters to vary and the range to vary them across # vary the mean from -10 to 5 # vary the sd from 1 to 5 parameters <- list(mean = c(-10, 5), sd = c(1, 5)) # mark all Bayes factors larger/smaller than 3/.3 as evidence for the # alternative / null cutoff <- 3 bfrr(data_model, alternative_prior, null_prior, parameters, steps = 10, cutoff, multicore = FALSE )
Extract the posterior
object from a product
object
extract_posterior(x)
extract_posterior(x)
x |
a |
a posterior
object
Extract the marginal predictions over the prior
extract_predictions(x)
extract_predictions(x)
x |
a |
a prediction
object
Computes the definite integral of a product
object over the range
of the parameter
integral(obj)
integral(obj)
obj |
a |
A numeric of the marginal likelihood
# define a likelihood data_model <- likelihood(family = "normal", mean = 5.5, sd = 32.35) # define a prior prior_model <- prior(family = "normal", mean = 5.5, sd = 13.3) # multiply the likelihood by the prior model <- data_model * prior_model # take the integral integral(model)
# define a likelihood data_model <- likelihood(family = "normal", mean = 5.5, sd = 32.35) # define a prior prior_model <- prior(family = "normal", mean = 5.5, sd = 13.3) # multiply the likelihood by the prior model <- data_model * prior_model # take the integral integral(model)
Define likelihoods using different different distribution families
likelihood(family, ...)
likelihood(family, ...)
family |
the likelihood distribution (see details) |
... |
see details |
The following distribution families can be used for the likelihood
normal
a normal distribution
student_t
a scaled and shifted t-distribution
noncentral_t
a noncentral t (for t statistic)
noncentral_d
a noncentral t (for one sample d)
noncentral_d2
a noncentral t (for independent samples d)
binomial
a binomial distribution
The parameters that need to be specified will be dependent on the family
When family
is set to normal
then the following
parameters must be set
mean
mean of the normal likelihood
sd
standard deviation of the normal likelihood
When family
is set to student_t
then the following
parameters may be set
mean
mean of the scaled and shifted t likelihood
sd
standard deviation of the scaled and shifted t likelihood
df
degrees of freedom
When family
is set to noncentral_t
then the following
parameters may be set
t
the t value of the data
df
degrees of freedom
When family
is set to noncentral_d
then the following
parameters may be set
d
the d (mean / sd) value of the data
n
the sample size
When family
is set to noncentral_d2
then the following
parameters may be set
d
the d (mean / s_pooled) value of the data
n1
the sample size of group 1
n2
the sample size of group 2
is set as below:
When the family
is set to binomial
then the following
parameters may be set
successes
the number of successes
trials
the number of trials
an object of class likelihood
# specify a normal likelihood likelihood(family = "normal", mean = 5.5, sd = 32.35) # specify a scaled and shifted t likelihood likelihood(family = "student_t", mean = 5.5, sd = 32.35, df = 10) # specify non-central t likelihood (t scaled) likelihood(family = "noncentral_t", t = 10, df = 10) # specify non-central t likelihood (d scaled) likelihood(family = "noncentral_d", d = 10, n = 10) # specify non-central t likelihood (independent samples d scaled) likelihood(family = "noncentral_d2", d = 10, n1 = 10, n2 = 12) # specify a binomial likelihood likelihood(family = "binomial", successes = 2, trials = 10)
# specify a normal likelihood likelihood(family = "normal", mean = 5.5, sd = 32.35) # specify a scaled and shifted t likelihood likelihood(family = "student_t", mean = 5.5, sd = 32.35, df = 10) # specify non-central t likelihood (t scaled) likelihood(family = "noncentral_t", t = 10, df = 10) # specify non-central t likelihood (d scaled) likelihood(family = "noncentral_d", d = 10, n = 10) # specify non-central t likelihood (independent samples d scaled) likelihood(family = "noncentral_d2", d = 10, n1 = 10, n2 = 12) # specify a binomial likelihood likelihood(family = "binomial", successes = 2, trials = 10)
Get names from data slot
## S4 method for signature 'bayesplay' names(x)
## S4 method for signature 'bayesplay' names(x)
x |
a |
the field names from the data slot
Plots an object created by bayesplay
## S3 method for class 'prior' plot(x, ...) ## S3 method for class 'posterior' plot(x, add_prior = FALSE, ...) ## S3 method for class 'likelihood' plot(x, ...) ## S3 method for class 'product' plot(x, ...) ## S3 method for class 'prediction' plot(x, model_name = "model", ...) ## S3 method for class 'robustness' plot(x, ...)
## S3 method for class 'prior' plot(x, ...) ## S3 method for class 'posterior' plot(x, add_prior = FALSE, ...) ## S3 method for class 'likelihood' plot(x, ...) ## S3 method for class 'product' plot(x, ...) ## S3 method for class 'prediction' plot(x, model_name = "model", ...) ## S3 method for class 'robustness' plot(x, ...)
x |
a |
... |
arguments passed to methods |
add_prior |
set to TRUE to add prior to the posterior plot |
model_name |
name of the model |
a ggplot2
object
Define priors using different different distribution families
prior(family, ...)
prior(family, ...)
family |
the prior distribution (see details) |
... |
see details |
The following distributions families can be used for the prior
normal
a normal distribution
student_t
a scaled and shifted t-distribution
cauchy
a Cauchy distribution
uniform
a uniform distribution
point
a point
beta
a beta distribution
The parameters that need to be specified will be dependent on the family
When family
is set to normal
then the following
parameters may be be set
mean
mean of the normal prior
sd
standard deviation of the normal prior
range
(optional) a vector specifying the parameter range
When family
is set to student_t
then the following
parameters may be set
mean
mean of the scaled and shifted t prior
sd
standard deviation of the scaled and shifted t prior
df
degrees of freedom of the scaled and shifted t prior
range
(optional) a vector specifying the parameter range
When family
is set to cauchy
then the following
parameters may be set
location
the centre of the Cauchy distribution (default: 0)
scale
the scale of the Cauchy distribution
range
(optional) a vector specifying the parameter range
When family
is set to uniform
then the following
parameters must be set
min
the lower bound
max
the upper bound
When family
is set to point
then the following
parameters may be set
point
the location of the point prior (default: 0)
When family
is set to beta
then the following
parameters may be set
alpha
the first shape parameter
beta
the second shape parameter
an object of class prior
# specify a normal prior prior(family = "normal", mean = 0, sd = 13.3) # specify a half-normal (range 0 to Infinity) prior prior(family = "normal", mean = 0, sd = 13.3, range = c(0, Inf)) # specify a student t prior prior(family = "student_t", mean = 0, sd = 13.3, df = 79) # specify a truncated t prior prior(family = "student_t", mean = 0, sd = 13.3, df = 79, range = c(-40, 40)) # specify a cauchy prior prior(family = "cauchy", location = 0, scale = .707) # specify a half cauchy prior prior(family = "cauchy", location = 0, scale = 1, range = c(-Inf, 0)) # specify a uniform prior prior(family = "uniform", min = 0, max = 20) # specify a point prior prior(family = "point", point = 0) # specify a beta prior prior(family = "beta", alpha = 2.5, beta = 3.8)
# specify a normal prior prior(family = "normal", mean = 0, sd = 13.3) # specify a half-normal (range 0 to Infinity) prior prior(family = "normal", mean = 0, sd = 13.3, range = c(0, Inf)) # specify a student t prior prior(family = "student_t", mean = 0, sd = 13.3, df = 79) # specify a truncated t prior prior(family = "student_t", mean = 0, sd = 13.3, df = 79, range = c(-40, 40)) # specify a cauchy prior prior(family = "cauchy", location = 0, scale = .707) # specify a half cauchy prior prior(family = "cauchy", location = 0, scale = 1, range = c(-Inf, 0)) # specify a uniform prior prior(family = "uniform", min = 0, max = 20) # specify a point prior prior(family = "point", point = 0) # specify a beta prior prior(family = "beta", alpha = 2.5, beta = 3.8)
Computes the Saveage-Dickey density ratio from a posterior
object
at a specified point
sd_ratio(x, point)
sd_ratio(x, point)
x |
a |
point |
the point at which to evaluate the Savage-Dickey ratio |
A numeric of the Savage-Dickey density ratio
# define a likelihood data_model <- likelihood(family = "normal", mean = 5.5, sd = 32.35) # define a prior prior_model <- prior(family = "normal", mean = 5.5, sd = 13.3) model <- extract_posterior(data_model * prior_model) # compute the Savage-Dickey density ratio at 0 sd_ratio(model, 0)
# define a likelihood data_model <- likelihood(family = "normal", mean = 5.5, sd = 32.35) # define a prior prior_model <- prior(family = "normal", mean = 5.5, sd = 13.3) model <- extract_posterior(data_model * prior_model) # compute the Savage-Dickey density ratio at 0 sd_ratio(model, 0)
Provide a verbal summary of a Bayes factor and the level of evidence
## S4 method for signature 'bf' summary(object)
## S4 method for signature 'bf' summary(object)
object |
a |
No return, called for side effects
Provide a summary of a robustness analysis
## S4 method for signature 'robustness' summary(object)
## S4 method for signature 'robustness' summary(object)
object |
a |
No return, called for side effects
Visually compare two models
visual_compare(model1, model2, ratio = FALSE)
visual_compare(model1, model2, ratio = FALSE)
model1 |
a |
model2 |
a |
ratio |
show ratio rather than comparison (default: FALSE) |
A ggplot2
object
# define two models data_model <- likelihood(family = "normal", .5, 1) h0_mod <- prior(family = "point", point = 0) h1_mod <- prior(family = "normal", mean = 0, sd = 10) m0 <- extract_predictions(data_model * h0_mod) m1 <- extract_predictions(data_model * h1_mod) # visually compare the model visual_compare(m0, m1) # plot the ratio of the two model predictions visual_compare(m0, m1, ratio = TRUE)
# define two models data_model <- likelihood(family = "normal", .5, 1) h0_mod <- prior(family = "point", point = 0) h1_mod <- prior(family = "normal", mean = 0, sd = 10) m0 <- extract_predictions(data_model * h0_mod) m1 <- extract_predictions(data_model * h1_mod) # visually compare the model visual_compare(m0, m1) # plot the ratio of the two model predictions visual_compare(m0, m1, ratio = TRUE)