diff --git a/NAMESPACE b/NAMESPACE index 94cd39cc6..744e6ea4c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/plot.binned_residuals.R b/R/plot.binned_residuals.R new file mode 100644 index 000000000..663ede26d --- /dev/null +++ b/R/plot.binned_residuals.R @@ -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 +} diff --git a/R/plot.check_model.R b/R/plot.check_model.R index a87e41d5e..ab5feb316 100644 --- a/R/plot.check_model.R +++ b/R/plot.check_model.R @@ -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)) @@ -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, diff --git a/R/print.binned_residuals.R b/R/print.binned_residuals.R deleted file mode 100644 index 96c26d2ac..000000000 --- a/R/print.binned_residuals.R +++ /dev/null @@ -1,69 +0,0 @@ -#' @importFrom ggplot2 .data -#' @export -print.see_binned_residuals <- function(x, ...) { - orig_x <- x - 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 <- ggplot(data = x, aes(x = .data$xbar)) + - geom_abline(slope = 0, intercept = 0, colour = "grey80") - - if (!is.null(term)) { - p <- p + - stat_smooth( - aes(y = .data$ybar), - method = "loess", - se = FALSE, - formula = y ~ x, - colour = "#00b159", - size = .6 - ) - } - - p <- p + - geom_ribbon(aes(ymin = -Inf, ymax = .data$se.lo), alpha = .1, fill = "grey70") + - geom_ribbon(aes(ymin = .data$se, ymax = Inf), alpha = .1, fill = "grey70") + - geom_line(aes(y = .data$se), colour = "grey70") + - geom_line(aes(y = .data$se.lo), colour = "grey70") + - theme_bw() + - scale_color_manual(values = geom_color) + - labs( - y = "Average residual", - x = xtitle, - colour = ltitle - ) - - if (is.null(term)) { - p <- p + scale_x_continuous(labels = .percents) - } - - if (is.null(ltitle)) { - p <- p + geom_point(aes(y = .data$ybar), size = geom_size) - } else { - p <- p + geom_point(aes(y = .data$ybar, colour = .data$group), size = geom_size) - } - - suppressWarnings(graphics::plot(p)) - invisible(orig_x) -}