Skip to content

Commit

Permalink
Update model
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 7, 2024
1 parent d3fa69c commit 90942e2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
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.11.0.7
Version: 0.11.0.8
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
the usual style as for other models and no longer returns plots from
`bayesplot::pp_check()`.

* Updated the trained model that is used to prediction distributions in
`check_distribution()`.

## Bug fixes

* `check_model()` now falls back on normal Q-Q plots when a model is not supported
Expand Down
27 changes: 22 additions & 5 deletions R/check_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,40 @@ check_distribution.numeric <- function(model) {
# 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")))
mode <- NULL

Check warning on line 195 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_distribution.R,line=195,col=3,[object_overwrite_linter] 'mode' is an exported object from package 'base'. Avoid re-using such symbols.
# find mode for integer, or MAP for distributions
if (all(.is_integer(x))) {
mode <- datawizard::distribution_mode(x)

Check warning on line 198 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_distribution.R,line=198,col=5,[object_overwrite_linter] 'mode' is an exported object from package 'base'. Avoid re-using such symbols.
} else {
# this might fail, so we wrap in ".safe()"
mode <- tryCatch(

Check warning on line 201 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_distribution.R,line=201,col=5,[object_overwrite_linter] 'mode' is an exported object from package 'base'. Avoid re-using such symbols.
as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
error = function(e) NULL
)
if (is.null(mode)) {
mode <- tryCatch(

Check warning on line 206 in R/check_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_distribution.R,line=206,col=7,[object_overwrite_linter] 'mode' is an exported object from package 'base'. Avoid re-using such symbols.
as.numeric(bayestestR::map_estimate(x, bw = "kernel")),
error = function(e) NULL
)
}
}

if (is.null(map_est)) {
map_est <- mean(x) - datawizard::distribution_mode(x)
if (is.null(mode)) {
mean_mode_diff <- 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.")
}
insight::format_alert(msg)
} else {
mean_mode_diff <- .safe(mean(x) - mode)
}

data.frame(
SD = stats::sd(x),
MAD = stats::mad(x, constant = 1),
Mean_Median_Distance = mean(x) - stats::median(x),
Mean_Mode_Distance = map_est,
Mean_Mode_Distance = mean_mode_diff,
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
Binary file modified R/sysdata.rda
Binary file not shown.
22 changes: 13 additions & 9 deletions WIP/generate_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@ for (di in seq_along(distrs)) {
# x_scaled <- parameters::normalize(x, verbose = FALSE)

if (length(x) >= 10) {
mode <- tryCatch(
bayestestR::map_estimate(x, bw = "nrd0"),
error = function(e) NULL
)
if (is.null(mode)) {
if (all(.is.integer(x))) {
mode <- datawizard::distribution_mode(x)
} else {
mode <- tryCatch(
bayestestR::map_estimate(x, bw = "kernel"),
as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
error = function(e) NULL
)
}
if (is.null(mode)) {
mode <- datawizard::distribution_mode(x)
if (is.null(mode)) {
mode <- tryCatch(
as.numeric(bayestestR::map_estimate(x, bw = "kernel")),
error = function(e) NULL
)
}
if (is.null(mode)) {
mode <- datawizard::distribution_mode(x)
}
}
# Extract features
data <- data.frame(
Expand Down
13 changes: 6 additions & 7 deletions tests/testthat/_snaps/check_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
Predicted Distribution of Residuals
Distribution Probability
cauchy 94%
gamma 3%
lognormal 3%
Distribution Probability
cauchy 91%
gamma 6%
neg. binomial (zero-infl.) 3%
Predicted Distribution of Response
Distribution Probability
lognormal 53%
gamma 44%
exponential 3%
lognormal 66%
gamma 34%

6 changes: 3 additions & 3 deletions tests/testthat/test-check_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ test_that("check_distribution", {
expect_equal(
out$p_Residuals,
c(
0, 0, 0, 0, 0.9375, 0, 0, 0, 0.03125, 0, 0, 0.03125, 0, 0,
0, 0, 0, 0, 0.90625, 0, 0, 0, 0.0625, 0, 0, 0.03125, 0, 0,
0, 0, 0, 0, 0, 0, 0
),
tolerance = 1e-4
)
expect_equal(
out$p_Response,
c(
0, 0, 0, 0, 0, 0, 0.03125, 0, 0.4375, 0, 0, 0.53125, 0, 0,
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0.34375, 0, 0, 0.65625, 0, 0, 0, 0,
0, 0, 0, 0, 0
),
tolerance = 1e-4
)
Expand Down

0 comments on commit 90942e2

Please sign in to comment.