Skip to content

Commit

Permalink
add gratia to Enhances and conditionally load methods for monotonic e…
Browse files Browse the repository at this point in the history
…valuation
  • Loading branch information
Nicholas Clark committed Jul 22, 2024
1 parent 5784633 commit 44a9b5c
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 139 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ Suggests:
coda,
runjags,
tibble (>= 3.0.0),
gratia,
usethis,
testthat
Enhances:
gratia
Additional_repositories: https://mc-stan.org/r-packages/
LinkingTo: Rcpp, RcppArmadillo
VignetteBuilder: knitr
5 changes: 2 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ S3method(as_draws_matrix,mvgam)
S3method(as_draws_rvars,mvgam)
S3method(coef,mvgam)
S3method(conditional_effects,mvgam)
S3method(eval_smooth,mod.smooth)
S3method(eval_smooth,moi.smooth)
S3method(find_predictors,mvgam)
S3method(find_predictors,mvgam_prefit)
S3method(fitted,mvgam)
Expand Down Expand Up @@ -78,7 +76,8 @@ export(code)
export(compare_mvgams)
export(dynamic)
export(eval_mvgam)
export(eval_smooth)
export(eval_smoothDotmodDotsmooth)
export(eval_smoothDotmoiDotsmooth)
export(forecast)
export(get_mvgam_priors)
export(hindcast)
Expand Down
122 changes: 65 additions & 57 deletions R/monotonic.R
Original file line number Diff line number Diff line change
Expand Up @@ -413,20 +413,54 @@ add_mono_model_file = function(model_file,
model_data = model_data))
}

