BF.default {BFpack} | R Documentation |
Bayes factors for Bayesian exploratory and confirmatory hypothesis testing
Description
The BF
function can be used for hypothesis testing and
model selection using the Bayes factor. By default exploratory hypothesis tests are
performed of whether each model parameter equals zero, is negative, or is
positive. Confirmatory hypothesis tests can be executed by specifying hypotheses with
equality and/or order constraints on the parameters of interest. Depending on the
class of the fitted model different Bayes factors are used as described in the output.
Usage
## Default S3 method:
BF(
x,
hypothesis = NULL,
prior.hyp.explo = NULL,
prior.hyp.conf = NULL,
prior.hyp = NULL,
complement = TRUE,
log = FALSE,
Sigma,
n,
...
)
## S3 method for class 'lm'
BF(
x,
hypothesis = NULL,
prior.hyp.explo = NULL,
prior.hyp.conf = NULL,
prior.hyp = NULL,
complement = TRUE,
log = FALSE,
BF.type = 2,
iter = 10000,
...
)
## S3 method for class 't_test'
BF(
x,
hypothesis = NULL,
prior.hyp.explo = NULL,
prior.hyp.conf = NULL,
prior.hyp = NULL,
complement = TRUE,
log = FALSE,
BF.type = 2,
iter = 1e+06,
...
)
Arguments
x |
An R object containing the outcome of a statistical analysis.
An R object containing the outcome of a statistical analysis. Currently, the
following objects can be processed: t_test(), bartlett_test(), lm(), aov(),
manova(), cor_test(), lmer() (only for testing random intercep variances),
glm(), coxph(), survreg(), polr(), zeroinfl(), rma(), ergm(), bergm(), or named
vector objects. In the case |
hypothesis |
A character string containing the constrained (informative) hypotheses to
evaluate in a confirmatory test. The default is NULL, which will result in standard exploratory testing
under the model |
prior.hyp.explo |
The prior probabilities of the hypotheses in the exploratory tests. Except for
objects of class |
prior.hyp.conf |
The prior probabilities of the constrained hypotheses in the confirmatory test. |
prior.hyp |
Deprecated. Please use the argument |
complement |
a logical specifying whether the complement should be added
to the tested hypothesis under |
log |
a logical specifying whether the Bayes factors should be computed on a log scale.
Default is |
Sigma |
An approximate posterior covariance matrix (e.g,. error covariance
matrix) of the parameters of interest. This argument is only required when |
n |
The (effective) sample size that was used to acquire the estimates in the named vector
|
... |
Parameters passed to and from other functions. |
BF.type |
An integer that specified the type of Bayes factor (or prior) that is used for the test.
Currently, this argument is only used for models of class 'lm' and 't_test',
where |
iter |
Number of iterations that are used to compute the Monte Carlo estimates (only used for certain hypotheses under multivariate models and when testing group variances). |
Details
The function requires a fitted modeling object. Current analyses
that are supported: t_test
,
bartlett_test
,
aov
, manova
,
lm
, mlm
,
glm
, hetcor
,
lmer
, coxph
,
survreg
, ergm
,
bergm
,
zeroinfl
, rma
and polr
.
For testing parameters from the results of t_test(), lm(), aov(),
manova(), and bartlett_test(), hypothesis testing is done using
adjusted fractional Bayes factors are computed (using minimal fractions).
For testing measures of association (e.g., correlations) via cor_test()
,
Bayes factors are computed using joint uniform priors under the correlation
matrices. For testing intraclass correlations (random intercept variances) via
lmer()
, Bayes factors are computed using uniform priors for the intraclass
correlations. For all other tests, approximate adjusted fractional Bayes factors
(with minimal fractions) are computed using Gaussian approximations, similar as
a classical Wald test.
Value
The output is an object of class BF
. The object has elements:
-
BFtu_exploratory
: The Bayes factors of the constrained hypotheses against the unconstrained hypothesis in the exploratory test. -
BFtu_main
(only foraov
objects with predictors of classfactor
): The Bayes factors of a constrained model where all levels of afactor
are assumed to have the same effect on the outcome variable versus an unconstrained (full) model with no constraints. -
BFtu_interaction
(only foraov
objects with interaction effects with predictors of classfactor
): The Bayes factors of a constrained model where the effect of the dummy variables corresponding to an interaction effects are assumed to be zero versus an unconstrained (full) model with no constraints. -
PHP_exploratory:
The posterior probabilities of the constrained hypotheses in the exploratory test. -
PHP_main
(only foraov
objects with predictors of classfactor
): The posterior probabilities a constrained model where all levels of afactor
are assumed to have the same effect on the outcome variable versus an unconstrained (full) model with no constraints. -
PHP_interaction
(only foraov
objects with interaction effects with predictors of classfactor
): The posterior probabilities of a constrained model where the effect of the dummy variables corresponding to an interaction effects are assumed to be zero versus an unconstrained (full) model with no constraints. -
BFtu_confirmatory
: The Bayes factors of the constrained hypotheses against the unconstrained hypothesis in the confirmatory test using thehypothesis
argument. -
PHP_confirmatory
: The posterior probabilities of the constrained hypotheses in the confirmatory test using thehypothesis
argument. -
BFmatrix_confirmatory
: The evidence matrix which contains the Bayes factors between all possible pairs of hypotheses in the confirmatory test. -
BFtable_confirmatory
: TheSpecification table
(output when printing thesummary
of aBF
for a confirmatory test) which contains the different elements of the extended Savage Dickey density ratio whereThe first column '
complex=
' quantifies the relative complexity of the equality constraints of a hypothesis (the prior density at the equality constraints in the extended Savage Dickey density ratio).The second column '
complex>
' quantifies the relative complexity of the order constraints of a hypothesis (the prior probability of the order constraints in the extended Savage Dickey density ratio).The third column '
fit=
' quantifies the relative fit of the equality constraints of a hypothesis (the posterior density at the equality constraints in the extended Savage Dickey density ratio).The fourth column '
fit>
' quantifies the relative fit of the order constraints of a hypothesis (the posterior probability of the order constraints in the extended Savage Dickey density ratio)The fifth column '
BF=
' contains the Bayes factor of the equality constraints against the unconstrained hypothesis.The sixth column '
BF>
' contains the Bayes factor of the order constraints against the unconstrained hypothesis.The seventh column '
BF
' contains the Bayes factor of the constrained hypothesis against the unconstrained hypothesis.The eighth column '
PHP
' contains the posterior probabilities of the hypotheses.
-
prior.hyp.explo
: The prior probabilities of the constrained hypotheses in the exploratory tests. -
prior.hyp.conf
: The prior probabilities of the constrained hypotheses in the confirmatory test. -
hypotheses
: The tested constrained hypotheses in a confirmatory test. -
estimates
: The unconstrained estimates. -
model
: The input modelx
. -
bayesfactor
: The type of Bayes factor that is used for this model. -
parameter
: The type of parameter that is tested. -
log
:logical
whether the Bayes factors were reported on a log scale. -
fraction_number_groupIDs
(only for objects of classlm
): The number of 'group identifiers' that were identified based on the number of unique combinations oflevel
s of predictor variables of classfactor
in the data. These group identifiers are used to automatically specify the minimal fractions that are used to compute (adjusted) fractional Bayes factors. -
fraction_groupID_observations
(only for objects of classlm
): A vector that specifies to which 'group identifier' an observation belongs. The group identifiers are constructed based on the unique combination of thelevels
based on the predictor variables of classfactor
of the observations. -
call
: The call of theBF
function.
Methods (by class)
-
BF(default)
: S3 method for a named vector 'x' -
BF(lm)
: S3 method for an object of class 'lm' -
BF(t_test)
: BF S3 method for an object of class 't_test'
References
Mulder, J., D.R. Williams, Gu, X., A. Tomarken, F. Böing-Messing, J.A.O.C. Olsson-Collentine, Marlyne Meyerink, J. Menke, J.-P. Fox, Y. Rosseel, E.J. Wagenmakers, H. Hoijtink., and van Lissa, C. (2021). BFpack: Flexible Bayes Factor Testing of Scientific Theories in R. Journal of Statistical Software. <https://doi.org/10.18637/jss.v100.i18>
Examples
# EXAMPLE 1. One-sample t test
ttest1 <- t_test(therapeutic, mu = 5)
print(ttest1)
# confirmatory Bayesian one sample t test
BF1 <- BF(ttest1, hypothesis = "mu = 5")
summary(BF1)
# exploratory Bayesian one sample t test
BF(ttest1)
# EXAMPLE 2. ANOVA
aov1 <- aov(price ~ anchor * motivation,data = tvprices)
BF1 <- BF(aov1, hypothesis = "anchorrounded = motivationlow;
anchorrounded < motivationlow")
summary(BF1)
# EXAMPLE 3. linear regression
lm1 <- lm(mpg ~ cyl + hp + wt, data = mtcars)
BF(lm1, hypothesis = "wt < cyl < hp = 0")
# EXAMPLE 4. Logistic regression
fit <- glm(sent ~ ztrust + zfWHR + zAfro + glasses + attract + maturity +
tattoos, family = binomial(), data = wilson)
BF1 <- BF(fit, hypothesis = "ztrust > zfWHR > 0;
ztrust > 0 & zfWHR = 0")
summary(BF1)
# EXAMPLE 5. Correlation analysis
set.seed(123)
cor1 <- cor_test(memory[1:20,c(1,2,6)])
BF1 <- BF(cor1)
summary(BF1)
BF2 <- BF(cor1, hypothesis = "Rat_with_Im > Rat_with_Del > 0;
Rat_with_Im = Rat_with_Del = 0")
summary(BF2)
# correlations can also be computed between continuous/ordinal variables
memory_test <- memory[1:20,c(1,2,6)]
memory_test[,3] <- as.ordered(memory_test[,3])
cor2 <- cor_test(memory_test)
BF(cor2)
# EXAMPLE 6. Bayes factor testing on a named vector
# A Poisson regression model is used to illustrate the computation
# of Bayes factors with a named vector as input
poisson1 <- glm(formula = breaks ~ wool + tension,
data = datasets::warpbreaks, family = poisson)
# extract estimates, error covariance matrix, and sample size:
estimates <- poisson1$coefficients
covmatrix <- vcov(poisson1)
samplesize <- nobs(poisson1)
# compute Bayes factors on equal/order constrained hypotheses on coefficients
BF1 <- BF(estimates, Sigma = covmatrix, n = samplesize, hypothesis =
"woolB > tensionM > tensionH; woolB = tensionM = tensionH")
summary(BF1)