Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 25, 2022
1 parent 36d3dc7 commit a7802be
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ S3method(performance_mse,negbinmfx)
S3method(performance_mse,poissonirr)
S3method(performance_mse,poissonmfx)
S3method(performance_mse,probitmfx)
S3method(plot,binned_residuals)
S3method(plot,check_collinearity)
S3method(plot,check_distribution)
S3method(plot,check_distribution_numeric)
Expand Down
18 changes: 4 additions & 14 deletions R/binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
#' of residuals along the x-axis is a signal to consider taking the logarithm
#' of the predictor (cf. Gelman and Hill 2007, pp. 97-98).
#'
#' @note Since `binned_residuals()` returns a data frame, the default
#' action for the result is *printing*. However, the `print()`-method for
#' `binned_residuals()` actually creates a plot. For further
#' modifications of the plot, use `print()` and add ggplot-layers to the
#' return values, e.g. `print(binned_residuals(model)) +
#' see::scale_color_pizza()`.
#' @note `binned_residuals()` returns a data frame, however, the `print()`
#' method only returns a short summary of the result. The data frame itself
#' is used for plotting. The `plot()` method, in turn, creates a ggplot-object.
#'
#' @references
#' Gelman, A., & Hill, J. (2007). Data analysis using regression and
Expand Down Expand Up @@ -103,19 +100,12 @@ binned_residuals <- function(model, term = NULL, n_bins = NULL, ...) {

resid_ok <- sum(d$group == "yes") / length(d$group)

if (resid_ok < .8) {
insight::print_color(sprintf("Warning: Probably bad model fit. Only about %g%% of the residuals are inside the error bounds.\n", round(100 * resid_ok)), "red")
} else if (resid_ok < .95) {
insight::print_color(sprintf("Warning: About %g%% of the residuals are inside the error bounds (~95%% or higher would be good).\n", round(100 * resid_ok)), "yellow")
} else {
insight::print_color(sprintf("Ok: About %g%% of the residuals are inside the error bounds.\n", round(100 * resid_ok)), "green")
}

add.args <- lapply(match.call(expand.dots = FALSE)$`...`, function(x) x)
size <- if ("size" %in% names(add.args)) add.args[["size"]] else 2
color <- if ("color" %in% names(add.args)) add.args[["color"]] else c("#d11141", "#00aedb")

class(d) <- c("binned_residuals", "see_binned_residuals", class(d))
attr(d, "resid_ok") <- resid_ok
attr(d, "resp_var") <- insight::find_response(model)
attr(d, "term") <- term
attr(d, "geom_size") <- size
Expand Down
7 changes: 5 additions & 2 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
#' plots are helpful to check model assumptions, they do not necessarily
#' indicate so-called "lack of fit", e.g. missed non-linear relationships or
#' interactions. Thus, it is always recommended to also look at
#' [effect
#' plots, including partial residuals](https://strengejacke.github.io/ggeffects/articles/introduction_partial_residuals.html).
#' [effect plots, including partial residuals](https://strengejacke.github.io/ggeffects/articles/introduction_partial_residuals.html).
#'
#' @section Residuals for (Generalized) Linear Models:
#' Plots that check the normality of residuals (QQ-plot) or the homogeneity of
Expand Down Expand Up @@ -126,6 +125,7 @@ check_model.default <- function(x,
attr(ca, "detrend") <- detrend
attr(ca, "colors") <- colors
attr(ca, "theme") <- theme
attr(ca, "model_info") <- minfo
ca
}

Expand Down Expand Up @@ -243,6 +243,9 @@ check_model.model_fit <- function(x,
threshold <- NULL
}
dat$INFLUENTIAL <- .influential_obs(model, threshold = threshold)
if (isTRUE(model_info$is_binomial)) {
dat$BINNED_RESID <- binned_residuals(model)
}

dat <- datawizard::compact_list(dat)
class(dat) <- c("check_model", "see_check_model")
Expand Down
18 changes: 17 additions & 1 deletion R/print-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,29 @@ print.icc_decomposed <- function(x, digits = 2, ...) {

#' @export
print.binned_residuals <- function(x, ...) {
insight::check_if_installed("see", "to plot binned residuals")
resid_ok <- attributes(x)$resid_ok

if (!is.null(resid_ok)) {
if (resid_ok < .8) {
insight::print_color(sprintf("Warning: Probably bad model fit. Only about %g%% of the residuals are inside the error bounds.\n", round(100 * resid_ok)), "red")
} else if (resid_ok < .95) {
insight::print_color(sprintf("Warning: About %g%% of the residuals are inside the error bounds (~95%% or higher would be good).\n", round(100 * resid_ok)), "yellow")
} else {
insight::print_color(sprintf("Ok: About %g%% of the residuals are inside the error bounds.\n", round(100 * resid_ok)), "green")
}
}
}


#' @export
plot.binned_residuals <- function(x, ...) {
insight::check_if_installed("see", "to plot binned residuals")
NextMethod()
}




#' @export
print.performance_hosmer <- function(x, ...) {
insight::print_color("# Hosmer-Lemeshow Goodness-of-Fit Test\n\n", "blue")
Expand Down
8 changes: 3 additions & 5 deletions man/binned_residuals.Rd

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

2 changes: 1 addition & 1 deletion man/check_distribution.Rd

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

0 comments on commit a7802be

Please sign in to comment.