Skip to content

Commit ba92a91

Browse files
committed
add residuals method
1 parent 3680678 commit ba92a91

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ S3method(r2_tjur,nestedLogit)
517517
S3method(residuals,BFBayesFactor)
518518
S3method(residuals,check_normality_numeric)
519519
S3method(residuals,iv_robust)
520+
S3method(residuals,performance_simres)
520521
S3method(rstudent,check_normality_numeric)
521522
S3method(test_bf,ListModels)
522523
S3method(test_bf,default)

R/simulate_residuals.R

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
#' @param x A model object.
99
#' @param iterations Number of simulations to run.
1010
#' @param ... Arguments passed on to [`DHARMa::simulateResiduals()`].
11+
#' @param object A `performance_simres` object, as returned by `simulate_residuals()`.
12+
#' @param quantile_function A function to apply to the residuals. If `NULL`, the
13+
#' residuals are returned as is. If not `NULL`, the residuals are passed to this
14+
#' function. This is useful for returning normally distributed residuals, for
15+
#' example: `residuals(x, quantile_function = qnorm)`.
16+
#' @param outlier_values A vector of length 2, specifying the values to replace
17+
#' `-Inf` and `Inf` with, respectively.
1118
#'
1219
#' @return Simulated residuals, which can be further processed with
1320
#' [`check_residuals()`]. The returned object is of class `DHARMa` and
@@ -50,6 +57,9 @@
5057
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
5158
#' simulate_residuals(m)
5259
#'
60+
#' # extract residuals
61+
#' head(residuals(simulate_residuals(m)))
62+
#'
5363
#' @export
5464
simulate_residuals <- function(x, iterations = 250, ...) {
5565
insight::check_if_installed("DHARMa")
@@ -93,6 +103,24 @@ plot.performance_simres <- function(x, ...) {
93103
}
94104

95105

106+
# methods --------------------------
107+
108+
#' @rdname simulate_residuals
109+
#' @export
110+
residuals.performance_simres <- function(object, quantile_function = NULL, outlier_values = NULL, ...) {
111+
if (is.null(quantile_function)) {
112+
res <- object$scaledResiduals
113+
} else {
114+
res <- quantile_function(object$scaledResiduals)
115+
if (!is.null(outlier_values)) {
116+
res[res == -Inf] <- outlier_values[1]
117+
res[res == Inf] <- outlier_values[2]
118+
}
119+
}
120+
res
121+
}
122+
123+
96124
# helper functions ---------------------
97125

98126
.simres_statistics <- function(x, statistic_fun, alternative = "two.sided") {

man/simulate_residuals.Rd

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/simulate_residuals.Rmd

+7-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@ simulated_residuals <- simulate_residuals(model)
6161
simulated_residuals
6262
```
6363

64+
The raw residuals can be extracted using `residuals()`:
65+
66+
```{r}
67+
head(residuals(simulated_residuals))
68+
```
69+
70+
6471
<!-- Aside -->
6572
Note that since this inherits the DHARMa class, all the methods implemented in DHARMa just work, including all the tests:
6673

6774
```{r}
6875
library(DHARMa)
69-
residuals(simulated_residuals)
70-
7176
DHARMa::testUniformity(simulated_residuals, plot = FALSE)
7277
```
7378
<!-- Aside -->

0 commit comments

Comments
 (0)