Skip to content

Commit

Permalink
model_paramaters with exponentiate = TRUE not working with bootstrapp…
Browse files Browse the repository at this point in the history
…ed object from bootstrap_model (#1005)

* model_paramaters with exponentiate = TRUE not working with bootstrapped object from bootstrap_model
Fixes #1004

* desc, news

* Update methods_posterior.R

* lintr

* lintr

* add test
  • Loading branch information
strengejacke authored Aug 22, 2024
1 parent 2e2ce84 commit e7f6c44
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 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: parameters
Title: Processing of Model Parameters
Version: 0.22.1.7
Version: 0.22.1.8
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Methods for `degrees_of_freedom()` have been removed. `degrees_of_freedom()`
now calls `insight::get_df()`.

* `model_parameters()` for data frames and `draws` objects from package
*posterior* also gets an `exponentiate` argument.

## Bug fixes

* Fixed issue with warning for spuriously high coefficients for Stan-models
Expand Down
31 changes: 15 additions & 16 deletions R/methods_base.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#' @rdname model_parameters.stanreg
#' @export
model_parameters.data.frame <- function(model, as_draws = FALSE, verbose = TRUE, ...) {
model_parameters.data.frame <- function(model,
as_draws = FALSE,
exponentiate = FALSE,
verbose = TRUE,
...) {
# treat data frame as bootstraps/posteriors?
if (isTRUE(as_draws)) {
return(model_parameters.draws(model, verbose = verbose, ...))
return(model_parameters.draws(model, exponentiate = exponentiate, verbose = verbose, ...))
}
if (isTRUE(verbose)) {
insight::format_warning(
Expand All @@ -22,14 +26,13 @@ model_parameters.data.frame <- function(model, as_draws = FALSE, verbose = TRUE,
#' @rdname standard_error
#' @export
standard_error.factor <- function(model, force = FALSE, verbose = TRUE, ...) {
if (force) {
standard_error(as.numeric(model), ...)
} else {
if (!force) {
if (verbose) {
insight::format_warning("Can't compute standard error of non-numeric variables.")
}
return(NA)
}
standard_error(as.numeric(model), ...)
}


Expand All @@ -55,10 +58,8 @@ standard_error.list <- function(model, verbose = TRUE, ...) {
model <- model$gam
class(model) <- c("gam", "lm", "glm")
standard_error(model)
} else {
if (isTRUE(verbose)) {
insight::print_color("\nCould not extract standard errors from model object.\n", "red")
}
} else if (isTRUE(verbose)) {
insight::print_color("\nCould not extract standard errors from model object.\n", "red")
}
}

Expand Down Expand Up @@ -136,10 +137,10 @@ p_value.numeric <- function(model, null = 0, ...) {

#' @export
p_value.data.frame <- function(model, ...) {
data <- model[vapply(model, is.numeric, TRUE)]
model_data <- model[vapply(model, is.numeric, TRUE)]
.data_frame(
Parameter = names(data),
p = vapply(data, p_value, 1)
Parameter = names(model_data),
p = vapply(model_data, p_value, 1)
)
}

Expand All @@ -150,9 +151,7 @@ p_value.list <- function(model, method = NULL, verbose = TRUE, ...) {
model <- model$gam
class(model) <- c("gam", "lm", "glm")
p_value(model, method = method)
} else {
if (isTRUE(verbose)) {
insight::format_warning("Could not extract p-values from model object.")
}
} else if (isTRUE(verbose)) {
insight::format_warning("Could not extract p-values from model object.")
}
}
4 changes: 4 additions & 0 deletions R/methods_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ model_parameters.draws <- function(model,
test = "pd",
rope_range = "default",
rope_ci = 0.95,
exponentiate = FALSE,
keep = NULL,
drop = NULL,
verbose = TRUE,
Expand All @@ -33,6 +34,9 @@ model_parameters.draws <- function(model,
...
)

# exponentiate coefficients and SE/CI, if requested
params <- .exponentiate_parameters(params, exponentiate = exponentiate)

attr(params, "ci") <- ci
attr(params, "object_name") <- insight::safe_deparse_symbol(substitute(model))
class(params) <- c("parameters_model", "see_parameters_model", class(params))
Expand Down
35 changes: 21 additions & 14 deletions man/model_parameters.stanreg.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-base.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ test_that("model_parameters.data.frame as draws", {
expect_identical(colnames(mp), c("Parameter", "Median", "CI_low", "CI_high", "pd"))
})

test_that("model_parameters.data.frame as draws, exponentiate", {
data(iris)
mp <- suppressWarnings(model_parameters(iris[1:4], as_draws = TRUE, exponentiate = TRUE))
expect_equal(mp$Median, c(330.29956, 20.08554, 77.47846, 3.6693), tolerance = 1e-2, ignore_attr = TRUE)
})

# require model input
test_that("model_parameters", {
expect_error(model_parameters())
Expand Down

0 comments on commit e7f6c44

Please sign in to comment.