Skip to content

Commit

Permalink
docs, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 25, 2023
1 parent aaa8eb6 commit 7ad5946
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
24 changes: 17 additions & 7 deletions R/binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
#' taken.
#' @param ci Numeric, the confidence level for the error bounds.
#' @param ci_type Character, the type of error bounds to calculate. Can be
#' `"gaussian"` (default), `"exact"` or `"boot"`.
#' `"exact"` (default), `"gaussian"` or `"boot"`. `"exact"` calculates the
#' error bounds based on the exact binomial distribution, using [`binom.test()`].
#' `"gaussian"` uses the Gaussian approximation, while `"boot"` uses a simple
#' bootstrap method, where confidence intervals are calculated based on the
#' quantiles of the bootstrap distribution.
#' @param residuals Character, the type of residuals to calculate. Can be
#' `"response"` (default), `"pearson"` or `"deviance"`.
#' `"deviance"` (default), `"pearson"` or `"response"`. It is recommended to
#' use `"response"` only for those models where other residuals are not
#' available.
#' @param iterations Integer, the number of iterations to use for the
#' bootstrap method. Only used if `ci_type = "boot"`.
#' @param show_dots Logical, if `TRUE`, will show data points in the plot. Set
Expand Down Expand Up @@ -74,8 +80,8 @@ binned_residuals <- function(model,
n_bins = NULL,
show_dots = NULL,
ci = 0.95,
ci_type = c("gaussian", "exact", "boot"),
residuals = c("response", "pearson", "deviance"),
ci_type = c("exact", "gaussian", "boot"),
residuals = c("deviance", "pearson", "response"),
iterations = 1000,
...) {
# match arguments
Expand Down Expand Up @@ -106,6 +112,11 @@ binned_residuals <- function(model,
deviance = .safe(stats::residuals(model, type = "deviance"))
)

# make sure we really have residuals
if (is.null(y)) {
insight::format_error("Could not calculate residuals. Try using `residuals = \"response\"`.")
}

if (is.null(n_bins)) n_bins <- round(sqrt(length(pred)))

breaks.index <- floor(length(pred) * (1:(n_bins - 1)) / n_bins)
Expand All @@ -124,7 +135,7 @@ binned_residuals <- function(model,
r <- switch(ci_type,
gaussian = stats::qnorm(c((1 - ci) / 2, (1 + ci) / 2), mean = ybar, sd = sdev / sqrt(n)),
exact = {
out <- stats:::binom.test(sum(y0[items]), n)$conf.int
out <- stats::binom.test(sum(y0[items]), n)$conf.int
out <- out - (min(out) - ybar) - (diff(out) / 2)
out
},
Expand All @@ -138,8 +149,7 @@ binned_residuals <- function(model,
n = n,
x.lo = model.range[1],
x.hi = model.range[2],
se = stats::qnorm((1 + ci) / 2) * sdev / sqrt(n),
ci_range = sdev / sqrt(n)
se = stats::qnorm((1 + ci) / 2) * sdev / sqrt(n)
)
cbind(d0, rbind(r))
}))
Expand Down
17 changes: 9 additions & 8 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
#' tries to guess whether performance will be poor due to a very large model
#' and thus automatically shows or hides dots.
#' @param verbose If `FALSE` (default), suppress most warning messages.
#' @param ... Currently not used.
#' @param ... Arguments passed down to the individual check functions, especially
#' to `check_predictions()` and `binned_residuals()`.
#' @inheritParams check_predictions
#'
#' @return The data frame that is used for plotting.
Expand Down Expand Up @@ -185,11 +186,11 @@ check_model.default <- function(x,
ca <- tryCatch(
{
if (minfo$is_bayesian) {
suppressWarnings(.check_assumptions_stan(x))
suppressWarnings(.check_assumptions_stan(x, ...))
} else if (minfo$is_linear) {
suppressWarnings(.check_assumptions_linear(x, minfo, verbose))
suppressWarnings(.check_assumptions_linear(x, minfo, verbose, ...))
} else {
suppressWarnings(.check_assumptions_glm(x, minfo, verbose))
suppressWarnings(.check_assumptions_glm(x, minfo, verbose, ...))
}
},
error = function(e) {
Expand Down Expand Up @@ -346,7 +347,7 @@ check_model.model_fit <- function(x,
threshold <- NULL
}
dat$INFLUENTIAL <- .influential_obs(model, threshold = threshold)
dat$PP_CHECK <- .safe(check_predictions(model))
dat$PP_CHECK <- .safe(check_predictions(model, ...))

dat <- insight::compact_list(dat)
class(dat) <- c("check_model", "see_check_model")
Expand All @@ -357,7 +358,7 @@ check_model.model_fit <- function(x,

# compile plots for checks of generalized linear models ------------------------

.check_assumptions_glm <- function(model, model_info, verbose = TRUE) {
.check_assumptions_glm <- function(model, model_info, verbose = TRUE, ...) {
dat <- list()

dat$VIF <- .diag_vif(model, verbose = verbose)
Expand All @@ -371,9 +372,9 @@ check_model.model_fit <- function(x,
threshold <- NULL
}
dat$INFLUENTIAL <- .influential_obs(model, threshold = threshold)
dat$PP_CHECK <- .safe(check_predictions(model))
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, ...)
}
if (isTRUE(model_info$is_count)) {
dat$OVERDISPERSION <- .diag_overdispersion(model)
Expand Down
14 changes: 10 additions & 4 deletions man/binned_residuals.Rd

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

3 changes: 2 additions & 1 deletion man/check_model.Rd

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

0 comments on commit 7ad5946

Please sign in to comment.