Skip to content
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
2 changes: 1 addition & 1 deletion 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.13.0.2
Version: 0.13.0.3
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
14 changes: 10 additions & 4 deletions R/binned_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ binned_residuals <- function(model,
n_bins = NULL,
show_dots = NULL,
ci = 0.95,
ci_type = c("exact", "gaussian", "boot"),
residuals = c("deviance", "pearson", "response"),
ci_type = "exact",
residuals = "deviance",
iterations = 1000,
verbose = TRUE,
...) {
ci_type <- match.arg(ci_type)
residuals <- match.arg(residuals)
ci_type <- insight::validate_argument(
ci_type,
c("exact", "gaussian", "boot")
)
residuals <- insight::validate_argument(
residuals,
c("deviance", "pearson", "response")
)

# for non-bernoulli models, `"exact"` doesn't work
if (isFALSE(insight::model_info(model)$is_bernoulli)) {
Expand Down
35 changes: 25 additions & 10 deletions R/check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,55 +305,70 @@
#' @rdname check_collinearity
#' @export
check_collinearity.glmmTMB <- function(x,
component = c("all", "conditional", "count", "zi", "zero_inflated"),
component = "all",
ci = 0.95,
verbose = TRUE,
...) {
component <- match.arg(component)
component <- insight::validate_argument(
component,
c("all", "conditional", "count", "zi", "zero_inflated")
)
.check_collinearity_zi_model(x, component, ci = ci, verbose = verbose)
}


#' @export
check_collinearity.MixMod <- function(x,
component = c("all", "conditional", "count", "zi", "zero_inflated"),
component = "all",
ci = 0.95,
verbose = TRUE,
...) {
component <- match.arg(component)
component <- insight::validate_argument(
component,
c("all", "conditional", "count", "zi", "zero_inflated")

Check warning on line 328 in R/check_collinearity.R

View check run for this annotation

Codecov / codecov/patch

R/check_collinearity.R#L326-L328

Added lines #L326 - L328 were not covered by tests
)
.check_collinearity_zi_model(x, component, ci = ci, verbose = verbose)
}


#' @export
check_collinearity.hurdle <- function(x,
component = c("all", "conditional", "count", "zi", "zero_inflated"),
component = "all",
ci = 0.95,
verbose = verbose,
...) {
component <- match.arg(component)
component <- insight::validate_argument(
component,
c("all", "conditional", "count", "zi", "zero_inflated")
)
.check_collinearity_zi_model(x, component, ci = ci, verbose = verbose)
}


#' @export
check_collinearity.zeroinfl <- function(x,
component = c("all", "conditional", "count", "zi", "zero_inflated"),
component = "all",
ci = 0.95,
verbose = verbose,
...) {
component <- match.arg(component)
component <- insight::validate_argument(
component,
c("all", "conditional", "count", "zi", "zero_inflated")
)
.check_collinearity_zi_model(x, component, ci = ci, verbose = verbose)
}


#' @export
check_collinearity.zerocount <- function(x,
component = c("all", "conditional", "count", "zi", "zero_inflated"),
component = "all",
ci = 0.95,
verbose = verbose,
...) {
component <- match.arg(component)
component <- insight::validate_argument(
component,
c("all", "conditional", "count", "zi", "zero_inflated")

Check warning on line 370 in R/check_collinearity.R

View check run for this annotation

Codecov / codecov/patch

R/check_collinearity.R#L368-L370

Added lines #L368 - L370 were not covered by tests
)
.check_collinearity_zi_model(x, component, ci = ci, verbose = verbose)
}

Expand Down Expand Up @@ -407,7 +422,7 @@
}


.check_collinearity <- function(x, component, ci = 0.95, verbose = TRUE) {

Check warning on line 425 in R/check_collinearity.R

View workflow job for this annotation

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

file=R/check_collinearity.R,line=425,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 44 to at most 40.

Check warning on line 425 in R/check_collinearity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_collinearity.R,line=425,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 44 to at most 40.
v <- .safe(insight::get_varcov(x, component = component, verbose = FALSE))

# sanity check
Expand Down
7 changes: 5 additions & 2 deletions R/check_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,17 @@ check_dag <- function(...,
exposure = NULL,
adjusted = NULL,
latent = NULL,
effect = c("all", "total", "direct"),
effect = "all",
coords = NULL) {
insight::check_if_installed(
c("ggdag", "dagitty"),
reason = "to check correct adjustments for identifying causal effects."
)

effect <- match.arg(effect)
effect <- insight::validate_argument(
effect,
c("all", "total", "direct")
)

# retrieve formulas
formulas <- list(...)
Expand Down
9 changes: 6 additions & 3 deletions R/check_homogeneity.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@
#' result <- check_homogeneity(model)
#' plot(result)
#' @export
check_homogeneity <- function(x, method = c("bartlett", "fligner", "levene", "auto"), ...) {
check_homogeneity <- function(x, method = "bartlett", ...) {
UseMethod("check_homogeneity")
}


# default -------------------------

#' @export
check_homogeneity.default <- function(x, method = c("bartlett", "fligner", "levene", "auto"), ...) {
method <- match.arg(method)
check_homogeneity.default <- function(x, method = "bartlett", ...) {
method <- insight::validate_argument(
method,
c("bartlett", "fligner", "levene", "auto")
)

resp <- insight::find_response(x)
pred <- insight::find_predictors(x, component = "conditional", flatten = TRUE)
Expand Down
4 changes: 2 additions & 2 deletions R/check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ print.check_normality <- function(x, ...) {

#' @rdname check_normality
#' @export
check_normality.merMod <- function(x, effects = c("fixed", "random"), ...) {
check_normality.merMod <- function(x, effects = "fixed", ...) {
# args
effects <- match.arg(effects)
effects <- insight::validate_argument(effects, c("fixed", "random"))
info <- insight::model_info(x)

# valid model?
Expand Down
4 changes: 2 additions & 2 deletions R/check_outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,8 @@ check_outliers.metabin <- check_outliers.metagen
#' @rdname check_outliers
#' @export
check_outliers.performance_simres <- function(x, type = "default", iterations = 100, alternative = "two.sided", ...) {
type <- match.arg(type, c("default", "binomial", "bootstrap"))
alternative <- match.arg(alternative, c("two.sided", "greater", "less"))
type <- insight::validate_argument(type, c("default", "binomial", "bootstrap"))
alternative <- insight::validate_argument(alternative, c("two.sided", "greater", "less"))

insight::check_if_installed("DHARMa")
result <- DHARMa::testOutliers(x, type = type, nBoot = iterations, alternative = alternative, plot = FALSE, ...)
Expand Down
7 changes: 5 additions & 2 deletions R/check_overdispersion.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ check_overdispersion.glmmTMB <- check_overdispersion.merMod

#' @rdname check_overdispersion
#' @export
check_overdispersion.performance_simres <- function(x, alternative = c("two.sided", "less", "greater"), ...) {
alternative <- match.arg(alternative)
check_overdispersion.performance_simres <- function(x, alternative = "two.sided", ...) {
alternative <- insight::validate_argument(
alternative,
c("two.sided", "less", "greater")
)

# check for special arguments - we may pass "object_name" from other methods
dots <- list(...)
Expand Down
10 changes: 8 additions & 2 deletions R/check_predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@
}

# args
type <- match.arg(type, choices = c("density", "discrete_dots", "discrete_interval", "discrete_both"))
type <- insight::validate_argument(
type,
c("density", "discrete_dots", "discrete_interval", "discrete_both")
)

pp_check.lm(
object,
Expand Down Expand Up @@ -161,7 +164,10 @@
}

# args
type <- match.arg(type, choices = c("density", "discrete_dots", "discrete_interval", "discrete_both"))
type <- insight::validate_argument(
type,
c("density", "discrete_dots", "discrete_interval", "discrete_both")

Check warning on line 169 in R/check_predictions.R

View check run for this annotation

Codecov / codecov/patch

R/check_predictions.R#L167-L169

Added lines #L167 - L169 were not covered by tests
)

# convert to type-argument for pp_check
pp_type <- switch(type,
Expand Down
12 changes: 8 additions & 4 deletions R/check_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#' @param x An object returned by [`simulate_residuals()`] or
#' [`DHARMa::simulateResiduals()`].
#' @param alternative A character string specifying the alternative hypothesis.
#' See [`stats::ks.test()`] for details.
#' Can be one of `"two.sided"`, `"less"`, or `"greater"`. See
#' [`stats::ks.test()`] for details.
#' @param ... Passed down to [`stats::ks.test()`].
#'
#' @details Uniformity of residuals is checked using a Kolmogorov-Smirnov test.
Expand Down Expand Up @@ -38,7 +39,7 @@ check_residuals <- function(x, ...) {

#' @rdname check_residuals
#' @export
check_residuals.default <- function(x, alternative = c("two.sided", "less", "greater"), ...) {
check_residuals.default <- function(x, alternative = "two.sided", ...) {
if (insight::is_model(x)) {
check_residuals(simulate_residuals(x, ...), alternative = alternative)
} else {
Expand All @@ -47,8 +48,11 @@ check_residuals.default <- function(x, alternative = c("two.sided", "less", "gre
}

#' @export
check_residuals.performance_simres <- function(x, alternative = c("two.sided", "less", "greater"), ...) {
alternative <- match.arg(alternative)
check_residuals.performance_simres <- function(x, alternative = "two.sided", ...) {
alternative <- insight::validate_argument(
alternative,
c("two.sided", "less", "greater")
)
ts_test <- suppressWarnings(
stats::ks.test(
stats::residuals(x),
Expand Down
8 changes: 6 additions & 2 deletions R/check_zeroinflation.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' between 1 +/- `tolerance` is considered as OK, while a ratio
#' beyond or below this threshold would indicate over- or underfitting.
#' @param alternative A character string specifying the alternative hypothesis.
#' Can be one of `"two.sided"`, `"less"`, or `"greater"`.
#' @param ... Arguments passed down to [`simulate_residuals()`]. This only applies
#' for models with zero-inflation component, or for models of class `glmmTMB`
#' from `nbinom1` or `nbinom2` family.
Expand Down Expand Up @@ -118,9 +119,12 @@ check_zeroinflation.default <- function(x, tolerance = 0.05, ...) {
#' @export
check_zeroinflation.performance_simres <- function(x,
tolerance = 0.1,
alternative = c("two.sided", "less", "greater"),
alternative = "two.sided",
...) {
alternative <- match.arg(alternative)
alternative <- insight::validate_argument(
alternative,
c("two.sided", "less", "greater")
)

# compute test results
result <- .simres_statistics(x, statistic_fun = function(i) sum(i == 0), alternative = alternative)
Expand Down
2 changes: 1 addition & 1 deletion R/compare_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @return A data frame with one row per model and one column per "index" (see
#' `metrics`).
#'
#' @note There is also a [`plot()`-method](https://easystats.github.io/see/articles/performance.html) implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}.

Check warning on line 20 in R/compare_performance.R

View workflow job for this annotation

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

file=R/compare_performance.R,line=20,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 180 characters.
#'
#' @details \subsection{Model Weights}{
#' When information criteria (IC) are requested in `metrics` (i.e., any of `"all"`,
Expand Down Expand Up @@ -105,11 +105,11 @@
}

# iterate over all models, i.e. model-performance for each model
m <- mapply(function(.x, .y) {

Check warning on line 108 in R/compare_performance.R

View workflow job for this annotation

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

file=R/compare_performance.R,line=108,col=8,[undesirable_function_linter] Avoid undesirable function "mapply".
dat <- model_performance(.x, metrics = metrics, estimator = estimator, verbose = FALSE)
model_name <- gsub("\"", "", insight::safe_deparse(.y), fixed = TRUE)
perf_df <- data.frame(Name = model_name, Model = class(.x)[1], dat, stringsAsFactors = FALSE)
attributes(perf_df) <- c(attributes(perf_df), attributes(dat)[!names(attributes(dat)) %in% c("names", "row.names", "class")])

Check warning on line 112 in R/compare_performance.R

View workflow job for this annotation

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

file=R/compare_performance.R,line=112,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 129 characters.
perf_df
}, model_objects, object_names, SIMPLIFY = FALSE)

Expand Down Expand Up @@ -193,7 +193,7 @@
!isTRUE(attributes(model_objects)$same_fixef)) {
insight::format_alert(
"Information criteria (like AIC) are based on REML fits (i.e. `estimator=\"REML\"`).",
"Please note that information criteria are probably not directly comparable and that it is not recommended comparing models with different fixed effects in such cases."

Check warning on line 196 in R/compare_performance.R

View workflow job for this annotation

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

file=R/compare_performance.R,line=196,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 174 characters.
)
}

Expand All @@ -207,12 +207,12 @@

#' @export
print.compare_performance <- function(x, digits = 3, layout = "horizontal", ...) {
layout <- match.arg(layout, choices = c("horizontal", "vertical"))
layout <- insight::validate_argument(layout, c("horizontal", "vertical"))
table_caption <- c("# Comparison of Model Performance Indices", "blue")
formatted_table <- format(x = x, digits = digits, format = "text", ...)

if ("Performance_Score" %in% colnames(formatted_table)) {
footer <- c(sprintf("\nModel `%s` (of class `%s`) performed best with an overall performance score of %s.", formatted_table$Model[1], formatted_table$Type[1], formatted_table$Performance_Score[1]), "yellow")

Check warning on line 215 in R/compare_performance.R

View workflow job for this annotation

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

file=R/compare_performance.R,line=215,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 211 characters.
} else {
footer <- NULL
}
Expand All @@ -224,7 +224,7 @@
colnames(formatted_table)[1] <- "Metric"
}

cat(insight::export_table(x = formatted_table, digits = digits, format = "text", caption = table_caption, footer = footer, ...))

Check warning on line 227 in R/compare_performance.R

View workflow job for this annotation

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

file=R/compare_performance.R,line=227,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 130 characters.
invisible(x)
}

Expand Down
17 changes: 8 additions & 9 deletions R/item_intercor.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#' @title Mean Inter-Item-Correlation
#' @name item_intercor
#'
#' @description Compute various measures of internal consistencies
#' for tests or item-scales of questionnaires.
#' @description Compute various measures of internal consistencies for tests or
#' item-scales of questionnaires.
#'
#' @param x A matrix as returned by the `cor()`-function,
#' or a data frame with items (e.g. from a test or questionnaire).
#' @param method Correlation computation method. May be one of
#' `"pearson"` (default), `"spearman"` or `"kendall"`.
#' You may use initial letter only.
#' @param x A matrix as returned by the `cor()`-function, or a data frame with
#' items (e.g. from a test or questionnaire).
#' @param method Correlation computation method. May be one of `"pearson"`
#' (default), `"spearman"` or `"kendall"`. You may use initial letter only.
#'
#' @return The mean inter-item-correlation value for `x`.
#'
Expand Down Expand Up @@ -36,9 +35,9 @@
#' x <- mtcars[, c("cyl", "gear", "carb", "hp")]
#' item_intercor(x)
#' @export
item_intercor <- function(x, method = c("pearson", "spearman", "kendall")) {
item_intercor <- function(x, method = "pearson") {
# Check parameter
method <- match.arg(method)
method <- insight::validate_argument(method, c("pearson", "spearman", "kendall"))

# Mean-interitem-corelation
if (inherits(x, "matrix")) {
Expand Down
5 changes: 4 additions & 1 deletion R/model_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ performance <- model_performance

#' @export
print.performance_model <- function(x, digits = 3, layout = "horizontal", ...) {
layout <- match.arg(layout, choices = c("horizontal", "vertical"))
layout <- insight::validate_argument(
layout,
c("horizontal", "vertical")
)
formatted_table <- format(x = x, digits = digits, format = "text", ...)

# switch to vertical layout
Expand Down
28 changes: 13 additions & 15 deletions R/performance_accuracy.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@
#' @param verbose Toggle warnings.
#' @inheritParams performance_pcp
#'
#' @return A list with three values: The `Accuracy` of the model
#' predictions, i.e. the proportion of accurately predicted values from the
#' model, its standard error, `SE`, and the `Method` used to compute
#' the accuracy.
#' @return A list with three values: The `Accuracy` of the model predictions,
#' i.e. the proportion of accurately predicted values from the model, its
#' standard error, `SE`, and the `Method` used to compute the accuracy.
#'
#' @details For linear models, the accuracy is the correlation coefficient
#' between the actual and the predicted value of the outcome. For
#' logistic regression models, the accuracy corresponds to the
#' AUC-value, calculated with the `bayestestR::auc()`-function.
#' \cr \cr
#' The accuracy is the mean value of multiple correlation resp.
#' AUC-values, which are either computed with cross-validation
#' or non-parametric bootstrapping (see argument `method`).
#' The standard error is the standard deviation of the computed
#' correlation resp. AUC-values.
#' between the actual and the predicted value of the outcome. For logistic
#' regression models, the accuracy corresponds to the AUC-value, calculated with
#' the [`bayestestR::auc()`]-function.
#'
#' The accuracy is the mean value of multiple correlation resp. AUC-values,
#' which are either computed with cross-validation or non-parametric
#' bootstrapping (see argument `method`). The standard error is the standard
#' deviation of the computed correlation resp. AUC-values.
#'
#' @examples
#' model <- lm(mpg ~ wt + cyl, data = mtcars)
Expand All @@ -38,15 +36,15 @@
#' performance_accuracy(model)
#' @export
performance_accuracy <- function(model,
method = c("cv", "boot"),
method = "cv",
k = 5,
n = 1000,
ci = 0.95,
verbose = TRUE) {
method <- match.arg(method)
method <- insight::validate_argument(method, c("cv", "boot"))

# get formula from model fit
formula <- stats::formula(model)

Check warning on line 47 in R/performance_accuracy.R

View workflow job for this annotation

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

file=R/performance_accuracy.R,line=47,col=3,[object_overwrite_linter] 'formula' is an exported object from package 'stats'. Avoid re-using such symbols.

# get name of response
resp.name <- insight::find_response(model)
Expand All @@ -67,7 +65,7 @@
bootstr <- replicate(n, sample(nrow(model_data), replace = TRUE), simplify = FALSE)

models <- lapply(bootstr, function(.x) {
text <- utils::capture.output({

Check warning on line 68 in R/performance_accuracy.R

View workflow job for this annotation

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

file=R/performance_accuracy.R,line=68,col=9,[object_overwrite_linter] 'text' is an exported object from package 'graphics'. Avoid re-using such symbols.
model_upd <- stats::update(model, data = model_data[.x, ])
})
# stats::lm(formula, data = model_data[.x, ])
Expand All @@ -82,7 +80,7 @@
as.data.frame(model_data[.x, ])[[resp.name]]
})

accuracy <- mapply(function(.x, .y) {

Check warning on line 83 in R/performance_accuracy.R

View workflow job for this annotation

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

file=R/performance_accuracy.R,line=83,col=19,[undesirable_function_linter] Avoid undesirable function "mapply".
stats::cor(.x, .y, use = "pairwise.complete.obs")
}, predictions, response)
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/performance_cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' @export
performance_cv <- function(model,
data = NULL,
method = c("holdout", "k_fold", "loo"),
method = "holdout",
metrics = "all",
prop = 0.30,
k = 5,
Expand All @@ -53,7 +53,7 @@ performance_cv <- function(model,
metrics[metrics == "DEVIANCE"] <- "Deviance"
}
if (is.null(data)) {
method <- match.arg(method, choices = c("holdout", "k_fold", "loo"))
method <- insight::validate_argument(method, c("holdout", "k_fold", "loo"))
}
if (!is.null(data) && inherits(model, "BFBayesFactor")) {
insight::format_error("Models of class 'BFBayesFactor' not yet supported.")
Expand Down
Loading
Loading