diff --git a/DESCRIPTION b/DESCRIPTION index cff59d90..37e6ee08 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -88,6 +88,7 @@ Collate: 'defaults.R' 'external-exports.R' 'jmpost-package.R' + 'settings.R' 'simulations.R' 'simulations_gsf.R' 'simulations_os.R' diff --git a/R/Prior.R b/R/Prior.R index 8ef9f09a..0d00d186 100755 --- a/R/Prior.R +++ b/R/Prior.R @@ -188,7 +188,8 @@ NULL #' @describeIn Prior-Getter-Methods The prior's initial value #' @export initialValues.Prior <- function(object, ...) { - 0.5 * object@init + 0.5 * object@sample(1) + getOption("jmpost.prior_shrinkage") * object@init + + (1 - getOption("jmpost.prior_shrinkage")) * object@sample(1) } diff --git a/R/settings.R b/R/settings.R new file mode 100644 index 00000000..099acb9f --- /dev/null +++ b/R/settings.R @@ -0,0 +1,35 @@ + + + +#' jmpost settings +#' +#' @description +#' Define settings that modify the behaviour of the `jmpost` package +#' +#' Each of the following are the name of options that can be set via: +#' ``` +#' options( = ) +#' ``` +#' +#' ## `jmpost.prior_shrinkage` +#' +#' Default = `0.5` +#' +#' By default all initial values are drawn as random sample from the respective prior +#' distribution with a shrinkage factor towards the mean. That is: +#' ``` +#' initial_value = prior_sample * prior_shrinkage + (1 - prior_shrinkage) * prior_mean +#' ``` +#' This setting controls the shrinkage factor. A value of 0 means no shrinkage (i.e. +#' pure random draw) whilst a value of 1 means the intial value is just the mean. +#' +#' @examples +#' \dontrun{ +#' options(jmpost.prior_shrinkage = 0.5) +#' } +#' @name jmpost-settings +NULL + +if (is.null(getOption("jmpost.prior_shrinkage", default = NULL))) { + options("jmpost.prior_shrinkage" = 0.5) +} diff --git a/man/jmpost-settings.Rd b/man/jmpost-settings.Rd new file mode 100644 index 00000000..19c31663 --- /dev/null +++ b/man/jmpost-settings.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/settings.R +\name{jmpost-settings} +\alias{jmpost-settings} +\title{jmpost settings} +\description{ +Define settings that modify the behaviour of the \code{jmpost} package + +Each of the following are the name of options that can be set via: + +\if{html}{\out{
}}\preformatted{options( = ) +}\if{html}{\out{
}} +\subsection{\code{jmpost.prior_shrinkage}}{ + +Default = \code{0.5} + +By default all initial values are drawn as random sample from the respective prior +distribution with a shrinkage factor towards the mean. That is: + +\if{html}{\out{
}}\preformatted{initial_value = prior_sample * prior_shrinkage + (1 - prior_shrinkage) * prior_mean +}\if{html}{\out{
}} + +This setting controls the shrinkage factor. A value of 0 means no shrinkage (i.e. +pure random draw) whilst a value of 1 means the intial value is just the mean. +} +} +\examples{ +\dontrun{ +options(jmpost.prior_shrinkage = 0.5) +} +} diff --git a/tests/testthat/test-Prior.R b/tests/testthat/test-Prior.R index e5536f34..859f9223 100644 --- a/tests/testthat/test-Prior.R +++ b/tests/testthat/test-Prior.R @@ -156,3 +156,33 @@ test_that("show() works for Prior objects", { expect_snapshot(print(prior_loglogistic(1, 2))) expect_snapshot(print(prior_invgamma(alpha = 1, beta = 2))) }) + + +test_that("jmpost.prior_shrinkage works as expected", { + x <- prior_normal(1, 2) + with_mocked_bindings( + { + options("jmpost.prior_shrinkage" = 0.5) + expect_equal( + initialValues(x), + 1 * 0.5 + 4 * 0.5 + ) + + options("jmpost.prior_shrinkage" = 0.9) + expect_equal( + initialValues(x), + 1 * 0.9 + 4 * 0.1 + ) + + options("jmpost.prior_shrinkage" = 0.1) + expect_equal( + initialValues(x), + 1 * 0.1 + 4 * 0.9 + ) + + ## Reset Shrinkage factor + options("jmpost.prior_shrinkage" = 0.1) + }, + local_rnorm = \(...) 4 + ) +})