knitr::opts_chunk$set(echo = TRUE, message = FALSE,warning = FALSE, fig.path = "man/")
set.seed(4)

BFEffects r packageVersion("BFEffects")

This (mini) R package allows for further computation of BFs based on models built with BayesFactor (but not only).

Download and Install

You can install BFEffects from github with:

# install.packages("devtools")
devtools::install_github("mattansb/BFEffects")

Example use

Restricting Models

The following example is a reproduction of the example given here by Richard Morey (a must read to truly understand restricted model BFs).

Original Analysis

library(BayesFactor)
# Load the data
disgust_data <- read.table(url('http://www.learnbayes.org/disgust_example.txt'),header=TRUE)
# Compute Model
BF <- anovaBF(score ~ condition, data = disgust_data, progress = FALSE)
# Sample from the posterior distribution
samples <- posterior(BF, iterations = 10000, progress = FALSE)
# Check order constraint
consistent <- 
  (samples[, "condition-control"] > samples[, "condition-lemon"]) &
  (samples[, "condition-sulfur"] > samples[, "condition-control"])
N_consistent <- sum(consistent)
# Compute the Bayes factor of the restriction to the full model
bf_restriction_against_full <- (N_consistent / 10000) / (1 / 6)
# Compute the Bayes factor of our restriction against the null hypothesis
bf_full_against_null <- unname(as.vector(BF))
bf_restriction_against_null <- bf_restriction_against_full * bf_full_against_null

After all that, we get:

c(BF_res.full = bf_restriction_against_full,
  BF_full.0   = bf_full_against_null,
  BF_res.0    = bf_restriction_against_null)

Using restrict

... or, we could use restrict!

library(BayesFactor)
library(BFEffects)
# Load the data
disgust_data <- read.table(url('http://www.learnbayes.org/disgust_example.txt'),header=TRUE)
# Compute Model
BF <- anovaBF(score ~ condition, data = disgust_data, progress = FALSE)
# Restrict the model
BF.r <- restrict(BF, lemon < control & control < sulfur)
BF.r

As we can see, we've reproduced the results.

Inclusion BFs

This function has been moved to bayestestR::bayesfactor_inclusion

This is an R version of JASP's output > effects option:

library(BayesFactor)
library(BFEffects)

BF <- generalTestBF(len ~ supp * dose, ToothGrowth, progress = FALSE)

BF

inclusionBF(BF)

Bayes Factors from other model types

This function has been moved to bayestestR::bayesfactor_models

If you wish to compute BFs from non-BayesFactor models, you can do so with the following functions:

  1. BIC_BFs - compute BF based on BIC approcimations (see more here).
  2. stan_BFs - compute BF from models fitted with brms and rstanarm packages via marginal likelihoods (using the bridgesampling package).

These functions produce a BFGrid object that prints nicely:

library(BFEffects)
mo0 <- lm(Sepal.Length ~ 1, data = iris)
mo1 <- lm(Sepal.Length ~ Species, data = iris)
mo2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
mo3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
BIC_BFs(mo0,mo1,mo2,mo3, .den = 1) # .den determines which of the models to compare to

To Do List

  1. More BayesFactor things

  2. [ ] Sample from priors?

    • [ ] inferBF?


mattansb/BFEffect documentation built on June 7, 2019, 8:49 p.m.