Skip to content

Commit

Permalink
Added option to change shrinkage factor
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc committed Feb 5, 2024
1 parent fba92f8 commit af2518a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Collate:
'defaults.R'
'external-exports.R'
'jmpost-package.R'
'settings.R'
'simulations.R'
'simulations_gsf.R'
'simulations_os.R'
Expand Down
3 changes: 2 additions & 1 deletion R/Prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down
35 changes: 35 additions & 0 deletions R/settings.R
Original file line number Diff line number Diff line change
@@ -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(<option_name> = <value>)
#' ```
#'
#' ## `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)
}
31 changes: 31 additions & 0 deletions man/jmpost-settings.Rd

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

30 changes: 30 additions & 0 deletions tests/testthat/test-Prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})

0 comments on commit af2518a

Please sign in to comment.