Skip to content

Commit be93d1d

Browse files
committed
allow to limit x axis in check_predictions
1 parent eb6cd38 commit be93d1d

6 files changed

Lines changed: 63 additions & 1 deletion

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: performance
33
Title: Assessment of Regression Models Performance
4-
Version: 0.17.0.2
4+
Version: 0.17.0.3
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# performance (devel)
22

3+
## Changes
4+
5+
* `check_overdispersion()` gets a `residual_type` argument, to decide whether
6+
overdispersion tests are based on simulated or "standard" residuals.
7+
8+
* `check_model()` gains a `ppc_range` argument for posterior predictive checks
9+
plot. Use this to zoom in on a specific region of interest, especially if the
10+
response variable has a large range.
11+
12+
* `check_predictions()` gains a `x_limits` argument for plots. Use this to zoom
13+
in on a specific region of interest, especially if the response variable has
14+
a large range.
15+
316
## Bug fixes
417

518
* The overdispersion plot in `check_model()` now uses simulated residuals (based

R/check_model.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
#' @param maximum_dots Limits the number of data points for models with many
5454
#' observations, to reduce the time for rendering the plot. Defaults to a
5555
#' maximum of 2000 data points to render
56+
#' @param ppc_range An integer vector of length two specifying the x-axis limits
57+
#' for the posterior predictive checks plot. Use this to zoom in on a specific
58+
#' region of interest, especially if the response variable has a large range.
5659
#' @param verbose If `FALSE` (default), suppress most warning messages.
5760
#' @param ... Arguments passed down to the individual check functions, especially
5861
#' to `check_predictions()` and `binned_residuals()`.
@@ -248,6 +251,7 @@ check_model.default <- function(
248251
base_size = 10,
249252
alpha = 0.2,
250253
alpha_dot = 0.8,
254+
ppc_range = NULL,
251255
colors = c("#3aaf85", "#1b6ca8", "#cd201f"),
252256
theme = see::theme_lucid(),
253257
verbose = FALSE,
@@ -397,6 +401,7 @@ check_model.default <- function(
397401
attr(assumptions_data, "overdisp_type") <- list(...)$plot_type
398402
attr(assumptions_data, "bandwidth") <- bandwidth
399403
attr(assumptions_data, "type") <- type
404+
attr(assumptions_data, "ppc_range") <- ppc_range
400405
attr(assumptions_data, "maximum_dots") <- maximum_dots
401406
attr(assumptions_data, "model_class") <- class(model)[1]
402407
assumptions_data
@@ -442,6 +447,7 @@ check_model.stanreg <- function(
442447
base_size = 10,
443448
alpha = 0.2,
444449
alpha_dot = 0.8,
450+
ppc_range = NULL,
445451
colors = c("#3aaf85", "#1b6ca8", "#cd201f"),
446452
theme = see::theme_lucid(),
447453
verbose = FALSE,
@@ -475,6 +481,7 @@ check_model.stanreg <- function(
475481
type = type,
476482
residual_type = residual_type,
477483
maximum_dots = maximum_dots,
484+
ppc_range = ppc_range,
478485
verbose = verbose,
479486
...
480487
)
@@ -504,6 +511,7 @@ check_model.model_fit <- function(
504511
base_size = 10,
505512
alpha = 0.2,
506513
alpha_dot = 0.8,
514+
ppc_range = NULL,
507515
colors = c("#3aaf85", "#1b6ca8", "#cd201f"),
508516
theme = see::theme_lucid(),
509517
verbose = FALSE,
@@ -537,6 +545,7 @@ check_model.model_fit <- function(
537545
bandwidth = bandwidth,
538546
type = type,
539547
residual_type = residual_type,
548+
ppc_range = ppc_range,
540549
verbose = verbose,
541550
...
542551
)
@@ -562,6 +571,7 @@ check_model.performance_simres <- function(
562571
base_size = 10,
563572
alpha = 0.2,
564573
alpha_dot = 0.8,
574+
ppc_range = NULL,
565575
colors = c("#3aaf85", "#1b6ca8", "#cd201f"),
566576
theme = see::theme_lucid(),
567577
verbose = FALSE,
@@ -595,6 +605,7 @@ check_model.performance_simres <- function(
595605
bandwidth = bandwidth,
596606
type = type,
597607
residual_type = "simulated",
608+
ppc_range = ppc_range,
598609
verbose = verbose,
599610
...
600611
)

R/check_predictions.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#' `"discrete_dots"`, `"discrete_interval"` or `"discrete_both"` (the `discrete_*`
3838
#' options are appropriate for models with discrete - binary, integer or ordinal
3939
#' etc. - outcomes).
40+
#' @param x_limits An integer vector of length two specifying the x-axis limits
41+
#' for the plot. Use this to zoom in on a specific region of interest,
42+
#' especially if the response variable has a large range.
4043
#' @param verbose Toggle warnings.
4144
#' @param ... Additional arguments passed on to downstream functions. For
4245
#' frequentist models, these are forwarded to `simulate()`; for Bayesian models
@@ -111,6 +114,7 @@ check_predictions.default <- function(
111114
re_formula = NULL,
112115
bandwidth = "nrd",
113116
type = "density",
117+
x_limits = NULL,
114118
verbose = TRUE,
115119
object = NULL,
116120
...
@@ -162,6 +166,7 @@ check_predictions.default <- function(
162166
type = type,
163167
verbose = verbose,
164168
model_info = minfo,
169+
x_limits = x_limits,
165170
...
166171
)
167172
}
@@ -329,6 +334,7 @@ pp_check.lm <- function(
329334
type = "density",
330335
verbose = TRUE,
331336
model_info = NULL,
337+
x_limits = NULL,
332338
...
333339
) {
334340
# we need the formula and the response values to check for matrix responses
@@ -347,13 +353,29 @@ pp_check.lm <- function(
347353
type,
348354
verbose,
349355
model_info,
356+
x_limits,
350357
...
351358
))
352359
}
353360

354361
# else, proceed as usual
355362
out <- .safe(stats::simulate(object, nsim = iterations, re.form = re_formula, ...))
356363

364+
# if it fails, try insight methods
365+
if (is.null(out)) {
366+
out <- .safe({
367+
sims <- insight::get_simulated(
368+
object,
369+
iterations = iterations,
370+
re.form = re_formula,
371+
...
372+
)
373+
# get_simulated returns "iter_" columms, so we rename here
374+
colnames(sims) <- gsub("^iter_(\\d+)", "sim_\\1", colnames(sims))
375+
sims
376+
})
377+
}
378+
357379
# validation check, for mixed models, where re.form = NULL (default) might fail
358380
out <- .check_re_formula(out, object, iterations, re_formula, verbose, ...)
359381

@@ -426,6 +448,8 @@ pp_check.lm <- function(
426448
attr(out, "bandwidth") <- bandwidth
427449
attr(out, "model_info") <- minfo
428450
attr(out, "type") <- type
451+
attr(out, "x_limits") <- x_limits
452+
429453
class(out) <- c("performance_pp_check", "see_performance_pp_check", class(out))
430454
out
431455
}
@@ -440,6 +464,7 @@ pp_check.glm <- function(
440464
type = "density",
441465
verbose = TRUE,
442466
model_info = NULL,
467+
x_limits = NULL,
443468
...
444469
) {
445470
# we need the formula and the response values to check for matrix responses
@@ -458,6 +483,7 @@ pp_check.glm <- function(
458483
type,
459484
verbose,
460485
model_info,
486+
x_limits,
461487
...
462488
))
463489
}
@@ -527,6 +553,8 @@ pp_check.glm <- function(
527553
attr(out, "bandwidth") <- bandwidth
528554
attr(out, "model_info") <- minfo
529555
attr(out, "type") <- type
556+
attr(out, "x_limits") <- x_limits
557+
530558
class(out) <- c(
531559
"performance_pp_check",
532560
"see_performance_pp_check",

man/check_model.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_predictions.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)