Skip to content

Commit 80b5b2f

Browse files
CRAN Release 0.11.0 (#700)
Co-authored-by: Indrajeet Patil <[email protected]>
1 parent 01eff88 commit 80b5b2f

30 files changed

+521
-65
lines changed

CRAN-SUBMISSION

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Version: 0.10.9
2-
Date: 2024-02-17 07:56:08 UTC
3-
SHA: 051016febd197937ad083266b630c871fa9e1623
1+
Version: 0.11.0
2+
Date: 2024-03-22 21:30:58 UTC
3+
SHA: 051b9bb2b7721c632ce145f85c55aa55c8eebf90

DESCRIPTION

+1-2
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.10.9.10
4+
Version: 0.11.0
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",
@@ -154,4 +154,3 @@ Config/Needs/website:
154154
r-lib/pkgdown,
155155
easystats/easystatstemplate
156156
Config/rcmdcheck/ignore-inconsequential-notes: true
157-
Remotes: easystats/see

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ S3method(check_normality,htest)
7272
S3method(check_normality,lmerModLmerTest)
7373
S3method(check_normality,merMod)
7474
S3method(check_normality,numeric)
75+
S3method(check_normality,performance_simres)
7576
S3method(check_outliers,BFBayesFactor)
7677
S3method(check_outliers,DHARMa)
7778
S3method(check_outliers,character)
@@ -517,6 +518,7 @@ S3method(r2_tjur,nestedLogit)
517518
S3method(residuals,BFBayesFactor)
518519
S3method(residuals,check_normality_numeric)
519520
S3method(residuals,iv_robust)
521+
S3method(residuals,performance_simres)
520522
S3method(rstudent,check_normality_numeric)
521523
S3method(test_bf,ListModels)
522524
S3method(test_bf,default)

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# performance 0.10.10
1+
# performance 0.11.0
22

33
## New supported models
44

R/check_model.R

+7-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@
144144
#' inside the error bounds. See [`binned_residuals()`] for further details.
145145
#'
146146
#' @section Residuals for (Generalized) Linear Models:
147-
#' Plots that check the normality of residuals (Q-Q plot) or the homogeneity of
148-
#' variance use standardized Pearson's residuals for generalized linear models,
149-
#' and standardized residuals for linear models. The plots for the normality of
150-
#' residuals (with overlayed normal curve) and for the linearity assumption use
151-
#' the default residuals for `lm` and `glm` (which are deviance residuals for
152-
#' `glm`).
147+
#' Plots that check the homogeneity of variance use standardized Pearson's
148+
#' residuals for generalized linear models, and standardized residuals for
149+
#' linear models. The plots for the normality of residuals (with overlayed
150+
#' normal curve) and for the linearity assumption use the default residuals
151+
#' for `lm` and `glm` (which are deviance residuals for `glm`). The Q-Q plots
152+
#' use simulated residuals (see [`simulate_residuals()`]) for non-Gaussian
153+
#' models and standardized residuals for linear models.
153154
#'
154155
#' @section Troubleshooting:
155156
#' For models with many observations, or for more complex models in general,

R/check_model_diagnostics.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
d <- data.frame(Predicted = predicted)
302302

303303
# residuals based on simulated residuals - but we want normally distributed residuals
304-
d$Residuals <- stats::residuals(simres, quantileFunction = stats::qnorm, ...)
304+
d$Residuals <- stats::residuals(simres, quantile_function = stats::qnorm, ...)
305305
d$Res2 <- d$Residuals^2
306306
d$StdRes <- insight::get_residuals(model, type = "pearson")
307307

R/check_normality.R

+17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ check_normality.glm <- function(x, ...) {
9292
invisible(out)
9393
}
9494

95+
# simulated residuals ----------
96+
97+
#' @export
98+
check_normality.performance_simres <- function(x, ...) {
99+
# check for normality of residuals
100+
res <- stats::residuals(x, quantile_function = stats::qnorm)
101+
p.val <- .check_normality(res[!is.infinite(res) & !is.na(res)], x)
102+
103+
attr(p.val, "data") <- x
104+
attr(p.val, "object_name") <- insight::safe_deparse_symbol(substitute(x))
105+
attr(p.val, "effects") <- "fixed"
106+
class(p.val) <- unique(c("check_normality", "see_check_normality", class(p.val)))
107+
108+
p.val
109+
}
110+
111+
95112
# numeric -------------------
96113

97114
#' @export

R/check_overdispersion.R

+2-11
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,10 @@
6262
#' multilevel/hierarchical models. Cambridge; New York: Cambridge University
6363
#' Press.
6464
#'
65-
#' @examplesIf getRversion() >= "4.0.0" && require("glmmTMB", quietly = TRUE)
66-
#'
67-
#' library(glmmTMB)
68-
#' data(Salamanders)
65+
#' @examplesIf getRversion() >= "4.0.0" && require("glmmTMB")
66+
#' data(Salamanders, package = "glmmTMB")
6967
#' m <- glm(count ~ spp + mined, family = poisson, data = Salamanders)
7068
#' check_overdispersion(m)
71-
#'
72-
#' m <- glmmTMB(
73-
#' count ~ mined + spp + (1 | site),
74-
#' family = poisson,
75-
#' data = Salamanders
76-
#' )
77-
#' check_overdispersion(m)
7869
#' @export
7970
check_overdispersion <- function(x, ...) {
8071
UseMethod("check_overdispersion")

R/check_residuals.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#'
2020
#' @inheritSection simulate_residuals Tests based on simulated residuals
2121
#'
22-
#' @seealso [`simulate_residuals()`] and [`check_predictions()`].
22+
#' @seealso [`simulate_residuals()`], [`check_zeroinflation()`],
23+
#' [`check_overdispersion()`] and [`check_predictions()`].
2324
#'
2425
#' @return The p-value of the test statistics.
2526
#'

R/check_zeroinflation.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#'
3131
#' @section Tests based on simulated residuals:
3232
#' For certain models, resp. model from certain families, tests are based on
33-
#' simulated residuals (see [`simulated_residual()`]). These are usually more
33+
#' simulated residuals (see [`simulate_residuals()`]). These are usually more
3434
#' accurate for testing such models than the traditionally used Pearson residuals.
3535
#' However, when simulating from more complex models, such as mixed models or
3636
#' models with zero-inflation, there are several important considerations.

R/simulate_residuals.R

+48-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@
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
1421
#' `performance_simres`.
1522
#'
16-
#' @seealso [`check_residuals()`] and [`check_predictions()`].
23+
#' @seealso [`check_residuals()`], [`check_zeroinflation()`],
24+
#' [`check_overdispersion()`] and [`check_predictions()`].
1725
#'
1826
#' @details This function is a small wrapper around [`DHARMa::simulateResiduals()`].
1927
#' It basically only sets `plot = FALSE` and adds an additional class attribute
2028
#' (`"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
2230
#' `plot()` method to visualize the distribution of the residuals.
2331
#'
2432
#' @section Tests based on simulated residuals:
@@ -50,6 +58,9 @@
5058
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
5159
#' simulate_residuals(m)
5260
#'
61+
#' # extract residuals
62+
#' head(residuals(simulate_residuals(m)))
63+
#'
5364
#' @export
5465
simulate_residuals <- function(x, iterations = 250, ...) {
5566
insight::check_if_installed("DHARMa")
@@ -78,9 +89,10 @@ print.performance_simres <- function(x, ...) {
7889
# DHARMa's method.
7990
msg <- paste0(
8091
"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",
8496
" in particular situations or for particular models.\n"
8597
)
8698
cat(insight::format_message(msg))
@@ -93,6 +105,37 @@ plot.performance_simres <- function(x, ...) {
93105
}
94106

95107

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+
96139
# helper functions ---------------------
97140

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

0 commit comments

Comments
 (0)