Skip to content

Instantly share code, notes, and snippets.

@crsh
Last active August 2, 2017 15:51
Show Gist options
  • Select an option

  • Save crsh/0b81e420819748edc5d1173b888601ea to your computer and use it in GitHub Desktop.

Select an option

Save crsh/0b81e420819748edc5d1173b888601ea to your computer and use it in GitHub Desktop.
Calculate Bayes factors for specific model comparisons (planned contrasts)
plannedContrastBF <- function(numerator_formula, denominator_formula, multicore = FALSE, ...) {
if(!is.list(numerator_formula)) numerator_formula <- list(numerator_formula)
formulae <- c(denominator_formula, numerator_formula)
if(multicore) {
doMC::registerDoMC()
linear_models <- foreach::"%dopar%"(
foreach::foreach(model_formula = formulae, .options.multicore = list(preschedule = FALSE, set.seed = TRUE)),
BayesFactor::lmBF(model_formula, ...)
)
} else {
linear_models <- list()
for(i in 1:length(formulae)) {
linear_models <- c(linear_models, lmBF(formulae[[i]], ...))
}
}
joined = do.call("c", linear_models)
numerators = joined[2:length(joined)]
denominator = joined[1]
numerators / denominator
}
@crsh
Copy link
Author

crsh commented Aug 2, 2017

data(puzzles)

plannedContrastBF(
  numerator_formula = list(RT ~ shape + color + ID, RT ~ shape + ID)
  , denominator_formula = RT ~ shape*color + ID
  , data = puzzles
  , whichRandom = "ID"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment