Skip to content

Commit

Permalink
Improve and add plots for (simulated) residuals
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 20, 2024
1 parent bd5b6e1 commit e1e8aa3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 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.6
Version: 0.8.2.7
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Major changes

- New `plot()` method for simulated residuals (implemented in the _performance_ package).
- New `plot()` method for simulated residuals (implemented in the _performance_
package).

- `plot()` for `check_model()` was revised and now includes more accurate Q-Q plots for non-Gaussian models.
- `plot()` for `check_model()` was revised and now includes more accurate Q-Q
plots for non-Gaussian models.

## Minor Changes

Expand Down
24 changes: 23 additions & 1 deletion R/plot.check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ plot.see_check_normality <- function(x,
res_ <- abs(stats::rstandard(model, type = "deviance"))
fitted_ <- stats::qnorm((stats::ppoints(length(res_)) + 1) / 2)[order(order(res_))]
dat <- stats::na.omit(data.frame(x = fitted_, y = res_))
} else if (inherits(model, "performance_simres")) {
return(plot.see_performance_simres(
model,
size_line = size_line,
size_point = size_point,
alpha = alpha,
dot_alpha = dot_alpha,
colors = colors,
detrend = detrend,
transform = stats::qnorm,
...
))
} else if (is.numeric(model)) {
res_ <- sort(model[!is.infinite(model)])
dat <- stats::na.omit(data.frame(y = res_))
} else {
res_ <- sort(stats::rstudent(model), na.last = NA)
dat <- stats::na.omit(data.frame(y = res_))
Expand All @@ -97,7 +112,14 @@ plot.see_check_normality <- function(x,
model_class = class(model)[1]
)
} else if (type == "density") {
r <- suppressMessages(stats::residuals(model))
if (inherits(model, "performance_simres")) {
r <- stats::residuals(model, quantile_function = stats::qnorm)
r <- r[!is.infinite(r)]
} else if (is.numeric(model)) {
r <- model[!is.infinite(model) & !is.na(model)]
} else {
r <- suppressMessages(stats::residuals(model))
}
dat <- as.data.frame(bayestestR::estimate_density(r))
dat$curve <- stats::dnorm(
seq(min(dat$x), max(dat$x), length.out = nrow(dat)),
Expand Down
2 changes: 1 addition & 1 deletion R/plot.check_predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ plot.see_performance_pp_check <- function(x,
color = .data$key
),
position = ggplot2::position_nudge(x = 0.2),
size = size_line,
size = size_point,
linewidth = size_line,
stroke = 0,
shape = 16
Expand Down
29 changes: 19 additions & 10 deletions R/plot.performance_simres.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ plot.see_performance_simres <- function(x,
transform = NULL,
style = theme_lucid,
...) {
dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)

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

Expand All @@ -50,10 +48,21 @@ plot.see_performance_simres <- function(x,
x <- attributes(x)$data
}

dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
dp_band <- list(min = 0, max = 1)
dfun <- "unif"

# prepare arguments, based on transformation
if (is.null(transform)) {
res <- stats::residuals(x)
} else {
res <- stats::residuals(x, quantileFunction = transform)
res <- res[!is.infinite(res)]
if (identical(transform, stats::qnorm)) {
dp <- list(mean = 0, sd = 1)
dp_band <- list(mean = 0, sd = 1)
dfun <- "norm"
}
}

# base plot information
Expand All @@ -66,20 +75,20 @@ plot.see_performance_simres <- function(x,
if (requireNamespace("qqplotr", quietly = TRUE)) {
qq_stuff <- list(
qqplotr::stat_qq_band(
distribution = "unif",
dparams = list(min = 0, max = 1),
distribution = dfun,
dparams = dp_band,
alpha = alpha,
detrend = detrend
),
qqplotr::stat_qq_line(
distribution = "unif",
distribution = dfun,
dparams = dp,
size = size_line,
colour = colors[1],
detrend = detrend
),
qqplotr::stat_qq_point(
distribution = "unif",
distribution = dfun,
dparams = dp,
size = size_point,
alpha = dot_alpha,
Expand All @@ -98,7 +107,7 @@ plot.see_performance_simres <- function(x,
ggplot2::geom_qq(
shape = 16,
stroke = 0,
distribution = stats::qunif,
distribution = dfun,
dparams = dp,
size = size_point,
colour = colors[2]
Expand All @@ -107,7 +116,7 @@ plot.see_performance_simres <- function(x,
linewidth = size_line,
colour = colors[1],
na.rm = TRUE,
distribution = stats::qunif,
distribution = dfun,
dparams = dp
)
)
Expand All @@ -117,9 +126,9 @@ plot.see_performance_simres <- function(x,
gg_init +
qq_stuff +
ggplot2::labs(
title = "Uniformity of Residuals",
title = ifelse(is.null(transform), "Uniformity of Residuals", "Residuals Check"),
subtitle = "Dots should fall along the line",
x = "Standard Uniform Distribution Quantiles",
x = ifelse(is.null(transform), "Standard Uniform Distribution Quantiles", "Distribution of Quantiles"),
y = y_lab
) +
style(
Expand Down

0 comments on commit e1e8aa3

Please sign in to comment.