#' S3 methods to evaluate individual smooths
#' @param smooth currently an object that inherits from class `mgcv.smooth`
#' @param ... arguments passed to other methods
#' @export
`eval_smooth` <- function(smooth, ...) {
UseMethod("eval_smooth")
# Add moi and mod smooth eval_smooth methods to gratia namespace
# on load
.onLoad <- function(libname, pkgname) {
if(requireNamespace("gratia", quietly = TRUE)){
registerS3method("eval_smooth",
"moi.smooth",
eval_smoothDotmoiDotsmooth,
envir = asNamespace("gratia"))
registerS3method("eval_smooth",
"mod.smooth",
eval_smoothDotmodDotsmooth,
envir = asNamespace("gratia"))
}
}

#' Evaluation of a monotonically increasing function
#' Evaluation of a monotonic functions in mvgam
#' These evaluation functions are needed so that gratia::draw methods work with mvgam
#' monotonic smooths
#' @rdname monotonic
#' @rdname eval_smooth_monotonic
#' @param model an object of class `"gam"`
#' @param smooth a smooth object of class `"moi.smooth"` or `"mod.smooth"`
#' @param n numeric; the number of points over the range of the covariate at
#' which to evaluate the smooth.
#' @param n_3d,n_4d numeric; the number of points over the range of last
#' covariate in a 3D or 4D smooth. The default is `NULL` which achieves the
#' standard behaviour of using `n` points over the range of all covariate,
#' resulting in `n^d` evaluation points, where `d` is the dimension of the
#' smooth. For `d > 2` this can result in very many evaluation points and slow
#' performance. For smooths of `d > 4`, the value of `n_4d` will be used for
#' all dimensions `> 4`, unless this is `NULL`, in which case the default
#' behaviour (using `n` for all dimensions) will be observed.
#' @param data a data frame of covariate values at which to evaluate the
#' smooth.
#' @param unconditional logical; should confidence intervals include the
#' uncertainty due to smoothness selection? If `TRUE`, the corrected Bayesian
#' covariance matrix will be used.
#' @param overall_uncertainty logical; should the uncertainty in the model
#' constant term be included in the standard error of the evaluate values of
#' the smooth?
#' @param dist numeric; if greater than 0, this is used to determine when
#' a location is too far from data to be plotted when plotting 2-D smooths.
#' The data are scaled into the unit square before deciding what to exclude,
#' and `dist` is a distance within the unit square. See
#' [mgcv::exclude.too.far()] for further details.
#' @param ... ignored.
#' @aliases eval_smooth.mod.smooth
#' @export
eval_smooth.moi.smooth = function(smooth,
eval_smoothDotmodDotsmooth = function(smooth,
model,
n = 100,
n_3d = NULL,
Expand All @@ -436,7 +470,6 @@ eval_smooth.moi.smooth = function(smooth,
overall_uncertainty = TRUE,
dist = NULL,
...) {

insight::check_if_installed("gratia")
model$cmX <- model$coefficients

Expand All @@ -446,7 +479,7 @@ eval_smooth.moi.smooth = function(smooth,
n = n, n_3d = n_3d, n_4d = n_4d,
id = which_smooth(
model,
smooth_label(smooth)
gratia::smooth_label(smooth)
)
)

Expand All @@ -466,7 +499,7 @@ eval_smooth.moi.smooth = function(smooth,
## add on info regarding by variable
eval_sm <- add_by_var_column(eval_sm, by_var = by_var)
## add on spline type info
eval_sm <- add_smooth_type_column(eval_sm, sm_type = "Mono inc P spline")
eval_sm <- add_smooth_type_column(eval_sm, sm_type = "Mono dec P spline")

# set some values to NA if too far from the data
if (gratia::smooth_dim(smooth) == 2L && (!is.null(dist) && dist > 0)) {
Expand All @@ -481,45 +514,20 @@ eval_smooth.moi.smooth = function(smooth,
eval_sm
}

#' Evaluation of a monotonically decreasing function
#' @rdname monotonic
#' @param model an object of class `"gam"`
#' @param smooth a smooth object of class `"moi.smooth"` or `"mod.smooth"`
#' @param n numeric; the number of points over the range of the covariate at
#' which to evaluate the smooth.
#' @param n_3d,n_4d numeric; the number of points over the range of last
#' covariate in a 3D or 4D smooth. The default is `NULL` which achieves the
#' standard behaviour of using `n` points over the range of all covariate,
#' resulting in `n^d` evaluation points, where `d` is the dimension of the
#' smooth. For `d > 2` this can result in very many evaluation points and slow
#' performance. For smooths of `d > 4`, the value of `n_4d` will be used for
#' all dimensions `> 4`, unless this is `NULL`, in which case the default
#' behaviour (using `n` for all dimensions) will be observed.
#' @param data a data frame of covariate values at which to evaluate the
#' smooth.
#' @param unconditional logical; should confidence intervals include the
#' uncertainty due to smoothness selection? If `TRUE`, the corrected Bayesian
#' covariance matrix will be used.
#' @param overall_uncertainty logical; should the uncertainty in the model
#' constant term be included in the standard error of the evaluate values of
#' the smooth?
#' @param dist numeric; if greater than 0, this is used to determine when
#' a location is too far from data to be plotted when plotting 2-D smooths.
#' The data are scaled into the unit square before deciding what to exclude,
#' and `dist` is a distance within the unit square. See
#' [mgcv::exclude.too.far()] for further details.
#' @param ... ignored.
#' @rdname eval_smooth_monotonic
#' @aliases eval_smooth.moi.smooth
#' @export
eval_smooth.mod.smooth = function(smooth,
model,
n = 100,
n_3d = NULL,
n_4d = NULL,
data = NULL,
unconditional = FALSE,
overall_uncertainty = TRUE,
dist = NULL,
...) {
eval_smoothDotmoiDotsmooth = function(smooth,
model,
n = 100,
n_3d = NULL,
n_4d = NULL,
data = NULL,
unconditional = FALSE,
overall_uncertainty = TRUE,
dist = NULL,
...) {

insight::check_if_installed("gratia")
model$cmX <- model$coefficients

Expand All @@ -529,7 +537,7 @@ eval_smooth.mod.smooth = function(smooth,
n = n, n_3d = n_3d, n_4d = n_4d,
id = which_smooth(
model,
smooth_label(smooth)
gratia::smooth_label(smooth)
)
)

Expand All @@ -549,7 +557,7 @@ eval_smooth.mod.smooth = function(smooth,
## add on info regarding by variable
eval_sm <- add_by_var_column(eval_sm, by_var = by_var)
## add on spline type info
eval_sm <- add_smooth_type_column(eval_sm, sm_type = "Mono dec P spline")
eval_sm <- add_smooth_type_column(eval_sm, sm_type = "Mono inc P spline")

# set some values to NA if too far from the data
if (gratia::smooth_dim(smooth) == 2L && (!is.null(dist) && dist > 0)) {
Expand Down Expand Up @@ -591,7 +599,7 @@ eval_smooth.mod.smooth = function(smooth,
if (is.gamm(object) || is.gamm4(object)) {
object <- object[["gam"]]
}
smooths <- smooths(object)
smooths <- gratia::smooths(object)
which(term == smooths)
}

Expand All @@ -600,7 +608,7 @@ eval_smooth.mod.smooth = function(smooth,
data, model, n, n_3d, n_4d, id,
var_order = NULL) {
if (is.null(data)) {
data <- smooth_data(
data <- gratia::smooth_data(
model = model,
n = n,
n_3d = n_3d,
Expand All @@ -609,9 +617,9 @@ eval_smooth.mod.smooth = function(smooth,
var_order = var_order
)
} else {
smooth <- get_smooths_by_id(model, id)[[1L]]
vars <- smooth_variable(smooth)
by_var <- by_variable(smooth)
smooth <- gratia::get_smooths_by_id(model, id)[[1L]]
vars <- gratia::smooth_variable(smooth)
by_var <- gratia::by_variable(smooth)
if (!identical(by_var, "NA")) {
vars <- append(vars, by_var)
}
Expand Down
16 changes: 0 additions & 16 deletions man/eval_smooth.Rd

This file was deleted.

78 changes: 78 additions & 0 deletions man/eval_smooth_monotonic.Rd

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

Loading

0 comments on commit 44a9b5c

Please sign in to comment.