Skip to content

Commit 9fd1491

Browse files
authored
Add a show_smooth argument to check_model() (#979)
1 parent 5f850d9 commit 9fd1491

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎NEWS.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
## Changes
4646

47+
* `check_model()` gets a `show_smooth` argument. With `show_smooth = FALSE`,
48+
the Linearity and Homogeneity of Variance panels draw neither the LOESS
49+
smoother nor the cell means, and the Influential Observations panel draws no
50+
LOESS smoother. This needs *see* newer than 0.14.2. Older versions ignore the
51+
setting.
52+
4753
* `check_model()` and `check_predictions()` now work for multivariate models
4854
from package *brms*. Use the `response` argument to name the response to
4955
check. Without `response`, both functions give an error that lists the names

‎R/check_model.R‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
#' @param show_ci Logical, if `TRUE`, confidence intervals in plots are shown.
5151
#' For models with only categorical predictors, confidence intervals are not shown
5252
#' by default, because in this case, these are usually on very large scales.
53+
#' @param show_smooth Logical, if `FALSE`, the Linearity and Homogeneity of
54+
#' Variance panels draw neither the LOESS smoother nor the cell means, and the
55+
#' Influential Observations panel draws no LOESS smoother. This needs **see**
56+
#' newer than 0.14.2. Older versions ignore the setting.
5357
#' @param maximum_dots Limits the number of data points for models with many
5458
#' observations, to reduce the time for rendering the plot. Defaults to a
5559
#' maximum of 2000 data points to render
@@ -286,6 +290,7 @@ check_model.default <- function(
286290
theme = see::theme_lucid(),
287291
verbose = FALSE,
288292
x = NULL,
293+
show_smooth = TRUE,
289294
...
290295
) {
291296
## TODO remove deprecation warning later
@@ -296,6 +301,8 @@ check_model.default <- function(
296301
model <- x
297302
}
298303

304+
.check_show_smooth(show_smooth)
305+
299306
# check model formula
300307
if (verbose) {
301308
insight::formula_ok(model)
@@ -392,6 +399,7 @@ check_model.default <- function(
392399
type_missing = missing(type),
393400
show_dots = show_dots,
394401
show_ci = show_ci,
402+
show_smooth = show_smooth,
395403
maximum_dots = maximum_dots,
396404
size_dot = size_dot,
397405
size_line = size_line,
@@ -423,6 +431,7 @@ check_model.default <- function(
423431
type_missing,
424432
show_dots,
425433
show_ci,
434+
show_smooth,
426435
maximum_dots,
427436
size_dot,
428437
size_line,
@@ -478,6 +487,7 @@ check_model.default <- function(
478487
attr(assumptions_data, "dot_alpha") <- alpha_dot
479488
attr(assumptions_data, "show_dots") <- isTRUE(show_dots)
480489
attr(assumptions_data, "show_ci") <- isTRUE(show_ci)
490+
attr(assumptions_data, "show_smooth") <- isTRUE(show_smooth)
481491
attr(assumptions_data, "detrend") <- detrend
482492
attr(assumptions_data, "colors") <- colors
483493
attr(assumptions_data, "theme") <- theme
@@ -492,6 +502,14 @@ check_model.default <- function(
492502
}
493503

494504

505+
# same check and message as in see::plot.see_check_model()
506+
.check_show_smooth <- function(show_smooth) {
507+
if (!isTRUE(show_smooth) && !isFALSE(show_smooth)) {
508+
insight::format_error("`show_smooth` must be either `TRUE` or `FALSE`.")
509+
}
510+
}
511+
512+
495513
# methods ----------------------------------
496514

497515
#' @export
@@ -537,6 +555,7 @@ check_model.stanreg <- function(
537555
verbose = FALSE,
538556
x = NULL,
539557
response = NULL,
558+
show_smooth = TRUE,
540559
...
541560
) {
542561
## TODO remove deprecation warning later
@@ -547,6 +566,8 @@ check_model.stanreg <- function(
547566
model <- x
548567
}
549568

569+
.check_show_smooth(show_smooth)
570+
550571
# multivariate brms models need one response, see https://github.com/easystats/performance/issues/908
551572
resp <- .check_multivariate_resp(
552573
model,
@@ -589,6 +610,7 @@ check_model.stanreg <- function(
589610
type_missing = missing(type),
590611
show_dots = show_dots,
591612
show_ci = show_ci,
613+
show_smooth = show_smooth,
592614
maximum_dots = maximum_dots,
593615
size_dot = size_dot,
594616
size_line = size_line,
@@ -620,6 +642,7 @@ check_model.stanreg <- function(
620642
detrend = detrend,
621643
show_dots = show_dots,
622644
show_ci = show_ci,
645+
show_smooth = show_smooth,
623646
bandwidth = bandwidth,
624647
type = type,
625648
residual_type = residual_type,
@@ -650,6 +673,7 @@ check_model.brmsfit <- check_model.stanreg
650673
type_missing,
651674
show_dots,
652675
show_ci,
676+
show_smooth,
653677
maximum_dots,
654678
size_dot,
655679
size_line,
@@ -740,6 +764,7 @@ check_model.brmsfit <- check_model.stanreg
740764
type_missing = type_missing,
741765
show_dots = show_dots,
742766
show_ci = show_ci,
767+
show_smooth = show_smooth,
743768
maximum_dots = maximum_dots,
744769
size_dot = size_dot,
745770
size_line = size_line,
@@ -816,6 +841,7 @@ check_model.model_fit <- function(
816841
theme = see::theme_lucid(),
817842
verbose = FALSE,
818843
x = NULL,
844+
show_smooth = TRUE,
819845
...
820846
) {
821847
## TODO remove deprecation warning later
@@ -841,6 +867,7 @@ check_model.model_fit <- function(
841867
detrend = detrend,
842868
show_dots = show_dots,
843869
show_ci = show_ci,
870+
show_smooth = show_smooth,
844871
maximum_dots = maximum_dots,
845872
bandwidth = bandwidth,
846873
type = type,
@@ -876,6 +903,7 @@ check_model.performance_simres <- function(
876903
theme = see::theme_lucid(),
877904
verbose = FALSE,
878905
x = NULL,
906+
show_smooth = TRUE,
879907
...
880908
) {
881909
## TODO remove deprecation warning later
@@ -901,6 +929,7 @@ check_model.performance_simres <- function(
901929
detrend = detrend,
902930
show_dots = show_dots,
903931
show_ci = show_ci,
932+
show_smooth = show_smooth,
904933
maximum_dots = maximum_dots,
905934
bandwidth = bandwidth,
906935
type = type,

‎man/check_model.Rd‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-check_model.R‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,46 @@ test_that("`check_model()` respects user-supplied `show_ci`", {
171171
expect_false(attr(check_model(m_numeric, show_ci = FALSE), "show_ci"))
172172
})
173173

174+
test_that("`check_model()` stores `show_smooth`", {
175+
data(mtcars)
176+
models <- list(
177+
lm = lm(mpg ~ wt, data = mtcars),
178+
glm = glm(am ~ wt, family = binomial, data = mtcars)
179+
)
180+
# simulate_residuals() needs DHARMa
181+
if (requireNamespace("DHARMa", quietly = TRUE)) {
182+
models$simres <- simulate_residuals(lm(mpg ~ wt, data = mtcars))
183+
}
184+
for (m in models) {
185+
expect_true(attr(check_model(m), "show_smooth"))
186+
expect_false(attr(check_model(m, show_smooth = FALSE), "show_smooth"))
187+
}
188+
})
189+
190+
test_that("every `check_model()` method has a `show_smooth` argument", {
191+
methods <- ls(asNamespace("performance"), pattern = "^check_model\\.")
192+
expect_gt(length(methods), 0)
193+
for (fun in methods) {
194+
expect_true(
195+
"show_smooth" %in%
196+
names(formals(get(fun, envir = asNamespace("performance")))),
197+
info = fun
198+
)
199+
}
200+
})
201+
202+
test_that("`check_model()` checks `show_smooth`", {
203+
data(mtcars)
204+
m <- lm(mpg ~ wt, data = mtcars)
205+
for (value in list(NA, 1, "no", c(TRUE, FALSE))) {
206+
expect_error(
207+
check_model(m, show_smooth = value),
208+
"`show_smooth` must be either `TRUE` or `FALSE`.",
209+
fixed = TRUE
210+
)
211+
}
212+
})
213+
174214
test_that("`check_model()` warnings for tweedie", {
175215
skip_if_not_installed("glmmTMB")
176216
skip_if_not_installed("lme4")
@@ -319,9 +359,11 @@ test_that("`check_model()` falls back to Bayesian checks if brms conversion fail
319359
expect_identical(attr(out, "check"), "all")
320360
expect_identical(attr(out, "model_class"), "brmsfit")
321361
expect_false(is.null(attr(out, "model_info")))
362+
expect_true(attr(out, "show_smooth"))
322363

323-
out <- suppressMessages(check_model(m, check = "vif"))
364+
out <- suppressMessages(check_model(m, check = "vif", show_smooth = FALSE))
324365
expect_named(out, "VIF")
366+
expect_false(attr(out, "show_smooth"))
325367
})
326368

327369
test_that("`check_model()` fallback for brms models can be plotted", {

0 commit comments

Comments
 (0)