Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 24, 2023
1 parent 3b08f69 commit 66da885
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
24 changes: 20 additions & 4 deletions R/binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ binned_residuals <- function(model,
term = NULL,
n_bins = NULL,
show_dots = NULL,
residuals = c("gaussian", "exact", "boot"),
ci = 0.95,
ci_type = c("gaussian", "exact", "boot"),
iterations = 1000,
...) {

residuals <- match.arg(residuals)
ci_type <- match.arg(ci_type)
fv <- stats::fitted(model)
mf <- insight::get_data(model, verbose = FALSE)

Expand Down Expand Up @@ -104,10 +105,10 @@ binned_residuals <- function(model,
n <- length(items)
sdev <- stats::sd(y[items], na.rm = TRUE)

r <- switch(residuals,
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,
boot = Hmisc::smean.cl.boot(y[items], conf.int = ci)[c("Lower", "Upper")]
boot = .boot_binned_ci(y[items], ci, iterations)
)
names(r) <- c("CI_low", "CI_high")

Expand Down Expand Up @@ -142,6 +143,21 @@ binned_residuals <- function(model,
}


# utilities ---------------------------

.boot_binned_ci <- function(x, ci = 0.95, iterations = 1000) {
x <- x[!is.na(x)]
n <- length(x)
out <- vector("numeric", iterations)
for (i in seq_len(iterations)) {
out[i] <- sum(x[sample.int(n, n, replace = TRUE)])
}
out <- out / n

quant <- stats::quantile(out, c((1 - ci) / 2, (1 + ci) / 2))
c(CI_low = quant[1L], CI_high = quant[2L])
}


# methods -----------------------------

Expand Down
11 changes: 10 additions & 1 deletion man/binned_residuals.Rd

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

0 comments on commit 66da885

Please sign in to comment.