Skip to content

Commit

Permalink
allow more families
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jul 9, 2024
1 parent 4e8207b commit 6552724
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 8 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions R/convert_bayesian_to_frequentist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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)
Expand Down

0 comments on commit 6552724

Please sign in to comment.