Skip to content

Commit 81258af

Browse files
committed
fix_dist -> fix_parameters
1 parent a3a06df commit 81258af

16 files changed

+76
-42
lines changed

NAMESPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ S3method("+",dist_spec)
44
S3method(c,dist_spec)
55
S3method(discretise,dist_spec)
66
S3method(discretise,multi_dist_spec)
7-
S3method(fix_dist,dist_spec)
8-
S3method(fix_dist,multi_dist_spec)
7+
S3method(fix_parameters,dist_spec)
8+
S3method(fix_parameters,multi_dist_spec)
99
S3method(is_constrained,dist_spec)
1010
S3method(is_constrained,multi_dist_spec)
1111
S3method(max,dist_spec)
@@ -57,7 +57,7 @@ export(extract_CrIs)
5757
export(extract_inits)
5858
export(extract_samples)
5959
export(extract_stan_param)
60-
export(fix_dist)
60+
export(fix_parameters)
6161
export(forecast_infections)
6262
export(forecast_secondary)
6363
export(gamma_dist_def)

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
- The interface for defining delay distributions has been generalised to also cater for continuous distributions
1212
- When defining probability distributions these can now be truncated using the `tolerance` argument
1313

14+
## Package changes
15+
16+
- `fix_dist()` has been renamed to `fix_parameters()`. By @sbfnk in and reviewed by.
17+
1418
## Bug fixes
1519

1620
- a bug was fixed that caused delay option functions to report an error if only the tolerance was specified. By @sbfnk in #716 and reviewed by @jamesmbaazam.

