Skip to content

Commit

Permalink
Fix #646
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 29, 2023
1 parent da58f2f commit 56fed78
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
12 changes: 11 additions & 1 deletion R/binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#' time-consuming. By default, `show_dots = NULL`. In this case `binned_residuals()`
#' tries to guess whether performance will be poor due to a very large model
#' and thus automatically shows or hides dots.
#' @param verbose Toggle warnings and messages.
#' @param ... Currently not used.
#'
#' @return A data frame representing the data that is mapped in the accompanying
Expand Down Expand Up @@ -83,11 +84,20 @@ binned_residuals <- function(model,
ci_type = c("exact", "gaussian", "boot"),
residuals = c("deviance", "pearson", "response"),
iterations = 1000,
verbose = TRUE,
...) {
# match arguments
ci_type <- match.arg(ci_type)
residuals <- match.arg(residuals)

# for non-bernoulli models, `"exact"` doesn't work
if (isFALSE(insight::model_info(model)$is_bernoulli)) {
ci_type <- "gaussian"
if (verbose) {
insight::format_alert("Using `ci_type = \"gaussian\"` because model is not bernoulli.")
}
}

fitted_values <- stats::fitted(model)
mf <- insight::get_data(model, verbose = FALSE)

Expand Down Expand Up @@ -186,7 +196,7 @@ binned_residuals <- function(model,
}
out <- out / n

quant <- stats::quantile(out, c((1 - ci) / 2, (1 + ci) / 2))
quant <- stats::quantile(out, c((1 - ci) / 2, (1 + ci) / 2), na.rm = TRUE)
c(CI_low = quant[1L], CI_high = quant[2L])
}

Expand Down
2 changes: 1 addition & 1 deletion R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ check_model.model_fit <- function(x,
dat$INFLUENTIAL <- .influential_obs(model, threshold = threshold)
dat$PP_CHECK <- .safe(check_predictions(model, ...))
if (isTRUE(model_info$is_binomial)) {
dat$BINNED_RESID <- binned_residuals(model, ...)
dat$BINNED_RESID <- binned_residuals(model, verbose = verbose, ...)
}
if (isTRUE(model_info$is_count)) {
dat$OVERDISPERSION <- .diag_overdispersion(model)
Expand Down
3 changes: 3 additions & 0 deletions man/binned_residuals.Rd

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

19 changes: 19 additions & 0 deletions tests/testthat/test-binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,22 @@ test_that("binned_residuals, bootstrapped CI", {
tolerance = 1e-4
)
})

test_that("binned_residuals, msg for non-bernoulli", {
skip_on_cran()
tot <- rep(10, 100)
suc <- rbinom(100, prob = 0.9, size = tot)

dat <- data.frame(tot, suc)
dat$prop <- suc / tot
dat$x1 <- as.factor(sample(1:5, 100, replace = TRUE))

mod <- glm(prop ~ x1,
family = binomial,
data = df,
weights = tot
)

expect_message(binned_residuals(mod1), regex = "Using `ci_type = \"gaussian\"`")
expect_silent(binned_residuals(mod1, verbose = FALSE))
})

0 comments on commit 56fed78

Please sign in to comment.