diff --git a/DESCRIPTION b/DESCRIPTION index 52131b894..721c4040f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: bayestestR Title: Understand and Describe Bayesian Models and Posterior Distributions -Version: 0.13.2.3 +Version: 0.13.2.4 Authors@R: c(person(given = "Dominique", family = "Makowski", diff --git a/NEWS.md b/NEWS.md index 88d65e273..333f8f6a3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,14 +8,19 @@ * `estimate_density()` +## Changes + +* `bayesian_as_frequentist()` now supports more model families from Bayesian + models that can be successfully converted to their frequentists counterparts. + # bayestestR 0.13.2 ## Breaking Changes * `pd_to_p()` now returns 1 and a warning for values smaller than 0.5. -* `map_estimate()`, `p_direction()`, `p_map()`, and `p_significance()` now - return a data-frame when the input is a numeric vector. (making the output +* `map_estimate()`, `p_direction()`, `p_map()`, and `p_significance()` now + return a data-frame when the input is a numeric vector. (making the output consistently a data frame for all inputs.) * Argument `posteriors` was renamed into `posterior`. Before, there were a mix @@ -43,7 +48,7 @@ * Improved speed performance to `bayesfactor_models()` for `brmsfit` objects that already included a `marglik` element in the model object. - + ## New functionality * `as.logical()` for `bayesfactor_restricted()` results, extracts the boolean diff --git a/R/convert_bayesian_to_frequentist.R b/R/convert_bayesian_to_frequentist.R index 83cc6adf5..29f2c59bf 100644 --- a/R/convert_bayesian_to_frequentist.R +++ b/R/convert_bayesian_to_frequentist.R @@ -45,26 +45,45 @@ convert_bayesian_as_frequentist <- function(model, data = NULL, REML = TRUE) { info <- insight::model_info(model, verbose = FALSE) formula <- insight::find_formula(model) - family <- insight::get_family(model) - if (inherits(family, "brmsfamily")) { - family <- get(family$family)(link = family$link) + model_family <- insight::get_family(model) + if (inherits(model_family, "brmsfamily")) { + insight::check_if_installed("glmmTMB") + # exception: ordbetareg() + if ("custom" %in% model_family$family && all(model_family$name == "ord_beta_reg")) { + model_family <- glmmTMB::ordbeta() + } else { + # not all families return proper objects from "get", so we capture + # some families via switch here... + model_family <- .safe(switch( + model_family$family, + "beta" = glmmTMB::beta_family(link = model_family$link), + "beta_binomial" = glmmTMB::betabinomial(link = model_family$link), + "negbinomial" = glmmTMB::nbinom1(link = model_family$link), + "lognormal" = glmmTMB::lognormal(link = model_family$link), + "student" = glmmTMB::t_family(link = model_family$link), + get(model_family$family)(link = model_family$link) + )) + if (is.null(model_family)) { + insight::format_error("Model could not be automatically converted to frequentist model.") + } + } } freq <- tryCatch(.convert_bayesian_as_frequentist( - info = info, formula = formula, data = data, family = family, REML = REML + info = info, formula = formula, data = data, family = model_family, REML = REML ), error = function(e) e) if (inherits(freq, "error")) { - family <- get(family$family)(link = family$link) + model_family <- get(model_family$family)(link = model_family$link) freq <- .convert_bayesian_as_frequentist( - info = info, formula = formula, data = data, family = family, REML = REML + info = info, formula = formula, data = data, family = model_family, REML = REML ) } if (inherits(freq, "error")) { insight::format_error("Model could not be automatically converted to frequentist model.") } else { - return(freq) + freq } } @@ -79,7 +98,7 @@ convert_bayesian_as_frequentist <- function(model, data = NULL, REML = TRUE) { # subset, # knots, # meta-analysis - if (info$is_dispersion || info$is_zero_inflated || info$is_zeroinf || info$is_hurdle) { + if (info$is_dispersion || info$is_beta || info$is_zero_inflated || info$is_zeroinf || info$is_hurdle) { insight::check_if_installed("glmmTMB") cond_formula <- .rebuild_cond_formula(formula)