Skip to content

Commit

Permalink
make it possible to either use pop out of sample or fit with it in sa…
Browse files Browse the repository at this point in the history
…mple
  • Loading branch information
seabbs committed Jan 10, 2025
1 parent 07064bd commit 3bc26d4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL,
breakpoints = breakpoints,
future_fixed = as.numeric(future_rt$fixed),
fixed_from = future_rt$from,
use_pop = as.integer(rt$pop != Fixed(0)),
use_pop = as.integer(rt$pop != Fixed(0)) + as.integer(rt$estimate_pop),
stationary = as.numeric(rt$gp_on == "R0"),
future_time = horizon - future_rt$from
)
Expand Down
27 changes: 21 additions & 6 deletions R/opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ trunc_opts <- function(dist = Fixed(0), default_cdf_cutoff = 0.001,
#' Used to adjust Rt estimates based on the proportion of the population that
#' is susceptible. Defaults to `Fixed(0)` which means no population adjustment
#' is done.
#'

Check warning on line 327 in R/opts.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/opts.R,line=327,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @param pop_within_horizon Logical, defaults to `FALSE`. Should the
#' susceptible population adjustment be applied within the time horizon of data
#' or just beyond it. Note that if `pop_within_horizon = TRUE` the Rt estimate
#' will be unadjusted for susceptible depletion but the resulting posterior
#' predictions for infections and cases will be adjusted for susceptible
#' depletion.
#'
#' @param gp_on Character string, defaulting to "R_t-1". Indicates how the
#' Gaussian process, if in use, should be applied to Rt. Currently supported
Expand Down Expand Up @@ -354,7 +361,8 @@ rt_opts <- function(prior = LogNormal(mean = 1, sd = 1),
use_breakpoints = TRUE,
future = "latest",
gp_on = c("R_t-1", "R0"),
pop = Fixed(0)) {
pop = Fixed(0),
pop_within_horizon = FALSE) {
rt <- list(
use_rt = use_rt,
rw = rw,
Expand Down Expand Up @@ -388,15 +396,22 @@ rt_opts <- function(prior = LogNormal(mean = 1, sd = 1),
}

if (is.numeric(pop)) {
cli_warn(
c(
"!" = "Specifying {.var pop} as a numeric value is deprecated.",
"i" = "Use a {.cls dist_spec} instead, e.g. Fixed({pop})."
)
lifecycle::deprecate_warn(

Check warning on line 399 in R/opts.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/opts.R,line=399,col=31,[trailing_whitespace_linter] Trailing whitespace is superfluous.
"1.7.0",

Check warning on line 400 in R/opts.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/opts.R,line=400,col=15,[trailing_whitespace_linter] Trailing whitespace is superfluous.
"rt_opts(pop = 'must be a `<dist_spec>`')",

Check warning on line 401 in R/opts.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/opts.R,line=401,col=50,[trailing_whitespace_linter] Trailing whitespace is superfluous.
details = "For specifying a fixed population size, use `Fixed(pop)`"
)
pop <- Fixed(pop)
}
rt$pop <- pop
if (isTRUE(pop_within_horizon) && pop == Fixed(0)) {
cli_abort(
c(
"!" = "pop_within_horizon = TRUE but pop is fixed at 0."
)
)
}
rt$estimate_pop <- TRUE

if (rt$use_rt) {
rt$prior <- prior
Expand Down
2 changes: 1 addition & 1 deletion inst/stan/functions/infections.stan
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ vector generate_infections(vector oR, int uot, vector gt_rev_pmf,
// iteratively update infections
for (s in 1:ot) {
infectiousness[s] = update_infectiousness(infections, gt_rev_pmf, uot, s);
if (use_pop && s > nht) {
if (use_pop == 2 && s > nht) {
exp_adj_Rt = exp(-R[s] * infectiousness[s] / (pop - cum_infections[nht]));
exp_adj_Rt = exp_adj_Rt > 1 ? 1 : exp_adj_Rt;
infections[s + uot] = (pop - cum_infections[s]) * (1 - exp_adj_Rt);
Expand Down
10 changes: 9 additions & 1 deletion man/rt_opts.Rd

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

0 comments on commit 3bc26d4

Please sign in to comment.