diff --git a/R/binned_residuals.R b/R/binned_residuals.R index 286323968..402f68788 100644 --- a/R/binned_residuals.R +++ b/R/binned_residuals.R @@ -62,7 +62,9 @@ #' } #' #' @export -binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL, ...) { +binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL, resids = c("gaussian", "exact", "boot"), level = 0.95, ...) { + + resids <- match.arg(resids) fv <- stats::fitted(model) mf <- insight::get_data(model, verbose = FALSE) @@ -78,7 +80,9 @@ binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL show_dots <- is.null(n) || n <= 1e5 } - y <- .recode_to_zero(insight::get_response(model, verbose = FALSE)) - fv + + y0 <- .recode_to_zero(insight::get_response(model, verbose = FALSE)) + y <- y0 - fv if (is.null(n_bins)) n_bins <- round(sqrt(length(pred))) @@ -95,23 +99,34 @@ binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL n <- length(items) sdev <- stats::sd(y[items], na.rm = TRUE) - data.frame( + ci_fun <- function() { + r <- switch(resids, + gaussian = qnorm(c((1-level)/2, (1+level)/2), + mean = ybar, sd = sdev/sqrt(n)), + exact = stats:::binom.test(sum(y0[items]), n)$conf.int - fv, + boot = Hmisc::smean.cl.boot(y[items], conf.int = level)[c("Lower", "Upper")] + ) + names(r) <- c("CI_low", "CI_high") + r + } + + d0 <- data.frame( xbar = xbar, ybar = ybar, n = n, x.lo = model.range[1], x.hi = model.range[2], - se = stats::qnorm(0.975) * sdev / sqrt(n), + se = stats::qnorm((1+level)/2) * sdev / sqrt(n), ci_range = sdev / sqrt(n) ) + cbind(d0, rbind(ci_fun())) + })) d <- do.call(rbind, d) d <- d[stats::complete.cases(d), ] # CIs - d$CI_low <- d$ybar - stats::qnorm(0.975) * d$ci_range - d$CI_high <- d$ybar + stats::qnorm(0.975) * d$ci_range gr <- abs(d$ybar) > abs(d$se) d$group <- "yes"