Skip to content

Commit 2fb679a

Browse files
committed
still hacking on binned_residuals
1 parent e9c9b46 commit 2fb679a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

R/binned_residuals.R

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@
6262
#' }
6363
#'
6464
#' @export
65-
binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL, ...) {
65+
binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL, resids = c("gaussian", "exact", "boot"), level = 0.95, ...) {
66+
67+
resids <- match.arg(resids)
6668
fv <- stats::fitted(model)
6769
mf <- insight::get_data(model, verbose = FALSE)
6870

@@ -78,8 +80,9 @@ binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL
7880
show_dots <- is.null(n) || n <= 1e5
7981
}
8082

81-
y <- .recode_to_zero(insight::get_response(model, verbose = FALSE)) - fv
82-
>>>>>>> upstream/main
83+
84+
y0 <- .recode_to_zero(insight::get_response(model, verbose = FALSE))
85+
y <- y0 - fv
8386

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

@@ -96,18 +99,28 @@ binned_residuals <- function(model, term = NULL, n_bins = NULL, show_dots = NULL
9699
n <- length(items)
97100
sdev <- stats::sd(y[items], na.rm = TRUE)
98101

99-
bt <- binom.test(sum(y0[items]), n)
100-
data.frame(
102+
ci_fun <- function() {
103+
r <- switch(resids,
104+
gaussian = qnorm(c((1-level)/2, (1+level)/2),
105+
mean = ybar, sd = sdev/sqrt(n)),
106+
exact = stats:::binom.test(sum(y0[items]), n)$conf.int - fv,
107+
boot = Hmisc::smean.cl.boot(y[items], conf.int = level)[c("Lower", "Upper")]
108+
)
109+
names(r) <- c("CI_low", "CI_high")
110+
r
111+
}
112+
113+
d0 <- data.frame(
101114
xbar = xbar,
102115
ybar = ybar,
103116
n = n,
104117
x.lo = model.range[1],
105118
x.hi = model.range[2],
106-
se = stats::qnorm(0.975) * sdev / sqrt(n),
107-
ci_range = sdev / sqrt(n),
108-
CI_low = bt$conf.int[1] - fv,
109-
CI_high = bt$conf.int[2] - fv
119+
se = stats::qnorm((1+level)/2) * sdev / sqrt(n),
120+
ci_range = sdev / sqrt(n)
110121
)
122+
cbind(d0, rbind(ci_fun()))
123+
111124
}))
112125

113126
d <- do.call(rbind, d)

0 commit comments

Comments
 (0)