diff --git a/DESCRIPTION b/DESCRIPTION index 18520031..3aa9a7a3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index e81fe1ac..24ee29bf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/monotonic.R b/R/monotonic.R index 1818e7c7..2e29031b 100644 --- a/R/monotonic.R +++ b/R/monotonic.R @@ -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, @@ -436,7 +470,6 @@ eval_smooth.moi.smooth = function(smooth, overall_uncertainty = TRUE, dist = NULL, ...) { - insight::check_if_installed("gratia") model$cmX <- model$coefficients @@ -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) ) ) @@ -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)) { @@ -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 @@ -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) ) ) @@ -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)) { @@ -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) } @@ -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, @@ -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) } diff --git a/man/eval_smooth.Rd b/man/eval_smooth.Rd deleted file mode 100644 index ede5c20a..00000000 --- a/man/eval_smooth.Rd +++ /dev/null @@ -1,16 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/monotonic.R -\name{eval_smooth} -\alias{eval_smooth} -\title{S3 methods to evaluate individual smooths} -\usage{ -eval_smooth(smooth, ...) -} -\arguments{ -\item{smooth}{currently an object that inherits from class \code{mgcv.smooth}} - -\item{...}{arguments passed to other methods} -} -\description{ -S3 methods to evaluate individual smooths -} diff --git a/man/eval_smooth_monotonic.Rd b/man/eval_smooth_monotonic.Rd new file mode 100644 index 00000000..2099478f --- /dev/null +++ b/man/eval_smooth_monotonic.Rd @@ -0,0 +1,78 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/monotonic.R +\name{eval_smoothDotmodDotsmooth} +\alias{eval_smoothDotmodDotsmooth} +\alias{eval_smooth.mod.smooth} +\alias{eval_smoothDotmoiDotsmooth} +\alias{eval_smooth.moi.smooth} +\title{Evaluation of a monotonic functions in mvgam +These evaluation functions are needed so that gratia::draw methods work with mvgam +monotonic smooths} +\usage{ +eval_smoothDotmodDotsmooth( + smooth, + model, + n = 100, + n_3d = NULL, + n_4d = NULL, + data = NULL, + unconditional = FALSE, + overall_uncertainty = TRUE, + dist = NULL, + ... +) + +eval_smoothDotmoiDotsmooth( + smooth, + model, + n = 100, + n_3d = NULL, + n_4d = NULL, + data = NULL, + unconditional = FALSE, + overall_uncertainty = TRUE, + dist = NULL, + ... +) +} +\arguments{ +\item{smooth}{a smooth object of class \code{"moi.smooth"} or \code{"mod.smooth"}} + +\item{model}{an object of class \code{"gam"}} + +\item{n}{numeric; the number of points over the range of the covariate at +which to evaluate the smooth.} + +\item{n_3d, n_4d}{numeric; the number of points over the range of last +covariate in a 3D or 4D smooth. The default is \code{NULL} which achieves the +standard behaviour of using \code{n} points over the range of all covariate, +resulting in \code{n^d} evaluation points, where \code{d} is the dimension of the +smooth. For \code{d > 2} this can result in very many evaluation points and slow +performance. For smooths of \code{d > 4}, the value of \code{n_4d} will be used for +all dimensions \verb{> 4}, unless this is \code{NULL}, in which case the default +behaviour (using \code{n} for all dimensions) will be observed.} + +\item{data}{a data frame of covariate values at which to evaluate the +smooth.} + +\item{unconditional}{logical; should confidence intervals include the +uncertainty due to smoothness selection? If \code{TRUE}, the corrected Bayesian +covariance matrix will be used.} + +\item{overall_uncertainty}{logical; should the uncertainty in the model +constant term be included in the standard error of the evaluate values of +the smooth?} + +\item{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 \code{dist} is a distance within the unit square. See +\code{\link[mgcv:exclude.too.far]{mgcv::exclude.too.far()}} for further details.} + +\item{...}{ignored.} +} +\description{ +Evaluation of a monotonic functions in mvgam +These evaluation functions are needed so that gratia::draw methods work with mvgam +monotonic smooths +} diff --git a/man/monotonic.Rd b/man/monotonic.Rd index 3e254b3d..2a7f5074 100644 --- a/man/monotonic.Rd +++ b/man/monotonic.Rd @@ -6,8 +6,6 @@ \alias{smooth.construct.mod.smooth.spec} \alias{Predict.matrix.moi.smooth} \alias{Predict.matrix.mod.smooth} -\alias{eval_smooth.moi.smooth} -\alias{eval_smooth.mod.smooth} \title{Monotonic splines in mvgam} \usage{ \method{smooth.construct}{moi.smooth.spec}(object, data, knots) @@ -17,74 +15,17 @@ \method{Predict.matrix}{moi.smooth}(object, data) \method{Predict.matrix}{mod.smooth}(object, data) - -\method{eval_smooth}{moi.smooth}( - smooth, - model, - n = 100, - n_3d = NULL, - n_4d = NULL, - data = NULL, - unconditional = FALSE, - overall_uncertainty = TRUE, - dist = NULL, - ... -) - -\method{eval_smooth}{mod.smooth}( - smooth, - model, - n = 100, - n_3d = NULL, - n_4d = NULL, - data = NULL, - unconditional = FALSE, - overall_uncertainty = TRUE, - dist = NULL, - ... -) } \arguments{ \item{object}{A smooth specification object, usually generated by a term \code{s(x, bs = "moi", ...)} or \code{s(x, bs = "mod", ...)}} -\item{data}{a data frame of covariate values at which to evaluate the -smooth.} +\item{data}{a list containing just the data (including any \code{by} variable) required by this term, + with names corresponding to \code{object$term} (and \code{object$by}). The \code{by} variable + is the last element.} \item{knots}{a list containing any knots supplied for basis setup --- in same order and with same names as \code{data}. Can be \code{NULL}. See details for further information.} - -\item{smooth}{a smooth object of class \code{"moi.smooth"} or \code{"mod.smooth"}} - -\item{model}{an object of class \code{"gam"}} - -\item{n}{numeric; the number of points over the range of the covariate at -which to evaluate the smooth.} - -\item{n_3d, n_4d}{numeric; the number of points over the range of last -covariate in a 3D or 4D smooth. The default is \code{NULL} which achieves the -standard behaviour of using \code{n} points over the range of all covariate, -resulting in \code{n^d} evaluation points, where \code{d} is the dimension of the -smooth. For \code{d > 2} this can result in very many evaluation points and slow -performance. For smooths of \code{d > 4}, the value of \code{n_4d} will be used for -all dimensions \verb{> 4}, unless this is \code{NULL}, in which case the default -behaviour (using \code{n} for all dimensions) will be observed.} - -\item{unconditional}{logical; should confidence intervals include the -uncertainty due to smoothness selection? If \code{TRUE}, the corrected Bayesian -covariance matrix will be used.} - -\item{overall_uncertainty}{logical; should the uncertainty in the model -constant term be included in the standard error of the evaluate values of -the smooth?} - -\item{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 \code{dist} is a distance within the unit square. See -\code{\link[mgcv:exclude.too.far]{mgcv::exclude.too.far()}} for further details.} - -\item{...}{ignored.} } \value{ An object of class \code{"moi.smooth"} or \code{"mod.smooth"}. In addition to diff --git a/src/mvgam.dll b/src/mvgam.dll index e4171240..24fb715d 100644 Binary files a/src/mvgam.dll and b/src/mvgam.dll differ