Skip to content

Commit

Permalink
Issues with blavaan (#656)
Browse files Browse the repository at this point in the history
* Issues with blavaan
Fixes #627

* add test

* lintr
  • Loading branch information
strengejacke authored Jul 23, 2024
1 parent 7be1c64 commit 0c71434
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
36 changes: 17 additions & 19 deletions R/bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ bayesfactor_models.default <- function(..., denominator = 1, verbose = TRUE) {
supported_models[!has_terms] <- FALSE
}

objects <- .safe(do.call(insight::ellipsis_info, c(mods, verbose = FALSE)))
if (!is.null(objects)) {
were_checked <- inherits(objects, "ListModels")
model_objects <- .safe(do.call(insight::ellipsis_info, c(mods, verbose = FALSE)))
if (!is.null(model_objects)) {
were_checked <- inherits(model_objects, "ListModels")

# Validate response
if (were_checked && verbose && !isTRUE(attr(objects, "same_response"))) {
if (were_checked && verbose && !isTRUE(attr(model_objects, "same_response"))) {
insight::format_warning(
"When comparing models, please note that probably not all models were fit from same data."
)
Expand All @@ -210,7 +210,7 @@ bayesfactor_models.default <- function(..., denominator = 1, verbose = TRUE) {
# Get BIC
if (were_checked && estimator == "REML" &&
any(vapply(mods, insight::is_mixed_model, TRUE)) &&
!isTRUE(attr(objects, "same_fixef")) &&
!isTRUE(attr(model_objects, "same_fixef")) &&
verbose) {
insight::format_warning(paste(
"Information criteria (like BIC) based on REML fits (i.e. `estimator=\"REML\"`)",
Expand Down Expand Up @@ -373,10 +373,10 @@ bayesfactor_models.BFBayesFactor <- function(..., verbose = TRUE) {
mBFs <- c(0, BayesFactor::extractBF(models, TRUE, TRUE))
mforms <- sapply(c(models@denominator, models@numerator), function(x) x@shortName)

if (!inherits(models@denominator, "BFlinearModel")) {
mforms <- .clean_non_linBF_mods(mforms)
} else {
if (inherits(models@denominator, "BFlinearModel")) {
mforms[mforms == "Intercept only"] <- "1"
} else {
mforms <- .clean_non_linBF_mods(mforms)
}

res <- data.frame(
Expand Down Expand Up @@ -446,20 +446,16 @@ as.matrix.bayesfactor_models <- function(x, ...) {
.cleanup_BF_models <- function(mods, denominator, cl) {
if (length(mods) == 1 && inherits(mods[[1]], "list")) {
mods <- mods[[1]]
mod_names <- tryCatch(
{
sapply(cl$`...`[[1]][-1], insight::safe_deparse)
},
error = function(e) {
NULL
}
)
mod_names <- .safe(sapply(cl$`...`[[1]][-1], insight::safe_deparse))

Check warning on line 449 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_models.R,line=449,col=34,[keyword_quote_linter] Only quote targets of extraction with $ if necessary, i.e., if the name is not a valid R symbol (see ?make.names). Use backticks to create non-syntactic names, or use [[ to extract by string.

if (!is.null(mod_names) && length(mod_names) == length(mods)) {
names(mods) <- mod_names
}
}

if (!is.numeric(denominator[[1]])) {
if (is.numeric(denominator[[1]])) {
denominator <- denominator[[1]]
} else {
denominator_model <- which(names(mods) == names(denominator))

if (length(denominator_model) == 0) {
Expand All @@ -468,8 +464,6 @@ as.matrix.bayesfactor_models <- function(x, ...) {
} else {
denominator <- denominator_model
}
} else {
denominator <- denominator[[1]]
}

attr(mods, "denominator") <- denominator
Expand All @@ -483,6 +477,10 @@ as.matrix.bayesfactor_models <- function(x, ...) {
bf_method = "method",
unsupported_models = FALSE,
model_names = NULL) {
# sanity check - are all BF NA?
if (!is.null(res$log_BF) && all(is.na(res$log_BF))) {
insight::format_error("Could not calculate Bayes Factor for these models. You may report this problem at {https://github.com/easystats/bayestestR/issues/}.") # nolint
}
attr(res, "denominator") <- denominator
attr(res, "BF_method") <- bf_method
attr(res, "unsupported_models") <- unsupported_models
Expand Down
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
}

Value <- c(
"left" = -1,
"right" = 1,
left = -1,
right = 1,
"two-sided" = 0,
"twosided" = 0,
twosided = 0,
"one-sided" = 1,
"onesided" = 1,
onesided = 1,
"<" = -1,
">" = 1,
"=" = 0,
Expand Down
11 changes: 2 additions & 9 deletions tests/testthat/test-blavaan.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,8 @@ test_that("blavaan, all", {

## Bayes factors ----

## FIXME: test fails
# expect_warning(bayesfactor_models(bfit, bfit2))
# x <- suppressWarnings(bayesfactor_models(bfit, bfit2))
# expect_lt(x$log_BF[2], 0)

## FIXME: test fails
# expect_warning(weighted_posteriors(bfit, bfit2))
# x <- suppressWarnings(weighted_posteriors(bfit, bfit2))
# expect_identical(ncol(x), 10L)
# For these models, no BF available, see #627
expect_error(bayesfactor_models(bfit, bfit2), regex = "Could not calculate Bayes")

bfit_prior <- unupdate(bfit)
capture.output(x <- expect_warning(bayesfactor_parameters(bfit, prior = bfit_prior)))
Expand Down

0 comments on commit 0c71434

Please sign in to comment.