EpiNow2 Stan Functions
secondary.stan
Go to the documentation of this file.
1/**
2 * Secondary Reports Functions
3 *
4 * This group of functions handles the calculation and manipulation of secondary
5 * reports in the model. Secondary reports are derived from primary reports through
6 * various transformations and can represent different epidemiological quantities.
7 *
8 * @ingroup secondary_reports_estimation
9 */
10
11/**
12 * @ingroup secondary_reports_estimation
13 * @brief Calculate secondary reports from primary reports
14 *
15 * This function calculates secondary reports based on primary reports,
16 * considering various options for how to combine historical and current data.
17 * It supports both cumulative and non-cumulative targets, and additive or
18 * subtractive relationships between primary and secondary reports.
19 *
20 * @param scaled_reports Vector of scaled primary reports
21 * @param conv_reports Vector of convolved primary reports
22 * @param obs Array of observed secondary reports
23 * @param cumulative Whether to use cumulative target (1) or not (0)
24 * @param historic Whether to use historical primary reports (1) or not (0)
25 * @param primary_hist_additive Whether historical primary reports are additive (1) or subtractive (0)
26 * @param current Whether to use current primary reports (1) or not (0)
27 * @param primary_current_additive Whether current primary reports are additive (1) or subtractive (0)
28 * @param predict Number of time points to predict
29 * @return A vector of secondary reports
30 *
31 * @par Example
32 * With `scaled_reports = {5, 6, 7, 8}`, `conv_reports = {2, 2, 2, 2}`,
33 * `cumulative = 0`, `historic = 1`, `primary_hist_additive = 1`,
34 * `current = 1` and `primary_current_additive = 1`, each time point adds
35 * both the historic and current primary contributions, giving
36 * `{7, 8, 9, 10}` (up to the `1e-6` numerical floor added to every entry).
37 */
39 vector scaled_reports, vector conv_reports, array[] int obs,
40 int cumulative, int historic, int primary_hist_additive,
41 int current, int primary_current_additive, int predict
42) {
43 int t = num_elements(scaled_reports);
44 vector[t] secondary_reports = rep_vector(0.0, t);
45 // if predicting and using a cumulative target
46 // combine reports with previous secondary data
47 for (i in 1:t) {
48 // update cumulative target
49 if (cumulative && i > 1) {
50 if (i > predict) {
51 secondary_reports[i] = secondary_reports[i - 1];
52 } else{
53 secondary_reports[i] = obs[i - 1];
54 }
55 }
56 // update based on previous primary reports
57 if (historic) {
58 if (primary_hist_additive) {
59 secondary_reports[i] += conv_reports[i];
60 } else{
61 secondary_reports[i] = fmax(0, secondary_reports[i] - conv_reports[i]);
62 }
63 }
64 // update based on current primary reports
65 if (current) {
66 if (primary_current_additive) {
67 secondary_reports[i] += scaled_reports[i];
68 } else{
69 secondary_reports[i] -= scaled_reports[i];
70 }
71 }
72 secondary_reports[i] = 1e-6 + secondary_reports[i];
73 }
74 return(secondary_reports);
75}
vector calculate_secondary(vector scaled_reports, vector conv_reports, array[] int obs, int cumulative, int historic, int primary_hist_additive, int current, int primary_current_additive, int predict)
Calculate secondary reports from primary reports.