Skip to content

Fix problem with percentage_central argument in check_outliers() with MCD method #673

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

Merged
merged 18 commits into from
Feb 4, 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
4 changes: 2 additions & 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.8.10
Version: 0.10.8.11
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -144,7 +144,7 @@ Suggests:
withr (>= 3.0.0)
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.2.3.9000
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/parallel: true
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* `performance_score()` should no longer fail for models where scoring rules
can't be calculated. Instead, an informative message is returned.

* `check_outliers()` now properly accept the `percentage_central` argument when
using the `"mcd"` method.

# performance 0.10.8

## Changes
Expand Down
10 changes: 5 additions & 5 deletions R/check_heterogeneity_bias.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ check_heterogeneity_bias <- function(x, select = NULL, group = NULL) {
if (insight::is_model(x)) {
group <- insight::find_random(x, split_nested = TRUE, flatten = TRUE)
if (is.null(group)) {
insight::format_error("Model is no mixed model. Please provide a mixed model, or a data frame and arguments `select` and `group`.")
insight::format_error("Model is no mixed model. Please provide a mixed model, or a data frame and arguments `select` and `group`.") # nolint
}
data <- insight::get_data(x, source = "mf", verbose = FALSE)
my_data <- insight::get_data(x, source = "mf", verbose = FALSE)
select <- insight::find_predictors(x, effects = "fixed", component = "conditional", flatten = TRUE)
} else {
if (inherits(select, "formula")) {
Expand All @@ -42,15 +42,15 @@ check_heterogeneity_bias <- function(x, select = NULL, group = NULL) {
if (inherits(group, "formula")) {
group <- all.vars(group)
}
data <- x
my_data <- x
}

unique_groups <- .n_unique(data[[group]])
unique_groups <- .n_unique(my_data[[group]])
combinations <- expand.grid(select, group)

result <- Map(function(predictor, id) {
# demean predictor
d <- datawizard::demean(data, select = predictor, group = id, verbose = FALSE)
d <- datawizard::demean(my_data, select = predictor, group = id, verbose = FALSE)

# get new names
within_name <- paste0(predictor, "_within")
Expand Down
14 changes: 6 additions & 8 deletions R/check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,12 @@ check_model.default <- function(x,
minfo <- insight::model_info(x, verbose = FALSE)

ca <- tryCatch(
{
if (minfo$is_bayesian) {
suppressWarnings(.check_assumptions_stan(x, ...))
} else if (minfo$is_linear) {
suppressWarnings(.check_assumptions_linear(x, minfo, verbose, ...))
} else {
suppressWarnings(.check_assumptions_glm(x, minfo, verbose, ...))
}
if (minfo$is_bayesian) {
suppressWarnings(.check_assumptions_stan(x, ...))
} else if (minfo$is_linear) {
suppressWarnings(.check_assumptions_linear(x, minfo, verbose, ...))
} else {
suppressWarnings(.check_assumptions_glm(x, minfo, verbose, ...))
},
error = function(e) {
NULL
Expand Down
74 changes: 33 additions & 41 deletions R/check_model_diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,25 @@
insight::check_if_installed("lme4")

tryCatch(
{
if (inherits(model, "glmmTMB")) {
var_attr <- "condVar"
re <- .collapse_cond(lme4::ranef(model, condVar = TRUE))
} else {
var_attr <- "postVar"
re <- lme4::ranef(model, condVar = TRUE)
}
if (inherits(model, "glmmTMB")) {
var_attr <- "condVar"
re <- .collapse_cond(lme4::ranef(model, condVar = TRUE))
} else {
var_attr <- "postVar"
re <- lme4::ranef(model, condVar = TRUE)
},
error = function(e) {
return(NULL)
NULL
}
)


se <- tryCatch(
{
suppressWarnings(lapply(re, function(.x) {
pv <- attr(.x, var_attr, exact = TRUE)
cols <- seq_len(dim(pv)[1])
unlist(lapply(cols, function(.y) sqrt(pv[.y, .y, ])))
}))
},
suppressWarnings(lapply(re, function(.x) {
pv <- attr(.x, var_attr, exact = TRUE)
cols <- seq_len(dim(pv)[1])
unlist(lapply(cols, function(.y) sqrt(pv[.y, .y, ])))
})),
error = function(e) {
NULL
}
Expand Down Expand Up @@ -186,15 +182,15 @@
n_params <- tryCatch(model$rank, error = function(e) insight::n_parameters(model))

infl <- stats::influence(model, do.coef = FALSE)
resid <- as.numeric(insight::get_residuals(model))
model_resid <- as.numeric(insight::get_residuals(model))

std_resid <- tryCatch(stats::rstandard(model, infl), error = function(e) resid)
std_resid <- tryCatch(stats::rstandard(model, infl), error = function(e) model_resid)

plot_data <- data.frame(
Hat = infl$hat,
Cooks_Distance = stats::cooks.distance(model, infl),
Fitted = insight::get_predicted(model, ci = NULL),
Residuals = resid,
Residuals = model_resid,
Std_Residuals = std_resid,
stringsAsFactors = FALSE
)
Expand All @@ -213,12 +209,10 @@

.diag_ncv <- function(model, verbose = TRUE) {
ncv <- tryCatch(
{
data.frame(
x = as.numeric(stats::fitted(model)),
y = as.numeric(stats::residuals(model))
)
},
data.frame(
x = as.numeric(stats::fitted(model)),
y = as.numeric(stats::residuals(model))
),
error = function(e) {
NULL
}
Expand All @@ -244,24 +238,22 @@
.diag_homogeneity <- function(model, verbose = TRUE) {
faminfo <- insight::model_info(model)
r <- tryCatch(
{
if (inherits(model, "merMod")) {
stats::residuals(model, scaled = TRUE)
} else if (inherits(model, "gam")) {
stats::residuals(model, type = "scaled.pearson")
} else if (inherits(model, c("glmmTMB", "MixMod"))) {
sigma <- if (faminfo$is_mixed) {
sqrt(insight::get_variance_residual(model))
} else {
.sigma_glmmTMB_nonmixed(model, faminfo)
}
stats::residuals(model) / sigma
} else if (inherits(model, "glm")) {
## TODO: check if we can / should use deviance residuals (as for QQ plots) here as well?
stats::rstandard(model, type = "pearson")
if (inherits(model, "merMod")) {
stats::residuals(model, scaled = TRUE)
} else if (inherits(model, "gam")) {
stats::residuals(model, type = "scaled.pearson")
} else if (inherits(model, c("glmmTMB", "MixMod"))) {
residual_sigma <- if (faminfo$is_mixed) {
sqrt(insight::get_variance_residual(model))
} else {
stats::rstandard(model)
.sigma_glmmTMB_nonmixed(model, faminfo)
}
stats::residuals(model) / residual_sigma
} else if (inherits(model, "glm")) {
## TODO: check if we can / should use deviance residuals (as for QQ plots) here as well?
stats::rstandard(model, type = "pearson")
} else {
stats::rstandard(model)
},
error = function(e) {
NULL
Expand Down
Loading