diff --git a/R/DataSurvival.R b/R/DataSurvival.R index f4d1c16d..bc09a26c 100644 --- a/R/DataSurvival.R +++ b/R/DataSurvival.R @@ -125,7 +125,10 @@ as_stan_list.DataSurvival <- function(object, ...) { rownames(design_mat) <- NULL # Parameters for efficient integration of hazard function -> survival function - gh_parameters <- statmod::gauss.quad(n = 15, kind = "legendre") + gh_parameters <- statmod::gauss.quad( + n = getOption("jmpost.gauss_quad_n"), + kind = getOption("jmpost.gauss_quad_kind") + ) model_data <- list( Nind_dead = sum(df[[vars$event]]), diff --git a/R/settings.R b/R/settings.R index 9004a0d7..0ebf300f 100644 --- a/R/settings.R +++ b/R/settings.R @@ -30,6 +30,21 @@ #' Directory to store compiled stan models in. If not set, a temporary directory is used for #' the given R session. Can also be set via the environment variable `JMPOST_CACHE_DIR`. #' +#' +#' +#' ## `jmpost.gauss_quad_n` +#' +#' Default = 15 +#' +#' In most cases the survival function of the joint model does not have a closed form +#' and as such it is calculated by integrating the hazard function. `jmpost` estimates this +#' via Gaussian Quadrature, in particular it uses [`statmod::gauss.quad`] with +#' `kind = "legendre"` to create the nodes and weights. +#' +#' This option specifies the `n` argument in the call to [`statmod::gauss.quad`]. In general +#' higher values of `n` lead to better accuracy of the approximation but at the cost of +#' increased computational time. +#' #' @examples #' \dontrun{ #' options(jmpost.prior_shrinkage = 0.5) @@ -46,7 +61,8 @@ set_options <- function() { current_opts <- names(options()) jmpost_opts <- list( jmpost.cache_dir = cache_dir, - jmpost.prior_shrinkage = 0.5 + jmpost.prior_shrinkage = 0.5, + jmpost.gauss_quad_n = 15 ) for (opt in names(jmpost_opts)) { if (!opt %in% current_opts) { diff --git a/design/interfaces.md b/design/interfaces.md index bb11b630..9a4b7abe 100755 --- a/design/interfaces.md +++ b/design/interfaces.md @@ -11,7 +11,7 @@ matrix link_contribution(matrix time, matrix pars_lm) ``` - must return same dimensions as time -- where time is 1 row per subject (Nind) and 1 column per required timepoint (nodes from the guasian quadrature) (i.e. if multiple timepoints need to be evaluated for each subject it will still be 1 row but with multiple columns, if only 1 timepoint needs to be evaluated then there will only be 1 column) +- where time is 1 row per subject (Nind) and 1 column per required timepoint (nodes from the Gaussian quadrature) (i.e. if multiple timepoints need to be evaluated for each subject it will still be 1 row but with multiple columns, if only 1 timepoint needs to be evaluated then there will only be 1 column) - pars_lm is a matrix with 1 row per subject and as many columns as you need defined in transformed_parameters() diff --git a/inst/WORDLIST b/inst/WORDLIST index 9cb3c337..c6450d65 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -36,7 +36,7 @@ frac funder gi gl -guasian +Gaussian hardcoded ie ig diff --git a/man/jmpost-settings.Rd b/man/jmpost-settings.Rd index b9d11ee3..84a13b4b 100644 --- a/man/jmpost-settings.Rd +++ b/man/jmpost-settings.Rd @@ -35,6 +35,20 @@ Default = \code{tempfile()} Directory to store compiled stan models in. If not set, a temporary directory is used for the given R session. Can also be set via the environment variable \code{JMPOST_CACHE_DIR}. } + +\subsection{\code{jmpost.gauss_quad_n}}{ + +Default = 15 + +In most cases the survival function of the joint model does not have a closed form +and as such it is calculated by integrating the hazard function. \code{jmpost} estimates this +via Gaussian Quadrature, in particular it uses \code{\link[statmod:gauss.quad]{statmod::gauss.quad}} with +\code{kind = "legendre"} to create the nodes and weights. + +This option specifies the \code{n} argument in the call to \code{\link[statmod:gauss.quad]{statmod::gauss.quad}}. In general +higher values of \code{n} lead to better accuracy of the approximation but at the cost of +increased computational time. +} } \examples{ \dontrun{ diff --git a/tests/testthat/test-options.R b/tests/testthat/test-options.R new file mode 100644 index 00000000..3df6b48f --- /dev/null +++ b/tests/testthat/test-options.R @@ -0,0 +1,43 @@ + + + + +test_that("Can alter Gaussian Quadrature arguments", { + x <- data.frame( + vpt = c("b", "a", "c", "d", "e"), + vtime = c(10, 20, 30, 25, 15), + vevent = c(1, 1, 0, 1, 0), + vcov1 = c("A", "A", "B", "B", "A"), + vcov2 = rnorm(5) + ) + + ## Test defaults 15 + "legendre" + df <- DataSurvival( + data = x, + formula = Surv(vtime, vevent) ~ vcov1 * vcov2 + ) + li <- as.list(df) + expect_equal(li$n_nodes, 15) + expect_equal( + li[c("nodes", "weights")], + statmod::gauss.quad(15, "legendre") + ) + + + ## Test modified values + options("jmpost.gauss_quad_n" = 20) + df <- DataSurvival( + data = x, + formula = Surv(vtime, vevent) ~ vcov1 * vcov2 + ) + li <- as.list(df) + expect_equal(li$n_nodes, 20) + expect_equal( + li[c("nodes", "weights")], + statmod::gauss.quad(20, "legendre") + ) + + + ## Reset back to default to not impact additional tests + options("jmpost.gauss_quad_n" = 15) +})