Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRAN Release 0.11.0 #700

Merged
merged 27 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.10.9
Date: 2024-02-17 07:56:08 UTC
SHA: 051016febd197937ad083266b630c871fa9e1623
Version: 0.11.0
Date: 2024-03-22 21:30:58 UTC
SHA: 051b9bb2b7721c632ce145f85c55aa55c8eebf90
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: performance
Title: Assessment of Regression Models Performance
Version: 0.10.9.10
Version: 0.11.0
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -154,4 +154,3 @@ Config/Needs/website:
r-lib/pkgdown,
easystats/easystatstemplate
Config/rcmdcheck/ignore-inconsequential-notes: true
Remotes: easystats/see
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ S3method(check_normality,htest)
S3method(check_normality,lmerModLmerTest)
S3method(check_normality,merMod)
S3method(check_normality,numeric)
S3method(check_normality,performance_simres)
S3method(check_outliers,BFBayesFactor)
S3method(check_outliers,DHARMa)
S3method(check_outliers,character)
Expand Down Expand Up @@ -517,6 +518,7 @@ S3method(r2_tjur,nestedLogit)
S3method(residuals,BFBayesFactor)
S3method(residuals,check_normality_numeric)
S3method(residuals,iv_robust)
S3method(residuals,performance_simres)
S3method(rstudent,check_normality_numeric)
S3method(test_bf,ListModels)
S3method(test_bf,default)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# performance 0.10.10
# performance 0.11.0

## New supported models

Expand Down
13 changes: 7 additions & 6 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#'
#' @details For Bayesian models from packages **rstanarm** or **brms**,
#' models will be "converted" to their frequentist counterpart, using
#' [`bayestestR::bayesian_as_frequentist`](https://easystats.github.io/bayestestR/reference/convert_bayesian_as_frequentist.html).

Check warning on line 62 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=62,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.
#' A more advanced model-check for Bayesian models will be implemented at a
#' later stage.
#'
Expand Down Expand Up @@ -87,7 +87,7 @@
#' plots are helpful to check model assumptions, they do not necessarily indicate
#' so-called "lack of fit", e.g. missed non-linear relationships or interactions.
#' Thus, it is always recommended to also look at
#' [effect plots, including partial residuals](https://strengejacke.github.io/ggeffects/articles/introduction_partial_residuals.html).

Check warning on line 90 in R/check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/check_model.R,line=90,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 134 characters.
#'
#' @section Homogeneity of Variance:
#' This plot checks the assumption of equal variance (homoscedasticity). The
Expand Down Expand Up @@ -144,12 +144,13 @@
#' inside the error bounds. See [`binned_residuals()`] for further details.
#'
#' @section Residuals for (Generalized) Linear Models:
#' Plots that check the normality of residuals (Q-Q plot) or the homogeneity of
#' variance use standardized Pearson's residuals for generalized linear models,
#' and standardized residuals for linear models. The plots for the normality of
#' residuals (with overlayed normal curve) and for the linearity assumption use
#' the default residuals for `lm` and `glm` (which are deviance residuals for
#' `glm`).
#' Plots that check the homogeneity of variance use standardized Pearson's
#' residuals for generalized linear models, and standardized residuals for
#' linear models. The plots for the normality of residuals (with overlayed
#' normal curve) and for the linearity assumption use the default residuals
#' for `lm` and `glm` (which are deviance residuals for `glm`). The Q-Q plots
#' use simulated residuals (see [`simulate_residuals()`]) for non-Gaussian
#' models and standardized residuals for linear models.
#'
#' @section Troubleshooting:
#' For models with many observations, or for more complex models in general,
Expand Down
2 changes: 1 addition & 1 deletion R/check_model_diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
d <- data.frame(Predicted = predicted)

# residuals based on simulated residuals - but we want normally distributed residuals
d$Residuals <- stats::residuals(simres, quantileFunction = stats::qnorm, ...)
d$Residuals <- stats::residuals(simres, quantile_function = stats::qnorm, ...)

Check warning on line 304 in R/check_model_diagnostics.R

View check run for this annotation

Codecov / codecov/patch

R/check_model_diagnostics.R#L304

Added line #L304 was not covered by tests
d$Res2 <- d$Residuals^2
d$StdRes <- insight::get_residuals(model, type = "pearson")

Expand Down
17 changes: 17 additions & 0 deletions R/check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ check_normality.glm <- function(x, ...) {
invisible(out)
}

# simulated residuals ----------

#' @export
check_normality.performance_simres <- function(x, ...) {
# check for normality of residuals
res <- stats::residuals(x, quantile_function = stats::qnorm)
p.val <- .check_normality(res[!is.infinite(res) & !is.na(res)], x)

attr(p.val, "data") <- x
attr(p.val, "object_name") <- insight::safe_deparse_symbol(substitute(x))
attr(p.val, "effects") <- "fixed"
class(p.val) <- unique(c("check_normality", "see_check_normality", class(p.val)))

p.val
}


# numeric -------------------

