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

DHARMa implementation for new check_residuals() function #643

Merged
merged 78 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
137a627
DHARMa implementation for new `check_residuals()` function
strengejacke Oct 26, 2023
4fc9274
draft function
strengejacke Oct 26, 2023
e38cc08
update pkgdown
strengejacke Oct 26, 2023
4c805bf
Don't prematurely return residuals in `simulate_residuals()`
mccarthy-m-g Oct 26, 2023
bb1a2f3
draft `check_residuals()`
mccarthy-m-g Oct 26, 2023
de272d7
Add (temporary?) vignette for basic simulated residuals workflow demo…
mccarthy-m-g Oct 26, 2023
a6a5afe
clarify why model fit is needed
mccarthy-m-g Oct 26, 2023
5b96362
fix vignette issues
strengejacke Oct 27, 2023
ddc4d6b
lintr
strengejacke Oct 27, 2023
a51e77a
RD; update namespace
strengejacke Oct 27, 2023
6ffa8b6
add method for check_normality
strengejacke Oct 27, 2023
991b483
add class attr for see::plot
strengejacke Oct 27, 2023
542ea79
save necessary info
strengejacke Oct 27, 2023
0e9a2ae
fix check issues
strengejacke Oct 27, 2023
30038c9
rd
strengejacke Oct 27, 2023
50f1f3d
Add basic print output for `simulate_residuals()`
mccarthy-m-g Oct 27, 2023
67d3319
Merge branch 'main' into strengejacke/issue595
strengejacke Oct 28, 2023
4d4b776
Merge branch 'main' into strengejacke/issue595
strengejacke Oct 29, 2023
25ff890
draft
strengejacke Oct 30, 2023
2215784
fix print
strengejacke Oct 30, 2023
5b439d7
docs
strengejacke Oct 30, 2023
bd737f1
add plot method
strengejacke Oct 30, 2023
e1979a4
Merge branch 'main' into strengejacke/issue595
strengejacke Oct 30, 2023
d515869
add print method for `check_residuals()`
mccarthy-m-g Nov 3, 2023
ac34fe2
Merge branch 'main' into strengejacke/issue595
strengejacke Dec 20, 2023
e6209b6
Merge branch 'main' into strengejacke/issue595
strengejacke Mar 13, 2024
08eb9e0
fix
strengejacke Mar 14, 2024
0533558
test
strengejacke Mar 14, 2024
87d9036
docs
strengejacke Mar 15, 2024
aea57eb
version bump
strengejacke Mar 15, 2024
4cf45ba
news
strengejacke Mar 15, 2024
b9454a2
include DHARMa plot
strengejacke Mar 15, 2024
b128174
docs
strengejacke Mar 15, 2024
85817fe
detrend default to FALSE
strengejacke Mar 15, 2024
764dcdf
allow to directly pass models
strengejacke Mar 15, 2024
7d80db4
close #613
strengejacke Mar 15, 2024
3820cb0
docs
strengejacke Mar 15, 2024
d8b77f9
docs, args
strengejacke Mar 15, 2024
d1d4098
zi-inflation check based on DHARMa
strengejacke Mar 15, 2024
15a660f
Fixes #367
strengejacke Mar 15, 2024
5842c15
fixes
strengejacke Mar 15, 2024
3d0663e
check_overdispersion method
strengejacke Mar 15, 2024
5d68703
docs
strengejacke Mar 15, 2024
9fa5420
add test function
strengejacke Mar 15, 2024
a25ecac
fix
strengejacke Mar 15, 2024
d0822ba
fix
strengejacke Mar 15, 2024
7111028
fix issues
strengejacke Mar 16, 2024
6d7ee1c
lintr, styler, fix test
strengejacke Mar 16, 2024
3575e6a
fix ZI
strengejacke Mar 16, 2024
13c696f
add tests
strengejacke Mar 16, 2024
ad60db1
fix
strengejacke Mar 16, 2024
3d5844d
update descr
strengejacke Mar 16, 2024
25f2a16
fix
strengejacke Mar 16, 2024
e0235ed
docs
strengejacke Mar 16, 2024
39b3751
closes #632
strengejacke Mar 16, 2024
3382ecb
fix genpois
strengejacke Mar 16, 2024
62d2bfe
add tests
strengejacke Mar 16, 2024
18eed23
fix
strengejacke Mar 16, 2024
42d20d9
check_model for DHARMa and simres
strengejacke Mar 16, 2024
22cb2a6
docs
strengejacke Mar 16, 2024
d9e22e8
tweak
strengejacke Mar 16, 2024
825d456
...
strengejacke Mar 16, 2024
4d00962
try
strengejacke Mar 16, 2024
fe1ee76
Fix #696
strengejacke Mar 17, 2024
a473296
fix
strengejacke Mar 17, 2024
567f2c1
add tests
strengejacke Mar 17, 2024
24faae5
add tests
strengejacke Mar 17, 2024
c094025
fix test
strengejacke Mar 17, 2024
3d78e36
detect over and underdispersion
strengejacke Mar 17, 2024
37e7adc
docs
strengejacke Mar 17, 2024
35b5e19
fix Underdispersion? #263
strengejacke Mar 17, 2024
50735e3
typo, tests
strengejacke Mar 17, 2024
0180abd
check_outliers for DHARMa
strengejacke Mar 18, 2024
276a847
fix
strengejacke Mar 18, 2024
7b7ba1c
docs
strengejacke Mar 18, 2024
a6fd86d
docs
strengejacke Mar 18, 2024
54327de
add tests
strengejacke Mar 18, 2024
4127d9d
lintrs, spelling
strengejacke Mar 18, 2024
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 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.6
Version: 0.10.9.8
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -70,9 +70,8 @@ Depends:
R (>= 3.6)
Imports:
bayestestR (>= 0.13.2),
insight (>= 0.19.8),
insight (>= 0.19.9),
datawizard (>= 0.9.1),
methods,
stats,
utils
Suggests:
Expand All @@ -91,6 +90,7 @@ Suggests:
correlation,
cplm,
dbscan,
DHARMa,
estimatr,
fixest,
flextable,
Expand Down
19 changes: 19 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ S3method(check_heteroscedasticity,default)
S3method(check_homogeneity,afex_aov)
S3method(check_homogeneity,default)
S3method(check_homogeneity,htest)
S3method(check_model,DHARMa)
S3method(check_model,brmsfit)
S3method(check_model,default)
S3method(check_model,model_fit)
S3method(check_model,performance_simres)
S3method(check_model,stanreg)
S3method(check_multimodal,data.frame)
S3method(check_multimodal,numeric)
Expand All @@ -71,6 +73,7 @@ S3method(check_normality,lmerModLmerTest)
S3method(check_normality,merMod)
S3method(check_normality,numeric)
S3method(check_outliers,BFBayesFactor)
S3method(check_outliers,DHARMa)
S3method(check_outliers,character)
S3method(check_outliers,data.frame)
S3method(check_outliers,default)
Expand All @@ -87,11 +90,13 @@ S3method(check_outliers,meta)
S3method(check_outliers,metabin)
S3method(check_outliers,metagen)
S3method(check_outliers,numeric)
S3method(check_outliers,performance_simres)
S3method(check_outliers,rma)
S3method(check_outliers,rma.uni)
S3method(check_outliers,rq)
S3method(check_outliers,rqs)
S3method(check_outliers,rqss)
S3method(check_overdispersion,DHARMa)
S3method(check_overdispersion,default)
S3method(check_overdispersion,fixest)
S3method(check_overdispersion,fixest_multi)
Expand All @@ -103,11 +108,15 @@ S3method(check_overdispersion,model_fit)
S3method(check_overdispersion,negbin)
S3method(check_overdispersion,negbinirr)
S3method(check_overdispersion,negbinmfx)
S3method(check_overdispersion,performance_simres)
S3method(check_overdispersion,poissonirr)
S3method(check_overdispersion,poissonmfx)
S3method(check_predictions,BFBayesFactor)
S3method(check_predictions,default)
S3method(check_predictions,lme)
S3method(check_residuals,DHARMa)
S3method(check_residuals,default)
S3method(check_residuals,performance_simres)
S3method(check_singularity,MixMod)
S3method(check_singularity,clmm)
S3method(check_singularity,cpglmm)
Expand All @@ -123,6 +132,9 @@ S3method(check_sphericity,default)
S3method(check_sphericity,mlm)
S3method(check_symmetry,htest)
S3method(check_symmetry,numeric)
S3method(check_zeroinflation,DHARMa)
S3method(check_zeroinflation,default)
S3method(check_zeroinflation,performance_simres)
S3method(cronbachs_alpha,data.frame)
S3method(cronbachs_alpha,matrix)
S3method(cronbachs_alpha,parameters_pca)
Expand Down Expand Up @@ -261,10 +273,12 @@ S3method(plot,check_model)
S3method(plot,check_normality)
S3method(plot,check_outliers)
S3method(plot,check_overdisp)
S3method(plot,check_residuals)
S3method(plot,check_sphericity)
S3method(plot,compare_performance)
S3method(plot,performance_pp_check)
S3method(plot,performance_roc)
S3method(plot,performance_simres)
S3method(plot,test_likelihoodratio)
S3method(plot,test_performance)
S3method(print,binned_residuals)
Expand All @@ -283,7 +297,9 @@ S3method(print,check_normality_binom)
S3method(print,check_outliers)
S3method(print,check_outliers_metafor)
S3method(print,check_outliers_metagen)
S3method(print,check_outliers_simres)
S3method(print,check_overdisp)
S3method(print,check_residuals)
S3method(print,check_sphericity)
S3method(print,check_symmetry)
S3method(print,check_zi)
Expand All @@ -302,6 +318,7 @@ S3method(print,performance_pcp)
S3method(print,performance_pp_check)
S3method(print,performance_roc)
S3method(print,performance_score)
S3method(print,performance_simres)
S3method(print,r2_bayes)
S3method(print,r2_generic)
S3method(print,r2_loo)
Expand Down Expand Up @@ -535,6 +552,7 @@ export(check_outliers)
export(check_overdispersion)
export(check_posterior_predictions)
export(check_predictions)
export(check_residuals)
export(check_singularity)
export(check_sphericity)
export(check_sphericity_bartlett)
Expand Down Expand Up @@ -588,6 +606,7 @@ export(r2_tjur)
export(r2_xu)
export(r2_zeroinflated)
export(rmse)
export(simulate_residuals)
export(test_bf)
export(test_likelihoodratio)
export(test_lrt)
Expand Down
25 changes: 25 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,35 @@

