-
Notifications
You must be signed in to change notification settings - Fork 33
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
Update growth calculation #213
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,21 +118,18 @@ model { | |
generated quantities { | ||
int imputed_reports[ot_h]; | ||
vector[estimate_r > 0 ? 0: ot_h] gen_R; | ||
real r[ot_h]; | ||
real r[ot_h - 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is related to approximating the growth rate for the 1st time step. |
||
vector[ot] log_lik; | ||
if (estimate_r){ | ||
// estimate growth from estimated Rt | ||
r = R_to_growth(R, gt_mean[1], gt_sd[1]); | ||
}else{ | ||
if (estimate_r == 0){ | ||
// sample generation time | ||
real gt_mean_sample = normal_rng(gt_mean_mean, gt_mean_sd); | ||
real gt_sd_sample = normal_rng(gt_sd_mean, gt_sd_sd); | ||
// calculate Rt using infections and generation time | ||
gen_R = calculate_Rt(infections, seeding_time, gt_mean_sample, gt_mean_sample, | ||
max_gt, rt_half_window); | ||
// estimate growth from calculated Rt | ||
r = R_to_growth(gen_R, gt_mean_sample, gt_sd_sample); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that |
||
} | ||
// estimate growth from infections | ||
r = calculate_growth(infections, seeding_time + 1); | ||
// simulate reported cases | ||
imputed_reports = report_rng(reports, rep_phi, model_type); | ||
// log likelihood of model | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,11 @@ real[] R_to_growth(vector R, real gt_mean, real gt_sd) { | |
} | ||
return(r); | ||
} | ||
// Calculate growth rate | ||
real[] calculate_growth(vector infections, int seeding_time) { | ||
int t = num_elements(infections); | ||
int ot = t - seeding_time; | ||
vector[t] log_inf = log(infections); | ||
vector[ot] growth = log_inf[(seeding_time + 1):t] - log_inf[seeding_time:(t - 1)]; | ||
return(to_array_1d(growth)); | ||
} | ||
Comment on lines
+48
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update, if needed, according to the above comments. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ generated quantities { | |
matrix[n, t] infections; //latent infections | ||
matrix[n, t - seeding_time] reports; // observed cases | ||
int imputed_reports[n, t - seeding_time]; | ||
real r[n, t - seeding_time]; | ||
real r[n, t - seeding_time - 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is related to approximating the growth rate for the 1st time step. |
||
for (i in 1:n) { | ||
// generate infections from Rt trace | ||
infections[i] = to_row_vector(generate_infections(to_vector(R[i]), seeding_time, | ||
|
@@ -48,6 +48,6 @@ generated quantities { | |
} | ||
// simulate reported cases | ||
imputed_reports[i] = report_rng(to_vector(reports[i]), rep_phi[i], model_type); | ||
r[i] = R_to_growth(to_vector(R[i]), gt_mean[i, 1], gt_sd[i, 1]); | ||
r[i] = calculate_growth(to_vector(infections[i]), seeding_time + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use a switch as discussed above. |
||
} | ||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
context("estimate_infections") | ||
if (!testthat:::on_cran()) { | ||
files <- c("pmfs.stan", "infections.stan", "generated_quantities.stan") | ||
suppressMessages(expose_stan_fns(files, target_dir = system.file("stan/functions", package = "EpiNow2"))) | ||
} | ||
|
||
|
||
test_that("calculate_growth works as expected", { | ||
skip_on_cran() | ||
expect_equal(calculate_growth(rep(1, 5), 1), rep(0, 4)) | ||
expect_equal(round(calculate_growth(1:5, 2), 2), c(0.41, 0.29, 0.22)) | ||
expect_equal(round(calculate_growth(exp(0.4*1:5), 2), 2), rep(0.4, 3)) | ||
expect_error(calculate_growth(1:5, 6)) | ||
expect_error(calculate_growth(1:5, 0)) | ||
}) | ||
Comment on lines
+8
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could also add tests to compare the outputs from |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of dropping the first time step, update
calculate_growth
to either get an approximate value fromRt
or set it toNA
.