Skip to content

Commit 90942e2

Browse files
committed
Update model
1 parent d3fa69c commit 90942e2

7 files changed

+48
-25
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: performance
33
Title: Assessment of Regression Models Performance
4-
Version: 0.11.0.7
4+
Version: 0.11.0.8
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
the usual style as for other models and no longer returns plots from
1616
`bayesplot::pp_check()`.
1717

18+
* Updated the trained model that is used to prediction distributions in
19+
`check_distribution()`.
20+
1821
## Bug fixes
1922

2023
* `check_model()` now falls back on normal Q-Q plots when a model is not supported

R/check_distribution.R

+22-5
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,40 @@ check_distribution.numeric <- function(model) {
192192
# validation check, remove missings
193193
x <- x[!is.na(x)]
194194

195-
# this might fail, so we wrap in ".safe()"
196-
map_est <- .safe(mean(x) - as.numeric(bayestestR::map_estimate(x, bw = "nrd0")))
195+
mode <- NULL
196+
# find mode for integer, or MAP for distributions
197+
if (all(.is_integer(x))) {
198+
mode <- datawizard::distribution_mode(x)
199+
} else {
200+
# this might fail, so we wrap in ".safe()"
201+
mode <- tryCatch(
202+
as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
203+
error = function(e) NULL
204+
)
205+
if (is.null(mode)) {
206+
mode <- tryCatch(
207+
as.numeric(bayestestR::map_estimate(x, bw = "kernel")),
208+
error = function(e) NULL
209+
)
210+
}
211+
}
197212

198-
if (is.null(map_est)) {
199-
map_est <- mean(x) - datawizard::distribution_mode(x)
213+
if (is.null(mode)) {
214+
mean_mode_diff <- mean(x) - datawizard::distribution_mode(x)
200215
msg <- "Could not accurately estimate the mode."
201216
if (!is.null(type)) {
202217
msg <- paste(msg, "Predicted distribution of the", type, "may be less accurate.")
203218
}
204219
insight::format_alert(msg)
220+
} else {
221+
mean_mode_diff <- .safe(mean(x) - mode)
205222
}
206223

207224
data.frame(
208225
SD = stats::sd(x),
209226
MAD = stats::mad(x, constant = 1),
210227
Mean_Median_Distance = mean(x) - stats::median(x),
211-
Mean_Mode_Distance = map_est,
228+
Mean_Mode_Distance = mean_mode_diff,
212229
SD_MAD_Distance = stats::sd(x) - stats::mad(x, constant = 1),
213230
Var_Mean_Distance = stats::var(x) - mean(x),
214231
Range_SD = diff(range(x)) / stats::sd(x),

R/sysdata.rda

11.2 KB
Binary file not shown.

WIP/generate_distribution.R

+13-9
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,22 @@ for (di in seq_along(distrs)) {
134134
# x_scaled <- parameters::normalize(x, verbose = FALSE)
135135

136136
if (length(x) >= 10) {
137-
mode <- tryCatch(
138-
bayestestR::map_estimate(x, bw = "nrd0"),
139-
error = function(e) NULL
140-
)
141-
if (is.null(mode)) {
137+
if (all(.is.integer(x))) {
138+
mode <- datawizard::distribution_mode(x)
139+
} else {
142140
mode <- tryCatch(
143-
bayestestR::map_estimate(x, bw = "kernel"),
141+
as.numeric(bayestestR::map_estimate(x, bw = "nrd0")),
144142
error = function(e) NULL
145143
)
146-
}
147-
if (is.null(mode)) {
148-
mode <- datawizard::distribution_mode(x)
144+
if (is.null(mode)) {
145+
mode <- tryCatch(
146+
as.numeric(bayestestR::map_estimate(x, bw = "kernel")),
147+
error = function(e) NULL
148+
)
149+
}
150+
if (is.null(mode)) {
151+
mode <- datawizard::distribution_mode(x)
152+
}
149153
}
150154
# Extract features
151155
data <- data.frame(

tests/testthat/_snaps/check_distribution.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
88
Predicted Distribution of Residuals
99
10-
Distribution Probability
11-
cauchy 94%
12-
gamma 3%
13-
lognormal 3%
10+
Distribution Probability
11+
cauchy 91%
12+
gamma 6%
13+
neg. binomial (zero-infl.) 3%
1414
1515
Predicted Distribution of Response
1616
1717
Distribution Probability
18-
lognormal 53%
19-
gamma 44%
20-
exponential 3%
18+
lognormal 66%
19+
gamma 34%
2120

tests/testthat/test-check_distribution.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ test_that("check_distribution", {
1818
expect_equal(
1919
out$p_Residuals,
2020
c(
21-
0, 0, 0, 0, 0.9375, 0, 0, 0, 0.03125, 0, 0, 0.03125, 0, 0,
21+
0, 0, 0, 0, 0.90625, 0, 0, 0, 0.0625, 0, 0, 0.03125, 0, 0,
2222
0, 0, 0, 0, 0, 0, 0
2323
),
2424
tolerance = 1e-4
2525
)
2626
expect_equal(
2727
out$p_Response,
2828
c(
29-
0, 0, 0, 0, 0, 0, 0.03125, 0, 0.4375, 0, 0, 0.53125, 0, 0,
30-
0, 0, 0, 0, 0, 0, 0
29+
0, 0, 0, 0, 0, 0, 0, 0, 0.34375, 0, 0, 0.65625, 0, 0, 0, 0,
30+
0, 0, 0, 0, 0
3131
),
3232
tolerance = 1e-4
3333
)

0 commit comments

Comments
 (0)