* Rudimentary support for models of class `serp` from package *serp*.

## New functions

* `simulate_residuals()` and `check_residuals()`, to simulate and check residuals
from generalized linear (mixed) models. Simulating residuals is based on the
DHARMa package, and objects returned by `simulate_residuals()` inherit from
the `DHARMa` class, and thus can be used with any functions from the *DHARMa*
package. However, there are also implementations in the *performance* package,
such as `check_overdispersion()`, `check_zeroinflation()`, `check_outliers()`
or `check_model()`.

* Plots for `check_model()` have been improved. The Q-Q plots are now based
on simulated residuals from the DHARMa package for non-Gaussian models, thus
providing more accurate and informative plots. The half-normal QQ plot for
generalized linear models can still be obtained by setting the new argument
`residual_type = "normal"`.

* Following functions now support simulated residuals (from `simulate_residuals()`)
resp. objects returned from `DHARMa::simulateResiduals()`:
- `check_overdispersion()`
- `check_zeroinflation()`
- `check_outliers()`
- `check_model()`

## General

* Improved error messages for `check_model()` when QQ-plots cannot be created.

* `check_distribution()` is more stable for possibly sparse data.

## Bug fixes

* Fixed issue in `check_normality()` for t-tests.
Expand Down
20 changes: 16 additions & 4 deletions R/check_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
} else {
x <- stats::residuals(model)
}
dat <- .extract_features(x)
dat <- .extract_features(x, "residuals")

