|
8 | 8 | #' @param x A model object.
|
9 | 9 | #' @param iterations Number of simulations to run.
|
10 | 10 | #' @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. |
11 | 18 | #'
|
12 | 19 | #' @return Simulated residuals, which can be further processed with
|
13 | 20 | #' [`check_residuals()`]. The returned object is of class `DHARMa` and
|
|
50 | 57 | #' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
|
51 | 58 | #' simulate_residuals(m)
|
52 | 59 | #'
|
| 60 | +#' # extract residuals |
| 61 | +#' head(residuals(simulate_residuals(m))) |
| 62 | +#' |
53 | 63 | #' @export
|
54 | 64 | simulate_residuals <- function(x, iterations = 250, ...) {
|
55 | 65 | insight::check_if_installed("DHARMa")
|
@@ -93,6 +103,24 @@ plot.performance_simres <- function(x, ...) {
|
93 | 103 | }
|
94 | 104 |
|
95 | 105 |
|
| 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 | + |
96 | 124 | # helper functions ---------------------
|
97 | 125 |
|
98 | 126 | .simres_statistics <- function(x, statistic_fun, alternative = "two.sided") {
|
|
0 commit comments