Skip to content

Fix pop_age() to use pop_column parameter in interpolation#271

Merged
sbfnk merged 1 commit intomainfrom
fix-pop-column-interpolation
Jan 19, 2026
Merged

Fix pop_age() to use pop_column parameter in interpolation#271
sbfnk merged 1 commit intomainfrom
fix-pop-column-interpolation

Conversation

@sbfnk
Copy link
Collaborator

@sbfnk sbfnk commented Jan 9, 2026

Summary

  • Fixes hard-coded population column in interpolation code to use the pop_column parameter
  • Line 115: population :=paste(pop_column) :=
  • Line 116: population *get(pop_column) *
  • Adds test for custom column names with interpolation

Fixes #267

Summary by CodeRabbit

  • Enhancements

    • Population column handling now supports custom column names while preserving data interpolation logic.
  • Tests

    • Added test coverage for custom column names and interpolation scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 9, 2026

📝 Walkthrough

Walkthrough

The pop_age() function now dynamically references the pop_column parameter during age-group interpolation, replacing hardcoded "population" column references with get(pop_column) and paste(pop_column). A corresponding test validates correct behavior with custom column names.

Changes

Cohort / File(s) Summary
Dynamic column reference in interpolation
R/pop_age.R
Updated hardcoded "population" column assignment to use paste(pop_column) on left-hand side and get(pop_column) on right-hand side in age-limit interpolation block, ensuring the function respects the pop_column parameter
Custom column name test coverage
tests/testthat/test-agegroups.r
Added test case "pop_age works with custom column names and interpolation" verifying function correctly processes non-standard column names (age_lower, pop_count) with age interpolation while preserving total population

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit fixed a column name,
Where hardcoded strings once took the blame,
Now pop_age() bends with graceful ease,
Respecting parameters as you please! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main fix: using the pop_column parameter in interpolation instead of hard-coded 'population'.
Linked Issues check ✅ Passed The PR directly addresses issue #267 by replacing hard-coded 'population' references with dynamic pop_column usage on lines 115-116, and adds test coverage for custom column names.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the pop_column parameter usage and adding test coverage; no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
R/pop_age.R (1)

111-121: Bug fix is correct; consider using idiomatic data.table dynamic LHS for robustness.

The replacement of hard-coded population with paste(pop_column) / get(pop_column) in the interpolation path matches the intended behavior and aligns with Line 111.

Optional follow-up: use (pop_column) := instead of paste(pop_column) := (canonical data.table; avoids surprises if pop_column isn’t scalar).