dist_residuals <- as.data.frame(t(stats::predict(classify_distribution, dat, type = "prob")))

Expand All @@ -88,7 +88,7 @@
dummy_factors = FALSE,
preserve_levels = TRUE
)
dat <- .extract_features(x)
dat <- .extract_features(x, "response")

dist_response <- as.data.frame(t(stats::predict(classify_distribution, dat, type = "prob")))

Expand Down Expand Up @@ -189,15 +189,27 @@

# utilities -----------------------------

.extract_features <- function(x) {
.extract_features <- function(x, type = NULL) {
# validation check, remove missings
x <- x[!is.na(x)]

# this might fail, so we wrap in ".safe()"
map_est <- .safe(mean(x) - as.numeric(bayestestR::map_estimate(x, bw = "nrd0")))

if (is.null(map_est)) {
map_est <- mean(x) - datawizard::distribution_mode(x)
msg <- "Could not accurately estimate the mode."
if (!is.null(type)) {
msg <- paste(msg, "Predicted distribution of the", type, "may be less accurate.")

Check warning on line 203 in R/check_distribution.R

View check run for this annotation

Codecov / codecov/patch

R/check_distribution.R#L200-L203

Added lines #L200 - L203 were not covered by tests
}
insight::format_alert(msg)

Check warning on line 205 in R/check_distribution.R

View check run for this annotation

Codecov / codecov/patch

R/check_distribution.R#L205

Added line #L205 was not covered by tests
}

data.frame(
SD = stats::sd(x),
MAD = stats::mad(x, constant = 1),
Mean_Median_Distance = mean(x) - stats::median(x),
Mean_Mode_Distance = mean(x) - as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
Mean_Mode_Distance = map_est,
SD_MAD_Distance = stats::sd(x) - stats::mad(x, constant = 1),
Var_Mean_Distance = stats::var(x) - mean(x),
Range_SD = diff(range(x)) / stats::sd(x),
Expand Down
Loading
Loading