Skip to content

Commit

Permalink
draft plot for check_residuals
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 15, 2024
1 parent 2bebe13 commit 642fab6
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 75 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ S3method(plot,see_check_model)
S3method(plot,see_check_normality)
S3method(plot,see_check_outliers)
S3method(plot,see_check_overdisp)
S3method(plot,see_check_residuals)
S3method(plot,see_compare_parameters)
S3method(plot,see_compare_performance)
S3method(plot,see_effectsize_table)
Expand All @@ -64,6 +65,7 @@ S3method(plot,see_parameters_sem)
S3method(plot,see_parameters_simulate)
S3method(plot,see_performance_pp_check)
S3method(plot,see_performance_roc)
S3method(plot,see_performance_simres)
S3method(plot,see_point_estimate)
S3method(plot,see_rope)
S3method(plot,see_si)
Expand Down
108 changes: 108 additions & 0 deletions R/plot.performance_simres.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#' Plot method for check model for (non-)normality of residuals
#'
#' The `plot()` method for the `performance::check_residuals()` resp.
#' `performance::simulate_residuals()` function.
#'
#' @inheritParams plot.see_check_normality
#'
#' @return A ggplot2-object.
#'
#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html).

Check warning on line 10 in R/plot.performance_simres.R

View workflow job for this annotation

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

file=R/plot.performance_simres.R,line=10,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
#'
#' @examplesIf require("performance") && require("glmmTMB") && require("qqplotr")
#' data(Salamanders, package = "glmmTMB")
#' model <- glmmTMB::glmmTMB(
#' count ~ mined + spp + (1 | site),
#' family = poisson(),
#' data = Salamanders
#' )
#' simulated_residuals <- performance::simulate_residuals(model)
#' plot(simulated_residuals)
#'
#' # or
#' simulated_residuals <- performance::simulate_residuals(model)
#' result <- performance::check_residuals(simulated_residuals)
#' plot(result)
#'
#' @export
plot.see_performance_simres <- function(x,
size_line = 0.8,
size_point = 1,
alpha = 0.2,
dot_alpha = 0.8,
colors = c("#3aaf85", "#1b6ca8"),
detrend = FALSE,
...) {
dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)

Check warning on line 36 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L36

Added line #L36 was not covered by tests

# base plot information
gg_init <- ggplot2::ggplot(
data.frame(scaled_residuals = stats::residuals(x)),
ggplot2::aes(sample = scaled_residuals)

Check warning on line 41 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L39-L41

Added lines #L39 - L41 were not covered by tests
)

# when we have package qqplotr, we can add confidence bands
if (requireNamespace("qqplotr", quietly = TRUE)) {
qq_stuff <- list(
qqplotr::stat_qq_band(
distribution = "unif",
dparams = list(min = 0, max = 1),
alpha = alpha,
detrend = detrend

Check warning on line 51 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L45-L51

Added lines #L45 - L51 were not covered by tests
),
qqplotr::stat_qq_line(
distribution = "unif",
dparams = dp,
size = size_line,
colour = colors[1],
detrend = detrend

Check warning on line 58 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L53-L58

Added lines #L53 - L58 were not covered by tests
),
qqplotr::stat_qq_point(
distribution = "unif",
dparams = dp,
size = size_point,
alpha = dot_alpha,
colour = colors[2],
detrend = detrend

Check warning on line 66 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L60-L66

Added lines #L60 - L66 were not covered by tests
)
)
if (detrend) {
y_lab <- "Sample Quantile Deviations"

Check warning on line 70 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L69-L70

Added lines #L69 - L70 were not covered by tests
} else {
y_lab <- "Sample Quantiles"

Check warning on line 72 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L72

Added line #L72 was not covered by tests
}
} else {
insight::format_alert("For confidence bands, please install `qqplotr`.")
qq_stuff <- list(
ggplot2::geom_point(
shape = 16,
stroke = 0,
size = size_point,
colour = colors[2]

Check warning on line 81 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L75-L81

Added lines #L75 - L81 were not covered by tests
),
ggplot2::geom_qq_line(
linewidth = size_line,
colour = colors[1],
na.rm = TRUE

Check warning on line 86 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L83-L86

Added lines #L83 - L86 were not covered by tests
)
)
y_lab <- "Sample Quantiles"

Check warning on line 89 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L89

Added line #L89 was not covered by tests
}

gg_init +
qq_stuff +
ggplot2::labs(
title = "Uniformity of Residuals",
subtitle = "Dots should fall along the line",
x = "Standard Uniform Distribution Quantiles",
y = y_lab

Check warning on line 98 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L92-L98

Added lines #L92 - L98 were not covered by tests
) +
theme_lucid(
base_size = 10,
plot.title.space = 3,
axis.title.space = 5

Check warning on line 103 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L100-L103

Added lines #L100 - L103 were not covered by tests
)
}

#' @export
plot.see_check_residuals <- plot.see_performance_simres
29 changes: 4 additions & 25 deletions man/geom_violindot.Rd

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

63 changes: 13 additions & 50 deletions man/geom_violinhalf.Rd

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

63 changes: 63 additions & 0 deletions man/plot.see_performance_simres.Rd

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

0 comments on commit 642fab6

Please sign in to comment.