Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from epiforecasts/feature_sbc
Feature: Add SBC quantile coverage based workflo
- Loading branch information
Showing
23 changed files
with
219 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
forecast_data <- function(data, strains, model, ...) { | ||
inits <- forecast.vocs::fv_inits(data, strains = strains) | ||
|
||
fit <- forecast.vocs::fv_sample(data, init = inits, model = model, ...) | ||
|
||
posterior <- forecast.vocs::fv_posterior(fit) | ||
return(posterior) | ||
} | ||
|
||
coverage <- function(value, lower, upper) { | ||
mean(lower < value & value < upper) | ||
} | ||
|
||
sbc_coverage <- function(sbc, by = c("strains", "overdispersion", | ||
"variant_relationship")) { | ||
by_with_id <- c(by, "dataset") | ||
parameters <- sbc[, rbindlist(sbc$parameters), by = by_with_id] | ||
setnames(parameters, "parameter", "variable") | ||
posterior <- sbc[, rbindlist(forecast), by = by_with_id] | ||
sbc_unnest <- merge(posterior, parameters, by = c(by_with_id, "parameter")) | ||
sbc_unnest[, | ||
.( | ||
coverage_10 = coverage(sample, q45, q55), | ||
coverage_20 = coverage(sample, q40, q60), | ||
coverage_30 = coverage(sample, q35, q65), | ||
coverage_40 = coverage(sample, q30, q70), | ||
coverage_50 = coverage(sample, q25, q75), | ||
coverage_60 = coverage(sample, q20, q80), | ||
coverage_70 = coverage(sample, q15, q85), | ||
coverage_80 = coverage(sample, q10, q90), | ||
coverage_90 = coverage(sample, q5, q95), | ||
coverage_95 = coverage(sample, q2.5, q97.5), | ||
), | ||
by = c("parameter", by) | ||
] | ||
sbc_melt <- melt( | ||
sbc_unnest, measure.vars = patterns("coverage"), | ||
variable.name = "target", value.name = "actual" | ||
) | ||
sbc_melt[, target := gsub("coverage_", "", target)] | ||
sbc_melt[, | ||
`:=`(target = as.numeric(target), | ||
actual = round(actual * 100, 1) | ||
) | ||
] | ||
return(sbc_melt[]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
sbc_targets <- list( | ||
tar_target( | ||
sbc_one_prior_simulations, | ||
do.call( | ||
generate_obs, | ||
c( | ||
retro_args, | ||
list( | ||
obs = retro_validation_obs, | ||
datasets = sbc_datasets, | ||
strains = 1, | ||
model = single_model, | ||
overdispersion = overdispersion_scenarios | ||
) | ||
) | ||
)[, `:=`( | ||
strains = 1, | ||
overdispersion = overdispersion_scenarios | ||
) | ||
], | ||
cross(retro_validation_obs, overdispersion_scenarios) | ||
), | ||
tar_target( | ||
sbc_two_prior_simulations, | ||
do.call( | ||
generate_obs, | ||
c( | ||
retro_args, | ||
list( | ||
obs = retro_validation_obs, | ||
datasets = sbc_datasets, | ||
strains = 2, | ||
model = two_model, | ||
overdispersion = overdispersion_scenarios, | ||
variant_relationship = variant_relationship_scenarios | ||
) | ||
) | ||
)[, `:=`( | ||
strains = 2, | ||
overdispersion = overdispersion_scenarios, | ||
variant_relationship = variant_relationship_scenarios | ||
) | ||
], | ||
cross(retro_validation_obs, variant_relationship_scenarios, | ||
overdispersion_scenarios) | ||
), | ||
tar_target( | ||
sbc_one_posteriors, | ||
sbc_one_prior_simulations[, | ||
forecast := list(do.call( | ||
forecast_data, | ||
c( | ||
stan_args, | ||
list( | ||
data = data[[1]], | ||
strains = 1, | ||
model = one_model | ||
) | ||
), | ||
))], | ||
map(sbc_one_prior_simulations) | ||
), | ||
tar_target( | ||
sbc_two_posteriors, | ||
sbc_two_prior_simulations[, | ||
forecast := list(do.call( | ||
forecast_data, | ||
c( | ||
stan_args, | ||
list( | ||
data = data[[1]], | ||
strains = 2, | ||
model = two_model | ||
) | ||
), | ||
))], | ||
map(sbc_two_prior_simulations) | ||
), | ||
# calculate coverage for one and two strain models across simulations | ||
tar_target( | ||
one_strain_coverage, | ||
sbc_coverage(sbc_one_posteriors, by = c("strains", "overdispersion")) | ||
), | ||
tar_target( | ||
two_strain_coverage, | ||
sbc_coverage(sbc_two_posteriors, by = c("strains", "overdispersion", | ||
"variant_relationship")) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.