Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature expectation reporting #154

Merged
merged 12 commits into from
Sep 1, 2022
Prev Previous commit
Next Next commit
debug implementation
  • Loading branch information
seabbs committed Aug 5, 2022
commit a87f63d14b4882900a3b92513a0de15a92e8141e
8 changes: 5 additions & 3 deletions R/model-modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ enw_report <- function(non_parametric = ~0, structural = ~0, data) {
#' multiplying a probability mass function by some fraction) to account
#' ascertainment etc.
#'
#' @param ... Additional parameters passed to [enw_add_metaobs_features()]. The
#' same arguments as passed to `enw_preprocess_data()` should be used here.
#' @inherit enw_report return
#' @inheritParams enw_obs
#' @family modelmodules
Expand Down Expand Up @@ -312,7 +314,7 @@ enw_expectation <- function(r = ~ rw(day, by = .group), generation_time = 1,
0, 1
)
# Observation formula
obs_form <- enw_formula(osbervation, data$metareference[[1]], sparse = FALSE)
obs_form <- enw_formula(observation, data$metareference[[1]], sparse = FALSE)
obs_data <- enw_formula_as_data_list(
obs_form,
prefix = "expo", drop_intercept = TRUE
Expand All @@ -324,7 +326,7 @@ enw_expectation <- function(r = ~ rw(day, by = .group), generation_time = 1,

names(r_list) <- paste0("expr_", names(r_list))
names(obs_list) <- paste0("expo_", names(obs_list))
out$data <- c(r_list, r_data, obs_data)
out$data <- c(r_list, r_data, obs_list, obs_data)

out$priors <- data.table::data.table(
variable = c(
Expand Down Expand Up @@ -364,7 +366,7 @@ enw_expectation <- function(r = ~ rw(day, by = .group), generation_time = 1,
1, priors$expr_r_int[1], priors$expr_r_int[2] * 0.1
),
expo_beta = numeric(0),
expo_beta_sd = numeric(0),
expo_beta_sd = numeric(0)
)
if (data$expr_fncol > 0) {
init$expr_beta <- rnorm(data$expr_fncol, 0, 0.01)
Expand Down
2 changes: 1 addition & 1 deletion R/preprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enw_add_metaobs_features <- function(metaobs, holidays = c(),
#' @description FUNCTION_DESCRIPTION
#' @param metaobs PARAM_DESCRIPTION
#' @param days
#' @param position Should new dates be added at the begining or end of
#' @param direction Should new dates be added at the begining or end of
#' the data. Default is "end" with "start" also available
#' @return OUTPUT_DESCRIPTION
#' @family preprocess
Expand Down
8 changes: 7 additions & 1 deletion inst/examples/germany_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ pobs <- enw_preprocess_data(retro_nat_germany, max_delay = 20)
# Fit the default nowcast model and produce a nowcast
# Note that we have reduced samples for this example to reduce runtimes
nowcast <- epinowcast(pobs,
expectation = enw_expectation(~1, data = pobs),
expectation = enw_expectation(
~ rw(week),
generation_time = c(0.2, 0.6, 0.2),
observation = ~ (1 | day_of_week),
latent_reporting_delay = c(0.3, 0.2, 0.1),
data = pobs
),
fit = enw_fit_opts(
save_warmup = FALSE, pp = TRUE,
chains = 2, iter_warmup = 500, iter_sampling = 500
Expand Down
4 changes: 2 additions & 2 deletions inst/stan/functions/log_expected_obs_from_latent_obs.stan
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
array[g] vector[t] exp_lobs;
for (k in 1:g) {
if (rd_n == 1) {
exp_lobs[k] = exp_latent_lobs[k] + lrrd;
exp_lobs[k] = exp_latent_lobs[k] + rep_vector(lrrd[1], t);
}else{
for (i in 1:t){
exp_lobs[k][i] =
log_sum_exp(
segment(exp_latent_lobs[k], rd_n + i, rd_n) + lrrd
segment(exp_latent_lobs[k], i, rd_n) + lrrd
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions man/enw_expectation.Rd

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

2 changes: 1 addition & 1 deletion man/enw_extend_date.Rd

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

2 changes: 1 addition & 1 deletion man/epinowcast.Rd

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