Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving binned_residuals() #640

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions R/binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {

Check warning on line 65 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=65,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 143 characters.

Check warning on line 65 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=65,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 143 characters.

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

Expand All @@ -78,7 +80,9 @@
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)))

Expand All @@ -95,23 +99,34 @@
n <- length(items)
sdev <- stats::sd(y[items], na.rm = TRUE)

data.frame(
ci_fun <- function() {
r <- switch(resids,

Check warning on line 103 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=103,col=8,[indentation_linter] Indentation should be 6 spaces but is 8 spaces.
gaussian = qnorm(c((1-level)/2, (1+level)/2),

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=104,col=37,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=104,col=44,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=104,col=50,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=104,col=57,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=104,col=15,[indentation_linter] Indentation should be 8 spaces but is 15 spaces.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=104,col=37,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=104,col=44,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=104,col=50,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 104 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=104,col=57,[infix_spaces_linter] Put spaces around all infix operators.
mean = ybar, sd = sdev/sqrt(n)),

Check warning on line 105 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=105,col=55,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 105 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=105,col=55,[infix_spaces_linter] Put spaces around all infix operators.
exact = stats:::binom.test(sum(y0[items]), n)$conf.int - fv,

Check warning on line 106 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=106,col=15,[indentation_linter] Indentation should be 8 spaces but is 15 spaces.
boot = Hmisc::smean.cl.boot(y[items], conf.int = level)[c("Lower", "Upper")]
)

Check warning on line 108 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/binned_residuals.R,line=108,col=15,[indentation_linter] Indentation should be 6 spaces but is 15 spaces.
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),

Check warning on line 119 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=119,col=27,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 119 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=119,col=34,[infix_spaces_linter] Put spaces around all infix operators.
ci_range = sdev / sqrt(n)
)
cbind(d0, rbind(ci_fun()))

Check warning on line 123 in R/binned_residuals.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/binned_residuals.R,line=123,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
}))

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"
Expand Down
Loading