Skip to content

properly compute BF_M for non uniform priors #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions R/commonAnovaBayesian.R
Original file line number Diff line number Diff line change
Expand Up @@ -782,26 +782,36 @@ BANOVAcomputMatchedInclusion <- function(effectNames, effects.matrix, interactio
# no errors, proceed normally and complete the table

logsumbfs <- logSumExp(logbfs + logprior)
internalTable[["P(M|data)"]] <- exp(logbfs + logprior - logsumbfs)
logPostProbModel <- logbfs + logprior - logsumbfs
internalTable[["P(M|data)"]] <- exp(logPostProbModel)

nmodels <- nrow(internalTable)
for (i in seq_len(nmodels)) {
internalTable[i, "BFM"] <- logbfs[i] - logSumExp(logbfs[-i]) + log(nmodels - 1L)
}
logNumPostOdds <- logPostProbModel
logDenPostOdds <- log1mexp(logNumPostOdds)
logNumPriorOdds <- logprior
logDenPriorOdds <- log1mexp(logNumPriorOdds)
internalTable[["BFM"]] <- logNumPostOdds - logDenPostOdds + logDenPriorOdds - logNumPriorOdds

} else {
# create table excluding failed models

idxGood <- !is.na(logbfs)
widxGood <- which(idxGood)
logsumbfs <- logSumExp(logbfs[idxGood])
internalTable[["P(M|data)"]] <- exp(logbfs - logsumbfs)

nmodels <- sum(idxGood)
# normalize the prior w.r.t the non-failed models
logpriorSubset <- logprior[idxGood]
logpriorSubset <- logpriorSubset - logSumExp(logpriorSubset)

logsumbfs <- logSumExp(logbfs[idxGood] + logpriorSubset)
logPostProbModel <- logbfs[idxGood] + logpriorSubset - logsumbfs
internalTable[widxGood, "P(M|data)"] <- exp(logPostProbModel)

logNumPostOdds <- logPostProbModel
logDenPostOdds <- log1mexp(logNumPostOdds)
logNumPriorOdds <- logpriorSubset
logDenPriorOdds <- log1mexp(logNumPriorOdds)
internalTable[widxGood, "BFM"] <- logNumPostOdds - logDenPostOdds + logDenPriorOdds - logNumPriorOdds

widxBad <- which(!idxGood)
for (i in widxGood) {
internalTable[["BFM"]][i] <- logbfs[i] - logSumExp(logbfs[-c(i, widxBad)]) + log(nmodels - 1L)
}

internalTable[widxBad, "P(M|data)"] <- NaN
internalTable[widxBad, "BFM"] <- NaN
Expand Down Expand Up @@ -2832,6 +2842,17 @@ BANOVAcomputMatchedInclusion <- function(effectNames, effects.matrix, interactio
return(interactions.matrix)
}

#' Accurately compute log(1 - exp(x))
#'
#' @param x numeric value or vector
#'
#' @details See https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf
#'
log1mexp <- function(x) {
a0 <- -0.69314718055994528623 # log(1 / 2)
ifelse(x < a0, log1p(-exp(x)), log(-expm1(x)))
}

# Model prior ----
.BANOVAcomputePriorModelProbs <- function(models, nuisance, options) {

Expand Down
Binary file modified tests/testthat/BANOVA_modelPriors.rds
Binary file not shown.
136 changes: 68 additions & 68 deletions tests/testthat/_snaps/anova/raincloud-plots-horizontal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 68 additions & 68 deletions tests/testthat/_snaps/anova/raincloud-plots-vertical.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
344 changes: 172 additions & 172 deletions tests/testthat/_snaps/anovarepeatedmeasures/raincloud-plots.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/testthat/test-anovabayesian.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ test_that("Model prior changes posterior model probabilities", {
} else {
jaspTools::expect_equal_tables(tableModelComparison, reference[[modelPrior]][["modelComparison"]])
jaspTools::expect_equal_tables(tableEffects, reference[[modelPrior]][["posteriorSummary"]])

# internal consistency check
BFM <- vapply(tableModelComparison, `[[`, "BFM", FUN.VALUE = numeric(1))
prior_model_prob <- vapply(tableModelComparison, `[[`, "P(M)", FUN.VALUE = numeric(1))
post_model_prob <- vapply(tableModelComparison, `[[`, "P(M|data)", FUN.VALUE = numeric(1))
testthat::expect_equal(BFM, post_model_prob / (1 - post_model_prob) * (1 - prior_model_prob) / prior_model_prob)
}
}
if (createReference) saveRDS(reference, referencePath)
Expand Down
Loading