#' @export
Expand Down
13 changes: 2 additions & 11 deletions R/check_overdispersion.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,10 @@
#' multilevel/hierarchical models. Cambridge; New York: Cambridge University
#' Press.
#'
#' @examplesIf getRversion() >= "4.0.0" && require("glmmTMB", quietly = TRUE)
#'
#' library(glmmTMB)
#' data(Salamanders)
#' @examplesIf getRversion() >= "4.0.0" && require("glmmTMB")
#' data(Salamanders, package = "glmmTMB")
#' m <- glm(count ~ spp + mined, family = poisson, data = Salamanders)
#' check_overdispersion(m)
#'
#' m <- glmmTMB(
#' count ~ mined + spp + (1 | site),
#' family = poisson,
#' data = Salamanders
#' )
#' check_overdispersion(m)
#' @export
check_overdispersion <- function(x, ...) {
UseMethod("check_overdispersion")
Expand Down
3 changes: 2 additions & 1 deletion R/check_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#'
#' @inheritSection simulate_residuals Tests based on simulated residuals
#'
#' @seealso [`simulate_residuals()`] and [`check_predictions()`].
#' @seealso [`simulate_residuals()`], [`check_zeroinflation()`],
#' [`check_overdispersion()`] and [`check_predictions()`].
#'
#' @return The p-value of the test statistics.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/check_zeroinflation.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#'
#' @section Tests based on simulated residuals:
#' For certain models, resp. model from certain families, tests are based on
#' simulated residuals (see [`simulated_residual()`]). These are usually more
#' simulated residuals (see [`simulate_residuals()`]). These are usually more
#' accurate for testing such models than the traditionally used Pearson residuals.
#' However, when simulating from more complex models, such as mixed models or
#' models with zero-inflation, there are several important considerations.
Expand Down
53 changes: 48 additions & 5 deletions R/simulate_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
#' @param x A model object.
#' @param iterations Number of simulations to run.
#' @param ... Arguments passed on to [`DHARMa::simulateResiduals()`].
#' @param object A `performance_simres` object, as returned by `simulate_residuals()`.
#' @param quantile_function A function to apply to the residuals. If `NULL`, the
#' residuals are returned as is. If not `NULL`, the residuals are passed to this
#' function. This is useful for returning normally distributed residuals, for
#' example: `residuals(x, quantile_function = qnorm)`.
#' @param outlier_values A vector of length 2, specifying the values to replace
#' `-Inf` and `Inf` with, respectively.
#'
#' @return Simulated residuals, which can be further processed with
#' [`check_residuals()`]. The returned object is of class `DHARMa` and
#' `performance_simres`.
#'
#' @seealso [`check_residuals()`] and [`check_predictions()`].
#' @seealso [`check_residuals()`], [`check_zeroinflation()`],
#' [`check_overdispersion()`] and [`check_predictions()`].
#'
#' @details This function is a small wrapper around [`DHARMa::simulateResiduals()`].
#' It basically only sets `plot = FALSE` and adds an additional class attribute
#' (`"performance_sim_res"`), which allows using the DHARMa object in own plotting
#' functions in the **see** package. See also `vignette("DHARMa")`. There is a
#' functions from the **see** package. See also `vignette("DHARMa")`. There is a
#' `plot()` method to visualize the distribution of the residuals.
#'
#' @section Tests based on simulated residuals:
Expand Down Expand Up @@ -50,6 +58,9 @@
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
#' simulate_residuals(m)
#'
#' # extract residuals
#' head(residuals(simulate_residuals(m)))
#'
#' @export
simulate_residuals <- function(x, iterations = 250, ...) {
insight::check_if_installed("DHARMa")
Expand Down Expand Up @@ -78,9 +89,10 @@ print.performance_simres <- function(x, ...) {
# DHARMa's method.
msg <- paste0(
"Simulated residuals from a model of class `", class(x$fittedModel)[1],
"` based on ", x$nSim, " simulations. Use `check_residuals()` to check ",
"uniformity of residuals. It is recommended to refer to `?DHARMa::simulateResiudals`",
" and `vignette(\"DHARMa\")` for more information about different settings",
"` based on ", x$nSim, " simulations. Use `check_residuals()` to check",
" uniformity of residuals or `residuals()` to extract simulated residuals.",
" It is recommended to refer to `?DHARMa::simulateResiudals` and",
" `vignette(\"DHARMa\")` for more information about different settings",
" in particular situations or for particular models.\n"
)
cat(insight::format_message(msg))
Expand All @@ -93,6 +105,37 @@ plot.performance_simres <- function(x, ...) {
}


# methods --------------------------

#' @rdname simulate_residuals
#' @export
residuals.performance_simres <- function(object, quantile_function = NULL, outlier_values = NULL, ...) {
# check for DHARMa argument names
dots <- list(...)
if (!is.null(dots$quantileFunction)) {
quantile_function <- dots$quantileFunction
}
if (!is.null(dots$outlierValues)) {
outlier_values <- dots$outlierValues
}

if (is.null(quantile_function)) {
res <- object$scaledResiduals
} else {
res <- quantile_function(object$scaledResiduals)
if (!is.null(outlier_values)) {
# check for correct length of outlier_values
if (length(outlier_values) != 2) {
insight::format_error("`outlier_values` must be a vector of length 2.")
}
res[res == -Inf] <- outlier_values[1]
res[res == Inf] <- outlier_values[2]
}
}
res
}


# helper functions ---------------------

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