Skip to content
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

Issues with blavaan #656

Merged
merged 3 commits into from
Jul 23, 2024
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
36 changes: 17 additions & 19 deletions R/bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
cl$...$estimator <- cl$...$check_response <- NULL


names(mods) <- sapply(cl$`...`, insight::safe_deparse)

Check warning on line 180 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_models.R,line=180,col=28,[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.

Check warning on line 180 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/bayesfactor_models.R,line=180,col=28,[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.
names(denominator) <- insight::safe_deparse(cl$denominator)

mods <- .cleanup_BF_models(mods, denominator, cl)
Expand All @@ -196,12 +196,12 @@
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 @@
# 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 @@ -346,7 +346,7 @@
denominator <- list(denominator)

cl <- match.call(expand.dots = FALSE)
names(mods) <- sapply(cl$`...`, insight::safe_deparse)

Check warning on line 349 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_models.R,line=349,col=28,[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.

Check warning on line 349 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/bayesfactor_models.R,line=349,col=28,[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.
names(denominator) <- insight::safe_deparse(cl$denominator)

mods <- .cleanup_BF_models(mods, denominator, cl)
Expand All @@ -373,10 +373,10 @@
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 @@
.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.

Check warning on line 449 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

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 @@
} else {
denominator <- denominator_model
}
} else {
denominator <- denominator[[1]]
}

attr(mods, "denominator") <- denominator
Expand All @@ -483,6 +477,10 @@
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 Expand Up @@ -567,7 +565,7 @@
return(m_txt)
},
error = function(e) {
return(m_names)

Check warning on line 568 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/bayesfactor_models.R,line=568,col=7,[return_linter] Use implicit return behavior; explicit return() is not needed.

Check warning on line 568 in R/bayesfactor_models.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/bayesfactor_models.R,line=568,col=7,[return_linter] Use implicit return behavior; explicit return() is not needed.
}
)
}
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,26 +75,19 @@

## 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)))

Check warning on line 82 in tests/testthat/test-blavaan.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-blavaan.R,line=82,col=18,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
expect_identical(nrow(x), 10L)

x <- expect_warning(si(bfit, prior = bfit_prior))
expect_identical(nrow(x), 10L)

## Prior/posterior checks ----
suppressWarnings(x <- check_prior(bfit))

Check warning on line 89 in tests/testthat/test-blavaan.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-blavaan.R,line=89,col=20,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
expect_equal(nrow(x), 13)

Check warning on line 90 in tests/testthat/test-blavaan.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-blavaan.R,line=90,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.

x <- check_prior(bfit, simulate_priors = FALSE)
expect_identical(nrow(x), 10L)
Expand Down
Loading