Proposed tweak (no behavior change intended)
-      pop <- pop[, paste(pop_column) := get(pop_column)[1], by = ..segment]
+      pop <- pop[, (pop_column) := get(pop_column)[1], by = ..segment]
       pop <- pop[, ..upper.age.limit := c(pop[[pop_age_column]][-1], NA)]
       pop[
         !is.na(..original.upper.age.limit),
-        paste(pop_column) := round(
+        (pop_column) := round(
           get(pop_column) *
             (..upper.age.limit - get(pop_age_column)) /
             (..original.upper.age.limit - ..original.lower.age.limit)
         )
       ]
tests/testthat/test-agegroups.r (1)

56-79: Test covers the regression well; prefer asserting (not suppressing) the interpolation warning.

Right now suppressWarnings() could hide unexpected warnings, and expect_identical() can fail on harmless type differences.

Suggested test adjustment
-  # Test with interpolation (age_limits not matching existing groups)
-  # nolint start: implicit_assignment_linter
-  result <- suppressWarnings(
-    pop_age(
-      pop_data,
-      age_limits = c(0, 8, 15),
-      pop_age_column = "age_lower",
-      pop_column = "pop_count"
-    )
-  )
-  # nolint end
+  # Test with interpolation (age_limits not matching existing groups)
+  expect_warning(
+    result <- pop_age(
+      pop_data,
+      age_limits = c(0, 8, 15),
+      pop_age_column = "age_lower",
+      pop_column = "pop_count"
+    ),
+    "Linearly estimating"
+  )

   expect_named(result, c("age_lower", "pop_count"))
-  expect_identical(result$age_lower, c(0, 8, 15))
+  expect_equal(result$age_lower, c(0, 8, 15))
   # Total population should be preserved
-  expect_identical(sum(result$pop_count), sum(pop_data$pop_count))
+  expect_equal(sum(result$pop_count), sum(pop_data$pop_count))
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 185c535 and 3eda9a0.

📒 Files selected for processing (2)
  • R/pop_age.R
  • tests/testthat/test-agegroups.r
🧰 Additional context used
📓 Path-based instructions (3)
tests/**/*.{R,r}

📄 CodeRabbit inference engine (CLAUDE.md)

tests/**/*.{R,r}: Run devtools::test() to ensure all tests pass before committing
Add tests for bug fixes (regression tests) or new features where appropriate

Files:

  • tests/testthat/test-agegroups.r
**/*.{R,r}

📄 CodeRabbit inference engine (CLAUDE.md)

Run lintr::lint_package() to check for style issues and fix any in files you modified before committing

Files:

  • tests/testthat/test-agegroups.r
  • R/pop_age.R
R/**/*.{R,r}

📄 CodeRabbit inference engine (CLAUDE.md)

Run devtools::document() to regenerate documentation from roxygen comments before committing

Files:

  • R/pop_age.R
🧠 Learnings (5)
📓 Common learnings
Learnt from: njtierney
Repo: epiforecasts/socialmixr PR: 196
File: R/contact-matrix-utils.R:7-13
Timestamp: 2025-09-02T05:04:15.939Z
Learning: In the socialmixr package, functions like est_part_age_mean create new columns (part_age) by imputing from other existing columns (part_age_exact, part_age_est_min, part_age_est_max). The condition checks the source columns, not the target column being created.
Learnt from: sbfnk
Repo: epiforecasts/socialmixr PR: 248
File: R/contact-matrix-utils.R:206-216
Timestamp: 2025-12-12T19:08:46.874Z
Learning: In the socialmixr package, the add_age() helper function in R/contact-matrix-utils.R intentionally overwrites the age column (e.g., part_age, cnt_age) with values from the exact age column (e.g., part_age_exact, cnt_age_exact) when the exact column exists. This overwriting behavior is by design.
📚 Learning: 2025-12-12T19:08:46.874Z
Learnt from: sbfnk
Repo: epiforecasts/socialmixr PR: 248
File: R/contact-matrix-utils.R:206-216
Timestamp: 2025-12-12T19:08:46.874Z
Learning: In the socialmixr package, the add_age() helper function in R/contact-matrix-utils.R intentionally overwrites the age column (e.g., part_age, cnt_age) with values from the exact age column (e.g., part_age_exact, cnt_age_exact) when the exact column exists. This overwriting behavior is by design.

Applied to files:

  • tests/testthat/test-agegroups.r
  • R/pop_age.R
📚 Learning: 2025-09-02T05:04:15.939Z
Learnt from: njtierney
Repo: epiforecasts/socialmixr PR: 196
File: R/contact-matrix-utils.R:7-13
Timestamp: 2025-09-02T05:04:15.939Z
Learning: In the socialmixr package, functions like est_part_age_mean create new columns (part_age) by imputing from other existing columns (part_age_exact, part_age_est_min, part_age_est_max). The condition checks the source columns, not the target column being created.

Applied to files:

  • tests/testthat/test-agegroups.r
  • R/pop_age.R
📚 Learning: 2026-01-07T10:52:15.874Z
Learnt from: sbfnk
Repo: epiforecasts/socialmixr PR: 259
File: R/load_survey.R:20-25
Timestamp: 2026-01-07T10:52:15.874Z
Learning: Use Zenodo concept DOIs (e.g., 10.5281/zenodo.1157918) in code examples and documentation so readers always access the latest dataset version without modifying code. Replace versioned or specific DOIs in examples (including R scripts like R/load_survey.R) with the concept DOI and include a brief note that it resolves to the latest version.

Applied to files:

  • R/pop_age.R
📚 Learning: 2026-01-09T19:13:19.631Z
Learnt from: sbfnk
Repo: epiforecasts/socialmixr PR: 266
File: tests/testthat/test-clean.R:197-208
Timestamp: 2026-01-09T19:13:19.631Z
Learning: In tests for the socialmixr package (e.g., any test files under tests/), ensure the clean() function preserves only columns present in the input data and does not add missing columns (such as country). Tests should explicitly verify that absent columns remain absent after cleaning, and that any changes to introduce missing columns are deliberate and intentional.

Applied to files:

  • R/pop_age.R
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: ubuntu-latest (oldrel-1)
  • GitHub Check: ubuntu-latest (devel)
  • GitHub Check: macOS-latest (release)
  • GitHub Check: windows-latest (release)
  • GitHub Check: ubuntu-latest (release)
  • GitHub Check: check-surveys
  • GitHub Check: test-coverage
  • GitHub Check: pkgdown
  • GitHub Check: lint-changed-files
  • GitHub Check: document

@sbfnk sbfnk added this pull request to the merge queue Jan 19, 2026
Merged via the queue into main with commit b559f7c Jan 19, 2026
14 checks passed
@sbfnk sbfnk deleted the fix-pop-column-interpolation branch January 19, 2026 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pop_age() ignores pop_column parameter in interpolation

1 participant