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
14
21
# ' `performance_simres`.
15
22
# '
16
- # ' @seealso [`check_residuals()`] and [`check_predictions()`].
23
+ # ' @seealso [`check_residuals()`], [`check_zeroinflation()`],
24
+ # ' [`check_overdispersion()`] and [`check_predictions()`].
17
25
# '
18
26
# ' @details This function is a small wrapper around [`DHARMa::simulateResiduals()`].
19
27
# ' It basically only sets `plot = FALSE` and adds an additional class attribute
20
28
# ' (`"performance_sim_res"`), which allows using the DHARMa object in own plotting
21
- # ' functions in the **see** package. See also `vignette("DHARMa")`. There is a
29
+ # ' functions from the **see** package. See also `vignette("DHARMa")`. There is a
22
30
# ' `plot()` method to visualize the distribution of the residuals.
23
31
# '
24
32
# ' @section Tests based on simulated residuals:
50
58
# ' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
51
59
# ' simulate_residuals(m)
52
60
# '
61
+ # ' # extract residuals
62
+ # ' head(residuals(simulate_residuals(m)))
63
+ # '
53
64
# ' @export
54
65
simulate_residuals <- function (x , iterations = 250 , ... ) {
55
66
insight :: check_if_installed(" DHARMa" )
@@ -78,9 +89,10 @@ print.performance_simres <- function(x, ...) {
78
89
# DHARMa's method.
79
90
msg <- paste0(
80
91
" Simulated residuals from a model of class `" , class(x $ fittedModel )[1 ],
81
- " ` based on " , x $ nSim , " simulations. Use `check_residuals()` to check " ,
82
- " uniformity of residuals. It is recommended to refer to `?DHARMa::simulateResiudals`" ,
83
- " and `vignette(\" DHARMa\" )` for more information about different settings" ,
92
+ " ` based on " , x $ nSim , " simulations. Use `check_residuals()` to check" ,
93
+ " uniformity of residuals or `residuals()` to extract simulated residuals." ,
94
+ " It is recommended to refer to `?DHARMa::simulateResiudals` and" ,
95
+ " `vignette(\" DHARMa\" )` for more information about different settings" ,
84
96
" in particular situations or for particular models.\n "
85
97
)
86
98
cat(insight :: format_message(msg ))
@@ -93,6 +105,37 @@ plot.performance_simres <- function(x, ...) {
93
105
}
94
106
95
107
108
+ # methods --------------------------
109
+
110
+ # ' @rdname simulate_residuals
111
+ # ' @export
112
+ residuals.performance_simres <- function (object , quantile_function = NULL , outlier_values = NULL , ... ) {
113
+ # check for DHARMa argument names
114
+ dots <- list (... )
115
+ if (! is.null(dots $ quantileFunction )) {
116
+ quantile_function <- dots $ quantileFunction
117
+ }
118
+ if (! is.null(dots $ outlierValues )) {
119
+ outlier_values <- dots $ outlierValues
120
+ }
121
+
122
+ if (is.null(quantile_function )) {
123
+ res <- object $ scaledResiduals
124
+ } else {
125
+ res <- quantile_function(object $ scaledResiduals )
126
+ if (! is.null(outlier_values )) {
127
+ # check for correct length of outlier_values
128
+ if (length(outlier_values ) != 2 ) {
129
+ insight :: format_error(" `outlier_values` must be a vector of length 2." )
130
+ }
131
+ res [res == - Inf ] <- outlier_values [1 ]
132
+ res [res == Inf ] <- outlier_values [2 ]
133
+ }
134
+ }
135
+ res
136
+ }
137
+
138
+
96
139
# helper functions ---------------------
97
140
98
141
.simres_statistics <- function (x , statistic_fun , alternative = " two.sided" ) {
0 commit comments