Skip to content

Commit 7ad5946

Browse files
committed
docs, minor fixes
1 parent aaa8eb6 commit 7ad5946

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

R/binned_residuals.R

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
#' taken.
1414
#' @param ci Numeric, the confidence level for the error bounds.
1515
#' @param ci_type Character, the type of error bounds to calculate. Can be
16-
#' `"gaussian"` (default), `"exact"` or `"boot"`.
16+
#' `"exact"` (default), `"gaussian"` or `"boot"`. `"exact"` calculates the
17+
#' error bounds based on the exact binomial distribution, using [`binom.test()`].
18+
#' `"gaussian"` uses the Gaussian approximation, while `"boot"` uses a simple
19+
#' bootstrap method, where confidence intervals are calculated based on the
20+
#' quantiles of the bootstrap distribution.
1721
#' @param residuals Character, the type of residuals to calculate. Can be
18-
#' `"response"` (default), `"pearson"` or `"deviance"`.
22+
#' `"deviance"` (default), `"pearson"` or `"response"`. It is recommended to
23+
#' use `"response"` only for those models where other residuals are not
24+
#' available.
1925
#' @param iterations Integer, the number of iterations to use for the
2026
#' bootstrap method. Only used if `ci_type = "boot"`.
2127
#' @param show_dots Logical, if `TRUE`, will show data points in the plot. Set
@@ -74,8 +80,8 @@ binned_residuals <- function(model,
7480
n_bins = NULL,
7581
show_dots = NULL,
7682
ci = 0.95,
77-
ci_type = c("gaussian", "exact", "boot"),
78-
residuals = c("response", "pearson", "deviance"),
83+
ci_type = c("exact", "gaussian", "boot"),
84+
residuals = c("deviance", "pearson", "response"),
7985
iterations = 1000,
8086
...) {
8187
# match arguments
@@ -106,6 +112,11 @@ binned_residuals <- function(model,
106112
deviance = .safe(stats::residuals(model, type = "deviance"))
107113
)
108114

115+
# make sure we really have residuals
116+
if (is.null(y)) {
117+
insight::format_error("Could not calculate residuals. Try using `residuals = \"response\"`.")
118+
}
119+
109120
if (is.null(n_bins)) n_bins <- round(sqrt(length(pred)))
110121

111122
breaks.index <- floor(length(pred) * (1:(n_bins - 1)) / n_bins)
@@ -124,7 +135,7 @@ binned_residuals <- function(model,
124135
r <- switch(ci_type,
125136
gaussian = stats::qnorm(c((1 - ci) / 2, (1 + ci) / 2), mean = ybar, sd = sdev / sqrt(n)),
126137
exact = {
127-
out <- stats:::binom.test(sum(y0[items]), n)$conf.int
138+
out <- stats::binom.test(sum(y0[items]), n)$conf.int
128139
out <- out - (min(out) - ybar) - (diff(out) / 2)
129140
out
130141
},
@@ -138,8 +149,7 @@ binned_residuals <- function(model,
138149
n = n,
139150
x.lo = model.range[1],
140151
x.hi = model.range[2],
141-
se = stats::qnorm((1 + ci) / 2) * sdev / sqrt(n),
142-
ci_range = sdev / sqrt(n)
152+
se = stats::qnorm((1 + ci) / 2) * sdev / sqrt(n)
143153
)
144154
cbind(d0, rbind(r))
145155
}))

R/check_model.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
#' tries to guess whether performance will be poor due to a very large model
3636
#' and thus automatically shows or hides dots.
3737
#' @param verbose If `FALSE` (default), suppress most warning messages.
38-
#' @param ... Currently not used.
38+
#' @param ... Arguments passed down to the individual check functions, especially
39+
#' to `check_predictions()` and `binned_residuals()`.
3940
#' @inheritParams check_predictions
4041
#'
4142
#' @return The data frame that is used for plotting.
@@ -185,11 +186,11 @@ check_model.default <- function(x,
185186
ca <- tryCatch(
186187
{
187188
if (minfo$is_bayesian) {
188-
suppressWarnings(.check_assumptions_stan(x))
189+
suppressWarnings(.check_assumptions_stan(x, ...))
189190
} else if (minfo$is_linear) {
190-
suppressWarnings(.check_assumptions_linear(x, minfo, verbose))
191+
suppressWarnings(.check_assumptions_linear(x, minfo, verbose, ...))
191192
} else {
192-
suppressWarnings(.check_assumptions_glm(x, minfo, verbose))
193+
suppressWarnings(.check_assumptions_glm(x, minfo, verbose, ...))
193194
}
194195
},
195196
error = function(e) {
@@ -346,7 +347,7 @@ check_model.model_fit <- function(x,
346347
threshold <- NULL
347348
}
348349
dat$INFLUENTIAL <- .influential_obs(model, threshold = threshold)
349-
dat$PP_CHECK <- .safe(check_predictions(model))
350+
dat$PP_CHECK <- .safe(check_predictions(model, ...))
350351

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

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

360-
.check_assumptions_glm <- function(model, model_info, verbose = TRUE) {
361+
.check_assumptions_glm <- function(model, model_info, verbose = TRUE, ...) {
361362
dat <- list()
362363

363364
dat$VIF <- .diag_vif(model, verbose = verbose)
@@ -371,9 +372,9 @@ check_model.model_fit <- function(x,
371372
threshold <- NULL
372373
}
373374
dat$INFLUENTIAL <- .influential_obs(model, threshold = threshold)
374-
dat$PP_CHECK <- .safe(check_predictions(model))
375+
dat$PP_CHECK <- .safe(check_predictions(model, ...))
375376
if (isTRUE(model_info$is_binomial)) {
376-
dat$BINNED_RESID <- binned_residuals(model)
377+
dat$BINNED_RESID <- binned_residuals(model, ...)
377378
}
378379
if (isTRUE(model_info$is_count)) {
379380
dat$OVERDISPERSION <- .diag_overdispersion(model)

man/binned_residuals.Rd

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_model.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)