Skip to content

Commit

Permalink
Merge branch 'issue-286': allow a user-specified seed in qq_plot() an…
Browse files Browse the repository at this point in the history
…d also in appraise(). Closes #286
  • Loading branch information
gavinsimpson committed Aug 11, 2024
2 parents 23957c6 + 7d391af commit f07d53e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
53 changes: 40 additions & 13 deletions R/diagnose.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#' in the direct computation method (`method = "uniform"`).
#' @param n_simulate numeric; number of data sets to simulate from the estimated
#' model when using the simulation method (`method = "simulate"`).
#' @param seed numeric; the random number seed to use for `method = "simulate"`
#' and `method = "uniform"`.
#' @param level numeric; the coverage level for reference intervals. Must be
#' strictly `0 < level < 1`. Only used with `method = "simulate"`.
#' @param xlab character or expression; the label for the y axis. If not
Expand Down Expand Up @@ -108,24 +110,45 @@
#' ## Alternatively use simulate new data from the model, which
#' ## allows construction of reference intervals for the Q-Q plot
#' qq_plot(m,
#' method = "simulate", point_col = "steelblue",
#' method = "simulate",
#' seed = 42,
#' point_col = "steelblue",
#' point_alpha = 0.4
#' )
#'
#' ## ... or use the usual normality assumption
#' qq_plot(m, method = "normal")
`qq_plot.gam` <- function(model,
method = c("uniform", "simulate", "normal", "direct"),
type = c("deviance", "response", "pearson"),
n_uniform = 10, n_simulate = 50,
level = 0.9,
ylab = NULL, xlab = NULL,
title = NULL, subtitle = NULL, caption = NULL,
ci_col = "black",
ci_alpha = 0.2,
point_col = "black",
point_alpha = 1,
line_col = "red", ...) {
method = c("uniform", "simulate", "normal", "direct"),
type = c("deviance", "response", "pearson"),
n_uniform = 10,
n_simulate = 50,
seed = NULL,
level = 0.9,
ylab = NULL,
xlab = NULL,
title = NULL,
subtitle = NULL,
caption = NULL,
ci_col = "black",
ci_alpha = 0.2,
point_col = "black",
point_alpha = 1,
line_col = "red", ...) {
# sort out seed
if (!exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE)) {
runif(1)
}
if (is.null(seed)) {
RNGstate <- get(".Random.seed", envir = .GlobalEnv)
} else {
R.seed <- get(".Random.seed", envir = .GlobalEnv)
set.seed(seed)
RNGstate <- structure(seed, kind = as.list(RNGkind()))
on.exit(assign(".Random.seed", R.seed, envir = .GlobalEnv))
}

# figure out method stuff
method <- match.arg(method) # what method for the QQ plot?
if (identical(method, "direct")) {
message("`method = \"direct\"` is deprecated, use `\"uniform\"`")
Expand Down Expand Up @@ -728,6 +751,8 @@
#' @param n_simulate numeric; number of data sets to simulate from the estimated
#' model when using the simulation method (`method = "simulate"`) for QQ
#' plots.
#' @param seed numeric; the random number seed to use for `method = "simulate"`
#' and `method = "uniform"`.
#' @param type character; type of residuals to use. Only `"deviance"`,
#' `"response"`, and `"pearson"` residuals are allowed.
#' @param n_bins character or numeric; either the number of bins or a string
Expand Down Expand Up @@ -771,7 +796,7 @@
#' ## To change the theme for all panels use the & operator, for example to
#' ## change the ggplot theme for all panels
#' library("ggplot2")
#' appraise(mod,
#' appraise(mod, seed = 42,
#' point_col = "steelblue", point_alpha = 0.4,
#' line_col = "black"
#' ) & theme_minimal()
Expand All @@ -785,6 +810,7 @@
method = c("uniform", "simulate", "normal", "direct"),
use_worm = FALSE,
n_uniform = 10, n_simulate = 50,
seed = NULL,
type = c("deviance", "pearson", "response"),
n_bins = c("sturges", "scott", "fd"),
ncol = NULL, nrow = NULL,
Expand Down Expand Up @@ -823,6 +849,7 @@
type = type,
n_uniform = n_uniform,
n_simulate = n_simulate,
seed = seed,
level = level,
ci_col = ci_col,
ci_alpha = ci_alpha,
Expand Down
6 changes: 5 additions & 1 deletion man/appraise.Rd

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

8 changes: 7 additions & 1 deletion man/qq_plot.Rd

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

0 comments on commit f07d53e

Please sign in to comment.