From 038dcddf85feab176f3414249bc39d63bdc29ee4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 24 Oct 2023 23:15:24 +0200 Subject: [PATCH] some fixes --- R/binned_residuals.R | 26 +++++++++++++++++++++----- man/binned_residuals.Rd | 12 ++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/R/binned_residuals.R b/R/binned_residuals.R index 3210a228c..77b3bae78 100644 --- a/R/binned_residuals.R +++ b/R/binned_residuals.R @@ -11,6 +11,13 @@ #' @param n_bins Numeric, the number of bins to divide the data. If #' `n_bins = NULL`, the square root of the number of observations is #' 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"`. +#' @param residuals Character, the type of residuals to calculate. Can be +#' `"response"` (default), `"pearson"` or `"deviance"`. +#' @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 #' to `FALSE` for models with many observations, if generating the plot is too #' time-consuming. By default, `show_dots = NULL`. In this case `binned_residuals()` @@ -68,15 +75,18 @@ binned_residuals <- function(model, show_dots = NULL, ci = 0.95, ci_type = c("gaussian", "exact", "boot"), + residuals = c("response", "pearson", "deviance"), iterations = 1000, ...) { - + # match arguments ci_type <- match.arg(ci_type) - fv <- stats::fitted(model) + residuals <- match.arg(residuals) + + fitted_values <- stats::fitted(model) mf <- insight::get_data(model, verbose = FALSE) if (is.null(term)) { - pred <- fv + pred <- fitted_values } else { pred <- mf[[term]] } @@ -88,7 +98,13 @@ binned_residuals <- function(model, } y0 <- .recode_to_zero(insight::get_response(model, verbose = FALSE)) - y <- y0 - fv + + # calculate residuals + y <- switch(residuals, + response = y0 - fitted_values, + pearson = .safe((y0 - fitted_values) / sqrt(fitted_values * (1 - fitted_values))), + deviance = .safe(stats::residuals(model, type = "deviance")) + ) if (is.null(n_bins)) n_bins <- round(sqrt(length(pred))) @@ -107,7 +123,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 = stats:::binom.test(sum(y0[items]), n)$conf.int - fv, + exact = stats:::binom.test(sum(y0[items]), n)$conf.int, boot = .boot_binned_ci(y[items], ci, iterations) ) names(r) <- c("CI_low", "CI_high") diff --git a/man/binned_residuals.Rd b/man/binned_residuals.Rd index f0362c091..b913ec53a 100644 --- a/man/binned_residuals.Rd +++ b/man/binned_residuals.Rd @@ -11,6 +11,7 @@ binned_residuals( show_dots = NULL, ci = 0.95, ci_type = c("gaussian", "exact", "boot"), + residuals = c("response", "pearson", "deviance"), iterations = 1000, ... ) @@ -33,6 +34,17 @@ time-consuming. By default, \code{show_dots = NULL}. In this case \code{binned_r tries to guess whether performance will be poor due to a very large model and thus automatically shows or hides dots.} +\item{ci}{Numeric, the confidence level for the error bounds.} + +\item{ci_type}{Character, the type of error bounds to calculate. Can be +\code{"gaussian"} (default), \code{"exact"} or \code{"boot"}.} + +\item{residuals}{Character, the type of residuals to calculate. Can be +\code{"response"} (default), \code{"pearson"} or \code{"deviance"}.} + +\item{iterations}{Integer, the number of iterations to use for the +bootstrap method. Only used if \code{ci_type = "boot"}.} + \item{...}{Currently not used.} } \value{