Skip to content

Commit

Permalink
Merge branch 'main' into use-new-dlogspline
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Feb 7, 2024
2 parents da69f9e + d29b70c commit 418b31c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 66 deletions.
6 changes: 3 additions & 3 deletions 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.1.7
Version: 0.13.1.9
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down Expand Up @@ -66,8 +66,8 @@ Description: Provides utilities to describe posterior
Depends:
R (>= 3.6)
Imports:
insight (>= 0.19.5),
datawizard (>= 0.9.0),
insight (>= 0.19.8),
datawizard (>= 0.9.1),
graphics,
methods,
stats,
Expand Down
3 changes: 1 addition & 2 deletions R/bayesfactor_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,8 @@ bayesfactor_parameters.stanreg <- function(posterior,
component <- match.arg(component)

samps <- .clean_priors_and_posteriors(posterior, prior,
verbose = verbose,
effects = effects, component = component,
parameters = parameters
parameters = parameters, verbose = verbose
)

# Get BFs
Expand Down
20 changes: 10 additions & 10 deletions R/rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ rope.mcmc.list <- rope.bcplm


rope <- data.frame(
"CI" = ci,
"ROPE_low" = range[1],
"ROPE_high" = range[2],
"ROPE_Percentage" = rope_percentage
CI = ci,
ROPE_low = range[1],
ROPE_high = range[2],
ROPE_Percentage = rope_percentage
)

attr(rope, "HDI_area") <- c(ci_bounds$CI_low, ci_bounds$CI_high)
Expand Down Expand Up @@ -493,7 +493,9 @@ rope.sim.merMod <- function(x,
tmp <- getropedata$tmp
HDI_area <- getropedata$HDI_area

if (!insight::is_empty_object(tmp)) {
if (insight::is_empty_object(tmp)) {
tmp <- NULL
} else {
tmp <- .clean_up_tmp_stanreg(
tmp,
group = .x,
Expand All @@ -504,8 +506,6 @@ rope.sim.merMod <- function(x,
if (!insight::is_empty_object(HDI_area)) {
attr(tmp, "HDI_area") <- HDI_area
}
} else {
tmp <- NULL
}

tmp
Expand Down Expand Up @@ -552,7 +552,9 @@ rope.sim <- function(x, range = "default", ci = 0.95, ci_method = "ETI", paramet
dat <- getropedata$tmp
HDI_area <- getropedata$HDI_area

if (!insight::is_empty_object(dat)) {
if (insight::is_empty_object(dat)) {
dat <- NULL
} else {
dat <- .clean_up_tmp_stanreg(
dat,
group = "fixed",
Expand All @@ -563,8 +565,6 @@ rope.sim <- function(x, range = "default", ci = 0.95, ci_method = "ETI", paramet
if (!insight::is_empty_object(HDI_area)) {
attr(dat, "HDI_area") <- HDI_area
}
} else {
dat <- NULL
}

attr(dat, "object_name") <- insight::safe_deparse_symbol(substitute(x))
Expand Down
78 changes: 38 additions & 40 deletions R/rope_range.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ rope_range.default <- function(x, verbose = TRUE, ...) {
ret <- Map(
function(i, j, ...) .rope_range(x, i, j), information, response, response_transform, verbose
)
return(ret)
} else {
.rope_range(x, information, response, response_transform, verbose)
ret <- .rope_range(x, information, response, response_transform, verbose)
}
ret
}


Expand Down Expand Up @@ -114,44 +114,42 @@ rope_range.mlm <- function(x, verbose = TRUE, ...) {

.rope_range <- function(x, information = NULL, response = NULL, response_transform = NULL, verbose = TRUE) {
negligible_value <- tryCatch(
{
if (!is.null(response_transform) && grepl("log", response_transform, fixed = TRUE)) {
# for log-transform, we assume that a 1% change represents the ROPE adequately
# see https://github.com/easystats/bayestestR/issues/487
0.01
} else if (information$is_linear && information$link_function == "log") {
# for log-transform, we assume that a 1% change represents the ROPE adequately
# see https://github.com/easystats/bayestestR/issues/487
0.01
} else if (information$family == "lognormal") {
# for log-transform, we assume that a 1% change represents the ROPE adequately
# see https://github.com/easystats/bayestestR/issues/487
0.01
} else if (!is.null(response) && information$link_function == "identity") {
# Linear Models
0.1 * stats::sd(response, na.rm = TRUE)
# 0.1 * stats::sigma(x) # https://github.com/easystats/bayestestR/issues/364
} else if (information$is_logit) {
# Logistic Models (any)
# Sigma==pi / sqrt(3)
0.1 * pi / sqrt(3)
} else if (information$is_probit) {
# Probit models
# Sigma==1
0.1 * 1
} else if (information$is_correlation) {
# Correlations
# https://github.com/easystats/bayestestR/issues/121
0.05
} else if (information$is_count) {
# Not sure about this
sig <- stats::sigma(x)
if (is.null(sig) || length(sig) == 0 || is.na(sig)) stop()
0.1 * sig
} else {
# Default
stop()
}
if (!is.null(response_transform) && grepl("log", response_transform, fixed = TRUE)) {
# for log-transform, we assume that a 1% change represents the ROPE adequately
# see https://github.com/easystats/bayestestR/issues/487
0.01
} else if (information$is_linear && information$link_function == "log") {
# for log-transform, we assume that a 1% change represents the ROPE adequately
# see https://github.com/easystats/bayestestR/issues/487
0.01
} else if (information$family == "lognormal") {
# for log-transform, we assume that a 1% change represents the ROPE adequately
# see https://github.com/easystats/bayestestR/issues/487
0.01
} else if (!is.null(response) && information$link_function == "identity") {
# Linear Models
0.1 * stats::sd(response, na.rm = TRUE)
# 0.1 * stats::sigma(x) # https://github.com/easystats/bayestestR/issues/364
} else if (information$is_logit) {
# Logistic Models (any)
# Sigma==pi / sqrt(3)
0.1 * pi / sqrt(3)
} else if (information$is_probit) {
# Probit models
# Sigma==1
0.1 * 1
} else if (information$is_correlation) {
# Correlations
# https://github.com/easystats/bayestestR/issues/121
0.05
} else if (information$is_count) {
# Not sure about this
sig <- stats::sigma(x)
if (is.null(sig) || length(sig) == 0 || is.na(sig)) stop(call. = FALSE)
0.1 * sig
} else {
# Default
stop(call. = FALSE)
},
error = function(e) {
if (isTRUE(verbose)) {
Expand Down
7 changes: 3 additions & 4 deletions R/si.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ si.stanreg <- function(posterior, prior = NULL,
component <- match.arg(component)

samps <- .clean_priors_and_posteriors(posterior, prior,
verbose = verbose,
effects = effects, component = component,
parameters = parameters
parameters = parameters, verbose = verbose
)

# Get SIs
Expand Down Expand Up @@ -318,7 +317,7 @@ si.rvar <- si.draws
x_supported <- stats::na.omit(x_axis[crit])
if (length(x_supported) < 2) {
return(c(NA, NA))
} else {
range(x_supported)
}

range(x_supported)
}
12 changes: 7 additions & 5 deletions R/utils_bayesfactor.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# clean priors and posteriors ---------------------------------------------

#' @keywords internal
.clean_priors_and_posteriors <- function(posterior, prior,
verbose = TRUE, ...) {
.clean_priors_and_posteriors <- function(posterior, prior, ...) {
UseMethod(".clean_priors_and_posteriors")
}

#' @keywords internal
.clean_priors_and_posteriors.stanreg <- function(posterior, prior,
effects, component,
parameters = NULL,
verbose = TRUE,
effects, component, ...) {
...) {
# Get Priors
if (is.null(prior)) {
prior <- posterior
Expand Down Expand Up @@ -65,7 +66,8 @@
#' @keywords internal
.clean_priors_and_posteriors.emmGrid <- function(posterior,
prior,
verbose = TRUE) {
verbose = TRUE,
...) {
insight::check_if_installed("emmeans")

if (is.null(prior)) {
Expand Down Expand Up @@ -126,7 +128,7 @@
}

.clean_priors_and_posteriors.emm_list <- function(posterior, prior,
verbose = TRUE) {
verbose = TRUE, ...) {
if (is.null(prior)) {
prior <- posterior
insight::format_warning("Prior not specified! Please provide the original model to get meaningful results.")
Expand Down
1 change: 0 additions & 1 deletion man/bayestestR-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/testthat/test-rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ test_that("rope (brms)", {
expect_equal(rope$ROPE_Percentage, c(0.00, 0.00, 0.50), tolerance = 0.1)
})

model <- brm(bf(mvbind(mpg, disp) ~ wt + gear) + set_rescor(TRUE), data = mtcars, iter = 500, refresh = 0)
skip_on_os("mac")

model <- suppressWarnings(brm(bf(mvbind(mpg, disp) ~ wt + gear) + set_rescor(TRUE), data = mtcars, iter = 500, refresh = 0))
rope <- rope(model, verbose = FALSE)

test_that("rope (brms, multivariate)", {
Expand Down

0 comments on commit 418b31c

Please sign in to comment.