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

Draft plotting simulated residuals #329

Merged
merged 13 commits into from
Mar 16, 2024
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: see
Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2'
Version: 0.8.2.4
Version: 0.8.2.5
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -75,6 +75,7 @@ Imports:
Suggests:
brms,
curl,
DHARMa,
emmeans,
factoextra,
ggdist,
Expand Down Expand Up @@ -118,3 +119,4 @@ Config/Needs/website:
r-lib/pkgdown,
easystats/easystatstemplate
Config/rcmdcheck/ignore-inconsequential-notes: true
Remotes: easystats/performance#643
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
5 changes: 5 additions & 0 deletions R/data_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
obj_name <- attr(x, "object_name", exact = TRUE)
dat <- NULL

# for simulated residuals, we save all necessary information in the object
if (inherits(x, "performance_simres")) {
return(x$fittedModel)

Check warning on line 153 in R/data_plot.R

View check run for this annotation

Codecov / codecov/patch

R/data_plot.R#L153

Added line #L153 was not covered by tests
}

if (!is.null(obj_name)) {
# first try, parent frame
dat <- tryCatch(get(obj_name, envir = parent.frame()), error = function(e) NULL)
Expand Down
40 changes: 27 additions & 13 deletions R/plot.check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#'
#' @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 12 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_model.R,line=12,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.

Check warning on line 12 in R/plot.check_model.R

View workflow job for this annotation

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

file=R/plot.check_model.R,line=12,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
#'
#' @examplesIf require("performance") && require("patchwork")
#' model <- lm(qsec ~ drat + wt, data = mtcars)
#' plot(check_model(model))
#'
#' @export
plot.see_check_model <- function(x,

Check warning on line 19 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_model.R,line=19,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 79 to at most 40.

Check warning on line 19 in R/plot.check_model.R

View workflow job for this annotation

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

file=R/plot.check_model.R,line=19,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 79 to at most 40.
style = theme_lucid,
colors = NULL,
type = c("density", "discrete_dots", "discrete_interval", "discrete_both"),
Expand All @@ -41,7 +41,7 @@
plot_type <- attr(x, "type")
model_class <- attr(x, "model_class")

if (missing(type) && !is.null(plot_type) && plot_type %in% c("density", "discrete_dots", "discrete_interval", "discrete_both")) {

Check warning on line 44 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_model.R,line=44,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.

Check warning on line 44 in R/plot.check_model.R

View workflow job for this annotation

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

file=R/plot.check_model.R,line=44,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.
type <- plot_type
} else {
type <- match.arg(type)
Expand Down Expand Up @@ -165,19 +165,33 @@
}

if ("QQ" %in% names(x) && !is.null(x$QQ) && any(c("qq", "all") %in% check)) {
p$QQ <- .plot_diag_qq(
x$QQ,
size_point,
size_line,
alpha_level = alpha_level,
detrend = detrend,
theme_style = style,
colors = colors,
dot_alpha_level = dot_alpha_level,
show_dots = TRUE, # qq-plots w/o dots makes no sense
model_info = model_info,
model_class = model_class
)
if (inherits(x$QQ, "performance_simres")) {
p$QQ <- plot(
x$QQ,
size_line = size_line,
size_point = size_point,
alpha = alpha_level,
dot_alpha = dot_alpha_level,
colors = colors,
detrend = detrend,
style = style

Check warning on line 177 in R/plot.check_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.check_model.R#L168-L177

Added lines #L168 - L177 were not covered by tests

)
} else {
p$QQ <- .plot_diag_qq(
x$QQ,
size_point,
size_line,
alpha_level = alpha_level,
detrend = detrend,
theme_style = style,
colors = colors,
dot_alpha_level = dot_alpha_level,
show_dots = TRUE, # qq-plots w/o dots makes no sense
model_info = model_info,
model_class = model_class

Check warning on line 192 in R/plot.check_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.check_model.R#L181-L192

Added lines #L181 - L192 were not covered by tests
)
}
}

if ("NORM" %in% names(x) && !is.null(x$NORM) && any(c("normality", "all") %in% check)) {
Expand Down
44 changes: 22 additions & 22 deletions R/plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#' result
#' plot(result)
#' @export
plot.see_parameters_model <- function(x,

Check warning on line 39 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=39,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 127 to at most 40.
show_intercept = FALSE,
size_point = 0.8,
size_text = NA,
Expand All @@ -60,7 +60,7 @@

# Clean up column names
if (!any(grepl("Coefficient", colnames(x), fixed = TRUE))) {
colnames(x)[which.min(match(colnames(x), c("Median", "Mean", "Map", "MAP", model_attributes$coefficient_name)))] <- "Coefficient"

Check warning on line 63 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=63,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 133 characters.
}

if (!any(grepl("Parameter", colnames(x), fixed = TRUE))) {
Expand Down Expand Up @@ -130,7 +130,7 @@
if (!is.null(cleaned_parameters)) {
x <- merge(x, cleaned_parameters, sort = FALSE)
x$Parameter <- x$Cleaned_Parameter
if (all(x$Group == "")) {

Check warning on line 133 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=133,col=15,[nzchar_linter] Use !nzchar(x) instead of x == "". Note that unlike nzchar(), EQ coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.
x$Group <- NULL
} else {
x <- x[!startsWith(x$Group, "SD/Cor"), , drop = FALSE]
Expand All @@ -149,7 +149,7 @@
x <- .fix_facet_names(x)

if (is_bayesian && "Group" %in% colnames(x)) {
x$Effects[x$Group != ""] <- paste0(x$Effects[x$Group != ""], " (", x$Group[x$Group != ""], ")")

Check warning on line 152 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=152,col=15,[nzchar_linter] Use nzchar(x) instead of x != "". Note that unlike nzchar(), NE coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.

Check warning on line 152 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=152,col=50,[nzchar_linter] Use nzchar(x) instead of x != "". Note that unlike nzchar(), NE coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.

Check warning on line 152 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=152,col=80,[nzchar_linter] Use nzchar(x) instead of x != "". Note that unlike nzchar(), NE coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.
}

# remember components
Expand All @@ -165,7 +165,7 @@
is_meta_bma <- !is.null(mc) && any(mc %in% c("meta_random", "meta_fixed", "meta_bma"))

# minor fixes for Bayesian models
if (!is.null(mc) && !is.null(cp) && any(mc %in% c("stanreg", "stanmvreg", "brmsfit")) && length(cp) == length(x$Parameter)) {

Check warning on line 168 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=168,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
x$Parameter <- cp
}

Expand Down Expand Up @@ -595,28 +595,28 @@

.meta_measure <- function(meta_measure) {
switch(meta_measure,
"MD" = "Raw Mean Difference",
"SMDH" = ,
"SMD" = "Standardized Mean Difference",
"ROM" = "Log transformed Ratio of Means",
"D2ORL" = ,
"D2ORN" = "Transformed Standardized Mean Difference",
"UCOR" = ,
"COR" = "Raw Correlation Coefficient",
"ZCOR" = "Z transformed Correlation Coefficient",
"PHI" = "Phi Coefficient",
"RR" = "Log Risk Ratio",
"OR" = "Log Odds Ratio",
"RD" = "Risk Difference",
"AS" = "Root transformed Risk Difference",
"PETO" = "Peto's Log Odds Ratio",
"PBIT" = "Standardized Mean Difference (Probit-transformed)",
"OR2DL" = ,
"OR2DN" = "Standardized Mean Difference (Odds Ratio-transformed)",
"IRR" = "Log Incidence Rate Ratio",
"IRD" = "Incidence Rate Difference",
"IRSD" = "Square Root transformed Incidence Rate Difference",
"GEN" = "Generic Estimate",
MD = "Raw Mean Difference",

Check warning on line 598 in R/plot.parameters_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.parameters_model.R#L598

Added line #L598 was not covered by tests
SMDH = ,
SMD = "Standardized Mean Difference",
ROM = "Log transformed Ratio of Means",

Check warning on line 601 in R/plot.parameters_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.parameters_model.R#L600-L601

Added lines #L600 - L601 were not covered by tests
D2ORL = ,
D2ORN = "Transformed Standardized Mean Difference",

Check warning on line 603 in R/plot.parameters_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.parameters_model.R#L603

Added line #L603 was not covered by tests
UCOR = ,
COR = "Raw Correlation Coefficient",
ZCOR = "Z transformed Correlation Coefficient",
PHI = "Phi Coefficient",
RR = "Log Risk Ratio",
OR = "Log Odds Ratio",
RD = "Risk Difference",
AS = "Root transformed Risk Difference",
PETO = "Peto's Log Odds Ratio",
PBIT = "Standardized Mean Difference (Probit-transformed)",

Check warning on line 613 in R/plot.parameters_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.parameters_model.R#L605-L613

Added lines #L605 - L613 were not covered by tests
OR2DL = ,
OR2DN = "Standardized Mean Difference (Odds Ratio-transformed)",
IRR = "Log Incidence Rate Ratio",
IRD = "Incidence Rate Difference",
IRSD = "Square Root transformed Incidence Rate Difference",
GEN = "Generic Estimate",

Check warning on line 619 in R/plot.parameters_model.R

View check run for this annotation

Codecov / codecov/patch

R/plot.parameters_model.R#L615-L619

Added lines #L615 - L619 were not covered by tests
"Estimate"
)
}
133 changes: 133 additions & 0 deletions R/plot.performance_simres.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#' Plot method for check model for (non-)normality of residuals
#'
#' The `plot()` method for the `performance::check_residuals()` resp.
#' `performance::simulate_residuals()` function.
#'
#' @param transform Function to transform the residuals. If `NULL` (default),
#' no transformation is applied and uniformly distributed residuals are expected.
#' See argument `quantileFuntion` in `?DHARMa:::residuals.DHARMa` for more details.
#'
#' @inheritParams plot.see_check_normality
#' @inheritParams plot.see_check_model
#'
#' @return A ggplot2-object.
#'
#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html).
#'
#' @examplesIf insight::check_if_installed("performance", "0.10.9.7") && require("glmmTMB") && require("qqplotr") && require("DHARMa")
#' 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,
transform = NULL,
style = theme_lucid,
...) {
dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L43

Added line #L43 was not covered by tests

# need DHARMa to be installed
insight::check_if_installed("DHARMa")

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L46

Added line #L46 was not covered by tests

# extract data, if from check_residuals
if (inherits(x, "see_check_residuals")) {
x <- attributes(x)$data

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L49-L50

Added lines #L49 - L50 were not covered by tests
}

if (is.null(transform)) {
res <- stats::residuals(x)

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L53-L54

Added lines #L53 - L54 were not covered by tests
} else {
res <- stats::residuals(x, quantileFunction = transform)

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L56

Added line #L56 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L60-L62

Added lines #L60 - L62 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 72 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L66-L72

Added lines #L66 - L72 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 79 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L74-L79

Added lines #L74 - L79 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 87 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L81-L87

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

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L90-L91

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

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L93

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

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L96-L104

Added lines #L96 - L104 were not covered by tests
),
ggplot2::geom_qq_line(
linewidth = size_line,
colour = colors[1],
na.rm = TRUE,
distribution = stats::qunif,
dparams = dp

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L106-L111

Added lines #L106 - L111 were not covered by tests
)
)
y_lab <- "Sample Quantiles"

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L114

Added line #L114 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 123 in R/plot.performance_simres.R

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L117-L123

Added lines #L117 - L123 were not covered by tests
) +
style(
base_size = 10,
plot.title.space = 3,
axis.title.space = 5

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

View check run for this annotation

Codecov / codecov/patch

R/plot.performance_simres.R#L125-L128

Added lines #L125 - L128 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.

Loading
Loading