Improve weekly aggregation in outbreak_model()#197
Conversation
WalkthroughThis PR refactors the weekly_cases calculation in the outbreak model to pre-initialise a zero-filled container for all weeks and overlay actual outbreak data, whilst updating numeric columns to integer types throughout the codebase and tests. Changes
Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
R/outbreak_model.R (1)
10-12: Documentation does not reflect the new integer column types.The
@returndocumentation states that$week,$weekly_cases, and$cumulativearenumeric, but the implementation now producesintegercolumns. Consider updating the documentation to match.📝 Suggested documentation update
#' `@return` `data.table` of cases by week, cumulative cases, and the effective #' reproduction number of the outbreak. `data.table` columns are: -#' * `$week`: `numeric` -#' * `$weekly_cases`: `numeric` -#' * `$cumulative`: `numeric` +#' * `$week`: `integer` +#' * `$weekly_cases`: `integer` +#' * `$cumulative`: `integer` #' * `$effective_r0`: `numeric` #' * `$cases_per_gen`: `list`
🧹 Nitpick comments (1)
R/outbreak_model.R (1)
108-116: Potential type inconsistency:floor()returns double, butweekcolumn is integer.The
floor(onset / 7)expression on line 108 returns a double, whilst theweekcolumn inweekly_cases(line 114) is created as integer via0:max_week. Although the data.table join will still function correctly, this creates a subtle type mismatch betweenweekly_cases_outbreak$week(double) andweekly_cases$week(integer).Consider wrapping in
as.integer()for explicit type consistency:♻️ Suggested refinement
- weekly_cases_outbreak <- case_data[, week := floor(onset / 7) + weekly_cases_outbreak <- case_data[, week := as.integer(floor(onset / 7)) ][, list(weekly_cases = .N), by = week ]
This PR addresses #168 but goes beyond fixing the typo in the
ifstatement and rewrites a section ofoutbreak_model()to more efficiently create the empty weeks in the outputdata.table.The complete outbreak data, with missing weeks is now created using a non-equi join. The need for the original
ifstatement is removed.This update also changes the type of the columns, from double to integer, resulting in a smaller memory footprint for the outbreak data.
Test expectations are updated to reflect the change in type for the
week,weekly_casesandcumulativecolumns returned fromoutbreak_model()(and therefore alsoscenario_sim()).Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.