Skip to content

Commit

Permalink
allow worm plots as a diagnostic in appraise as per #62
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinsimpson committed Feb 21, 2023
1 parent dddb5a3 commit cde715b
Show file tree
Hide file tree
Showing 6 changed files with 3,294 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,5 +1,5 @@
Package: gratia
Version: 0.8.1.8
Version: 0.8.1.9
Date: 2023-02-21
Title: Graceful 'ggplot'-Based Graphics and Other Functions for GAMs Fitted Using 'mgcv'
Authors@R: c(person(given = "Gavin L.", family = "Simpson",
Expand Down
7 changes: 5 additions & 2 deletions NEWS.md
@@ -1,4 +1,4 @@
# gratia 0.8.1.8
# gratia 0.8.1.9

## User visible changes

Expand All @@ -16,7 +16,7 @@

## New features

* `data_sim()` can now simulate response data from Tweedie and ordered
* `data_sim()` can now simulate response data from gamma, Tweedie and ordered
categorical distributions.

* `fitted_samples()` and `smooth_samples()` can now use the Metropolis Hastings
Expand Down Expand Up @@ -47,6 +47,9 @@

* `link()` and `inv_link()` work for models fitted using the `cnorm()` family.

* A worm plot can now be drawn in place of the QQ plot with `appraise()` via
new argument `use_worm = TRUE`. #62

## Bug fixes

* `link()`, `inv_link()`, and related family functions for the `ocat()` weren't
Expand Down
19 changes: 14 additions & 5 deletions R/diagnose.R
Expand Up @@ -618,10 +618,11 @@
}

#' @title Model diagnostic plots
#'
#'
#' @param model a fitted model. Currently only class `"gam"`.
#' @param method character; method used to generate theoretical quantiles. Note
#' that `method = "direct"` is deprecated in favour of `method = "uniform"`.
#' @param use_worm logical; should a worm plot be drawn in place of the QQ plot?
#' @param n_uniform numeric; number of times to randomize uniform quantiles
#' in the direct computation method (`method = "direct"`) for QQ plots.
#' @param n_simulate numeric; number of data sets to simulate from the estimated
Expand Down Expand Up @@ -681,6 +682,7 @@
#' @export
`appraise.gam` <- function(model,
method = c("uniform", "simulate", "normal", "direct"),
use_worm = FALSE,
n_uniform = 10, n_simulate = 50,
type = c("deviance", "pearson", "response"),
n_bins = c("sturges", "scott", "fd"),
Expand All @@ -707,10 +709,17 @@
paste(dQuote(c("sturges", "scott", "fd")), collapse = ", "))
}

plt1 <- qq_plot(model, method = method, type = type, n_uniform = n_uniform,
n_simulate = n_simulate, level = level, ci_alpha = ci_alpha,
point_col = point_col, point_alpha = point_alpha,
line_col = line_col)
plt1 <- if (isTRUE(use_worm)) {
worm_plot(model, method = method, type = type, n_uniform = n_uniform,
n_simulate = n_simulate, level = level, ci_alpha = ci_alpha,
point_col = point_col, point_alpha = point_alpha,
line_col = line_col)
} else {
qq_plot(model, method = method, type = type, n_uniform = n_uniform,
n_simulate = n_simulate, level = level, ci_alpha = ci_alpha,
point_col = point_col, point_alpha = point_alpha,
line_col = line_col)
}
plt2 <- residuals_linpred_plot(model, type = type, point_col = point_col,
point_alpha = point_alpha,
line_col = line_col)
Expand Down
3 changes: 3 additions & 0 deletions man/appraise.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3,264 changes: 3,264 additions & 0 deletions tests/testthat/_snaps/diagnostic-plots/appraise-worm-plot.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/testthat/test-diagnostic-plots.R
Expand Up @@ -110,4 +110,11 @@ test_that("appraise works for a LM", {
set.seed(1)
expect_silent(plt <- appraise(m_lm))
expect_doppelganger("appraise lm ", plt)
})

test_that("appraise can use the worm plot", {
set.seed(1)
expect_silent(plt <- appraise(m_gam, use_worm = TRUE,
method = "simulate"))
expect_doppelganger("appraise worm plot", plt)
})

0 comments on commit cde715b

Please sign in to comment.