EpiNow2 Stan Functions
generated_quantities.stan
Go to the documentation of this file.
1/**
2 * Generated Quantities Functions
3 *
4 * Functions for calculating additional quantities from model outputs.
5 */
6
7/**
8 * Calculate Rt directly from inferred infections
9 *
10 * This function estimates the reproduction number (Rt) using the Cori et al. approach,
11 * directly from a time series of infections. Optionally applies smoothing.
12 *
13 * @param infections Vector of infection counts
14 * @param seeding_time Number of time steps used for seeding
15 * @param gt_rev_pmf Vector of reversed generation time PMF
16 * @param smooth Number of time steps to use for smoothing (0 for no smoothing)
17 * @return A vector of reproduction numbers (Rt)
18 *
19 * @ingroup rt_estimation
20 */
21vector calculate_Rt(vector infections, int seeding_time,
22 vector gt_rev_pmf, int smooth) {
23 int t = num_elements(infections);
24 int ot = t - seeding_time;
25 vector[ot] R;
26 vector[ot] sR;
27 vector[ot] infectiousness = rep_vector(1e-5, ot);
28 // calculate Rt using Cori et al. approach
29 for (s in 1:ot) {
30 infectiousness[s] += update_infectiousness(
31 infections, gt_rev_pmf, seeding_time, s
32 );
33 R[s] = infections[s + seeding_time] / infectiousness[s];
34 }
35 if (smooth) {
36 for (s in 1:ot) {
37 real window = 0;
38 sR[s] = 0;
39 for (i in max(1, s - smooth):min(ot, s + smooth)) {
40 sR[s] += R[i];
41 window += 1;
42 }
43 sR[s] = sR[s] / window;
44 }
45 } else{
46 sR = R;
47 }
48 return(sR);
49}
50
51/**
52 * Calculate growth rate
53 *
54 * This function calculates the growth rate from a time series of infections
55 * by taking the log difference between consecutive time points.
56 *
57 * @param infections Vector of infection counts
58 * @param seeding_time Number of time steps used for seeding
59 * @return A vector of growth rates
60 *
61 * @ingroup rt_estimation
62 */
63vector calculate_growth(vector infections, int seeding_time) {
64 int t = num_elements(infections);
65 int ot = t - seeding_time;
66 vector[t] log_inf = log(infections);
67 vector[ot] growth =
68 log_inf[(seeding_time + 1):t] - log_inf[seeding_time:(t - 1)];
69 return(growth);
70}
real update_infectiousness(vector infections, vector gt_rev_pmf, int seeding_time, int index)
vector calculate_Rt(vector infections, int seeding_time, vector gt_rev_pmf, int smooth)
vector calculate_growth(vector infections, int seeding_time)