R/create.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ create_stan_delays <- function(..., time_points = 1L) {
742742
## discretise
743743
delays <- map(delays, discretise, strict = FALSE)
744744
## get maximum delays
745-
bounded_delays <- map(delays, function(x) discretise(fix_dist(x)))
745+
bounded_delays <- map(delays, function(x) discretise(fix_parameters(x)))
746746
max_delay <- unname(as.numeric(flatten(map(bounded_delays, max))))
747747
## number of different non-empty types
748748
type_n <- vapply(delays, ndist, integer(1))

R/deprecated.R

+21-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ adjust_infection_to_report <- function(infections, delay_defs,
6767
#' probability mass function of the delay (starting with 0); defaults to an
6868
#' empty vector corresponding to a parametric specification of the distribution
6969
#' (using \code{params_mean}, and \code{params_sd}.
70-
#' @param fixed Deprecated, use [fix_dist()] instead.
70+
#' @param fixed Deprecated, use [fix_parameters()] instead.
7171
#' @return A list of distribution options.
7272
#' @importFrom rlang warn arg_match
7373
#' @keywords internal
@@ -618,3 +618,23 @@ apply_tolerance <- function(x, tolerance) {
618618
attributes(y) <- attributes(x)
619619
return(y)
620620
}
621+
622+
#' Fix the parameters of a `<dist_spec>`
623+
#'
624+
#' @description `r lifecycle::badge("deprecated")`
625+
#' This function has been renamed to [fix_parameters()].
626+
#' @return A `<dist_spec>` object without uncertainty
627+
#' @keywords internal
628+
#' @param x A `<dist_spec>`
629+
#' @param strategy Character; either "mean" (use the mean estimates of the
630+
#' mean and standard deviation) or "sample" (randomly sample mean and
631+
#' standard deviation from uncertainty given in the `<dist_spec>`
632+
fix_parameters <- function(x, strategy = c("mean", "sample")) {
633+
lifecycle::deprecate_warn(
634+
"1.6.0", "fix_parameters()", "fix_parameters()"
635+
)
636+
if (!is(x, "dist_spec")) {
637+
stop("Can only fix distributions in a <dist_spec>.")
638+
}
639+
fix_parameters(x, strategy)
640+
}

R/dist_spec.R

+11-10
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ plot.dist_spec <- function(x, samples = 50L, res = 1, cumulative = TRUE, ...) {
618618
samples <- 1 ## only need 1 sample if fixed
619619
}
620620
dists <- lapply(seq_len(samples), function(y) {
621-
fix_dist(extract_single_dist(x, i), strategy = "sample")
621+
fix_parameters(extract_single_dist(x, i), strategy = "sample")
622622
})
623623
tolerance <- attr(x, "tolerance")
624624
if (is.null(tolerance)) {
@@ -703,12 +703,12 @@ extract_single_dist <- function(x, i) {
703703
}
704704

705705
#' @export
706-
fix_dist <- function(x, ...) {
707-
UseMethod("fix_dist")
706+
fix_parameters <- function(x, ...) {
707+
UseMethod("fix_parameters")
708708
}
709709
#' Fix the parameters of a `<dist_spec>`
710710
#'
711-
#' @name fix_dist
711+
#' @name fix_parameters
712712
#' @description `r lifecycle::badge("experimental")`
713713
#' If the given `<dist_spec>` has any uncertainty, it is removed and the
714714
#' corresponding distribution converted into a fixed one.
@@ -721,15 +721,15 @@ fix_dist <- function(x, ...) {
721721
#' @param ... ignored
722722
#' @importFrom truncnorm rtruncnorm
723723
#' @importFrom rlang arg_match
724-
#' @method fix_dist dist_spec
724+
#' @method fix_parameters dist_spec
725725
#' @examples
726726
#' # An uncertain gamma distribution with mean 3 and sd 2
727727
#' dist <- LogNormal(
728728
#' meanlog = Normal(3, 0.5), sdlog = Normal(2, 0.5), max = 20
729729
#' )
730730
#'
731-
#' fix_dist(dist)
732-
fix_dist.dist_spec <- function(x, strategy = c("mean", "sample"), ...) {
731+
#' fix_parameters(dist)
732+
fix_parameters.dist_spec <- function(x, strategy = c("mean", "sample"), ...) {
733733
## match strategy argument to options
734734
strategy <- arg_match(strategy)
735735

@@ -758,10 +758,11 @@ fix_dist.dist_spec <- function(x, strategy = c("mean", "sample"), ...) {
758758
}
759759

760760
#' @export
761-
#' @method fix_dist multi_dist_spec
762-
fix_dist.multi_dist_spec <- function(x, strategy = c("mean", "sample"), ...) {
761+
#' @method fix_parameters multi_dist_spec
762+
fix_parameters.multi_dist_spec <- function(x, strategy =
763+
c("mean", "sample"), ...) {
763764
for (i in seq_len(ndist(x))) {
764-
x[[i]] <- fix_dist(x[[i]])
765+
x[[i]] <- fix_parameters(x[[i]])
765766
}
766767
return(x)
767768
}

R/simulate_infections.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
#' R = R,
5454
#' initial_infections = 100,
5555
#' generation_time = generation_time_opts(
56-
#' fix_dist(example_generation_time)
56+
#' fix_parameters(example_generation_time)
5757
#' ),
58-
#' delays = delay_opts(fix_dist(example_reporting_delay)),
58+
#' delays = delay_opts(fix_parameters(example_reporting_delay)),
5959
#' obs = obs_opts(family = "poisson")
6060
#' )
6161
#' }
@@ -135,7 +135,7 @@ simulate_infections <- function(estimates, R, initial_infections,
135135

136136
if (length(data$delay_params_sd) > 0 && any(data$delay_params_sd > 0)) {
137137
stop(
138-
"Cannot simulate from uncertain parameters. Use the [fix_dist()] ",
138+
"Cannot simulate from uncertain parameters. Use the [fix_parameters()] ",
139139
"function to set the parameters of uncertain distributions either the ",
140140
"mean or a randomly sampled value"
141141
)

R/simulate_secondary.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#' cases <- as.data.table(example_confirmed)[, primary := confirm]
2929
#' sim <- simulate_secondary(
3030
#' cases,
31-
#' delays = delay_opts(fix_dist(example_reporting_delay)),
31+
#' delays = delay_opts(fix_parameters(example_reporting_delay)),
3232
#' obs = obs_opts(family = "poisson")
3333
#' )
3434
#' }
@@ -77,7 +77,7 @@ simulate_secondary <- function(primary,
7777

7878
if (length(data$delay_params_sd) > 0 && any(data$delay_params_sd > 0)) {
7979
stop(
80-
"Cannot simulate from uncertain parameters. Use the [fix_dist()] ",
80+
"Cannot simulate from uncertain parameters. Use the [fix_parameters()] ",
8181
"function to set the parameters of uncertain distributions either the ",
8282
"mean or a randomly sampled value"
8383
)

_pkgdown.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ reference:
108108
- bound_dist
109109
- collapse
110110
- discretise
111-
- fix_dist
111+
- fix_parameters
112+
- fix_parameters
112113
- get_parameters
113114
- get_pmf
114115
- get_distribution

man/dist_spec.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fix_dist.Rd renamed to man/fix_parameters.Rd

+16-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_infections.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simulate_secondary.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-dist_spec.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test_that("dist_spec returns correct output for gamma distribution parameterised
3333

3434
test_that("dist_spec returns correct output for fixed distribution", {
3535
result <- discretise(
36-
fix_dist(LogNormal(meanlog = Normal(5, 3), sdlog = 1, max = 19))
36+
fix_parameters(LogNormal(meanlog = Normal(5, 3), sdlog = 1, max = 19))
3737
)
3838
expect_equal(get_distribution(result), "nonparametric")
3939
expect_equal(max(result), 19)
@@ -201,11 +201,11 @@ test_that("plot.dist_spec correctly plots a combination of fixed distributions",
201201
expect_equal(length(plot$facet$params$facets), 1)
202202
})
203203

204-
test_that("fix_dist works with composite delay distributions", {
204+
test_that("fix_parameters works with composite delay distributions", {
205205
dist1 <- LogNormal(meanlog = Normal(1, 0.1), sdlog = 1, max = 19)
206206
dist2 <- Gamma(mean = 3, sd = 2, max = 19)
207207
dist <- dist1 + dist2
208-
expect_equal(ndist(collapse(discretise(fix_dist(dist)))), 1L)
208+
expect_equal(ndist(collapse(discretise(fix_parameters(dist)))), 1L)
209209
})
210210

211211
test_that("composite delay distributions can be disassembled", {

tests/testthat/test-simulate-infections.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ test_that("simulate_infections works as expected with standard parameters", {
2828
test_that("simulate_infections works as expected with additional parameters", {
2929
set.seed(123)
3030
sim <- test_simulate_infections(
31-
generation_time = gt_opts(fix_dist(example_generation_time)),
32-
delays = delay_opts(fix_dist(example_reporting_delay)),
31+
generation_time = gt_opts(fix_parameters(example_generation_time)),
32+
delays = delay_opts(fix_parameters(example_reporting_delay)),
3333
obs = obs_opts(family = "negbin", phi = list(mean = 0.5, sd = 0)),
3434
seeding_time = 10
3535
)

tests/testthat/test-simulate-secondary.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test_that("simulate_secondary works as expected with standard parameters", {
2121
test_that("simulate_secondary works as expected with additional parameters", {
2222
set.seed(123)
2323
sim <- test_simulate_secondary(
24-
delays = delay_opts(fix_dist(example_reporting_delay)),
24+
delays = delay_opts(fix_parameters(example_reporting_delay)),
2525
obs = obs_opts(family = "negbin", phi = list(mean = 0.5, sd = 0))
2626
)
2727
expect_equal(nrow(sim), nrow(cases))

touchstone/setup.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ library("EpiNow2")
22

33
reported_cases <- example_confirmed[1:60]
44

5-
fixed_generation_time <- fix_dist(example_generation_time)
6-
fixed_incubation_period <- fix_dist(example_incubation_period)
7-
fixed_reporting_delay <- fix_dist(example_reporting_delay)
5+
fixed_generation_time <- fix_parameters(example_generation_time)
6+
fixed_incubation_period <- fix_parameters(example_incubation_period)
7+
fixed_reporting_delay <- fix_parameters(example_reporting_delay)
88

99
delays <- delay_opts(example_incubation_period + example_reporting_delay)
1010
fixed_delays <- delay_opts(fixed_incubation_period + fixed_reporting_delay)

0 commit comments

Comments
 (0)