Skip to content

Commit

Permalink
https://github.com/easystats/performance/issues/376
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 25, 2022
1 parent 898cf86 commit 84f5c76
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 70 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ S3method(data_plot,see_compare_parameters)
S3method(plot,see_bayesfactor_models)
S3method(plot,see_bayesfactor_parameters)
S3method(plot,see_bayesfactor_savagedickey)
S3method(plot,see_binned_residuals)
S3method(plot,see_check_collinearity)
S3method(plot,see_check_distribution)
S3method(plot,see_check_distribution_numeric)
Expand Down Expand Up @@ -65,7 +66,6 @@ S3method(plot,see_si)
S3method(plot,see_visualisation_recipe)
S3method(plot,see_visualisation_recipes)
S3method(print,data_plot)
S3method(print,see_binned_residuals)
S3method(print,see_check_model)
S3method(print,see_performance_pp_check)
export(add_plot_attributes)
Expand Down
83 changes: 83 additions & 0 deletions R/plot.binned_residuals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' @importFrom ggplot2 .data
#' @export
plot.see_binned_residuals <- function(x, theme_style = theme_lucid, ...) {
x$se.lo <- -x$se
if (length(unique(x$group)) > 1) {
ltitle <- "Within error bounds"
} else {
ltitle <- NULL
}

# set defaults

term <- attr(x, "term", exact = TRUE)
geom_color <- attr(x, "geom_color", exact = TRUE)
geom_size <- attr(x, "geom_size", exact = TRUE)


if (is.null(term)) {
xtitle <- sprintf("Estimated Probability of %s", attr(x, "resp_var", exact = TRUE))
} else {
xtitle <- term
}

if (is.null(geom_color)) geom_color <- c("#d11141", "#00aedb")
if (is.null(geom_size)) geom_size <- 2


p <- ggplot2::ggplot(data = x, aes(x = .data$xbar)) +
ggplot2::geom_abline(slope = 0, intercept = 0, colour = "grey80")

if (!is.null(term)) {
p <- p +
ggplot2::stat_smooth(
ggplot2::aes(y = .data$ybar),
method = "loess",
se = FALSE,
formula = y ~ x,
colour = "#00b159",
size = .6
)
}

p <- p +
ggplot2::geom_ribbon(ggplot2::aes(ymin = -Inf, ymax = .data$se.lo), alpha = .1, fill = "grey70") +
ggplot2::geom_ribbon(ggplot2::aes(ymin = .data$se, ymax = Inf), alpha = .1, fill = "grey70") +
ggplot2::geom_line(ggplot2::aes(y = .data$se), colour = "grey70") +
ggplot2::geom_line(ggplot2::aes(y = .data$se.lo), colour = "grey70") +
ggplot2::scale_color_manual(values = geom_color) +
ggplot2::labs(
x = xtitle,
y = "Average residual",
colour = ltitle,
title = "Binned Residuals",
subtitle = "Points should be within error bounds"
)

if (is.null(term)) {
p <- p + ggplot2::scale_x_continuous(labels = .percents)
}

if (is.null(ltitle)) {
p <- p + ggplot2::geom_point(ggplot2::aes(y = .data$ybar), size = geom_size)
} else {
p <- p + ggplot2::geom_point(ggplot2::aes(y = .data$ybar, colour = .data$group), size = geom_size)
}

p <- p + theme_style(
base_size = 10,
plot.title.space = 3,
axis.title.space = 5
)

dots <- list(...)
if (isTRUE(dots[["adjust_legend"]])) {
p <- p + ggplot2::theme(
legend.position = "bottom",
legend.margin = ggplot2::margin(0, 0, 0, 0),
legend.box.margin = ggplot2::margin(-5, -5, -5, -5)
)
}

p
}
10 changes: 10 additions & 0 deletions R/plot.check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ plot.see_check_model <- function(x,
alpha_level <- attr(x, "alpha")
dot_alpha_level <- attr(x, "dot_alpha")
detrend <- attr(x, "detrend")
model_info <- attr(x, "model_info")

if (missing(style) && !is.null(attr(x, "theme"))) {
theme_style <- unlist(strsplit(attr(x, "theme"), "::", fixed = TRUE))
Expand Down Expand Up @@ -53,6 +54,15 @@ plot.see_check_model <- function(x,
)
}

if ("BINNED_RESID" %in% names(x) && !is.null(x$BINNED_RESID) && any(c("binned_residuals", "all") %in% check)) {
x$HOMOGENEITY <- NULL
p$BINNED_RESID <- plot.see_binned_residuals(
x$BINNED_RESID,
adjust_legend = TRUE,
theme_style = style
)
}

if ("HOMOGENEITY" %in% names(x) && !is.null(x$HOMOGENEITY) && any(c("homogeneity", "all") %in% check)) {
p$HOMOGENEITY <- .plot_diag_homogeneity(
x$HOMOGENEITY,
Expand Down
69 changes: 0 additions & 69 deletions R/print.binned_residuals.R

This file was deleted.

0 comments on commit 84f5c76

Please sign in to comment.