Skip to content

Commit

Permalink
add rope_range method for model_parameters() objects
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 16, 2024
1 parent 43819c7 commit 04c951e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 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.14.0.7
Version: 0.14.0.8
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ S3method(rope,stanreg)
S3method(rope_range,data.frame)
S3method(rope_range,default)
S3method(rope_range,mlm)
S3method(rope_range,parameters_model)
S3method(sensitivity_to_prior,default)
S3method(sensitivity_to_prior,stanreg)
S3method(sexit_thresholds,BFBayesFactor)
Expand Down
42 changes: 0 additions & 42 deletions R/print.equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,3 @@ print.equivalence_test <- function(x, digits = 2, ...) {
cat("\n")
}
}


.retrieve_model <- function(x) {
# retrieve model
obj_name <- attr(x, "object_name", exact = TRUE)
model <- NULL

if (!is.null(obj_name)) {
# first try, parent frame
model <- tryCatch(get(obj_name, envir = parent.frame()), error = function(e) NULL)

if (is.null(model)) {
# second try, global env
model <- tryCatch(get(obj_name, envir = globalenv()), error = function(e) NULL)
}

if (is.null(model)) {
# last try
model <- .dynGet(obj_name, ifnotfound = NULL)
}
}
model
}


.dynGet <- function(x,
ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE),
minframe = 1L,
inherits = FALSE) {
x <- insight::safe_deparse(x)
n <- sys.nframe()
myObj <- structure(list(.b = as.raw(7)), foo = 47L)
while (n > minframe) {
n <- n - 1L
env <- sys.frame(n)
r <- get0(x, envir = env, inherits = inherits, ifnotfound = myObj)
if (!identical(r, myObj)) {
return(r)
}
}
ifnotfound
}
7 changes: 7 additions & 0 deletions R/rope_range.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ rope_range.default <- function(x, verbose = TRUE, ...) {
}


#' @export
rope_range.parameters_model <- function(x, verbose = TRUE, ...) {
model <- .retrieve_model(x)
rope_range.default(x = model, verbose = verbose, ...)
}


#' @export
rope_range.data.frame <- function(x, verbose = TRUE, ...) {
# to avoid errors with "get_response()" in the default method
Expand Down
41 changes: 41 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,47 @@
x[unlist(lapply(x, is.numeric))]
}

#' @keywords internal
.retrieve_model <- function(x) {
# retrieve model
obj_name <- attr(x, "object_name", exact = TRUE)
model <- NULL

if (!is.null(obj_name)) {
# first try, parent frame
model <- .safe(get(obj_name, envir = parent.frame()))

if (is.null(model)) {
# second try, global env
model <- .safe(get(obj_name, envir = globalenv()))
}

if (is.null(model)) {
# last try
model <- .dynGet(obj_name, ifnotfound = NULL)
}
}
model
}

#' @keywords internal
.dynGet <- function(x,
ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE),
minframe = 1L,
inherits = FALSE) {
x <- insight::safe_deparse(x)
n <- sys.nframe()
myObj <- structure(list(.b = as.raw(7)), foo = 47L)
while (n > minframe) {
n <- n - 1L
env <- sys.frame(n)
r <- get0(x, envir = env, inherits = inherits, ifnotfound = myObj)
if (!identical(r, myObj)) {
return(r)
}
}
ifnotfound
}

#' @keywords internal
.get_direction <- function(direction) {
Expand Down

0 comments on commit 04c951e

Please sign in to comment.