Repository navigation
feature: bin by strata - #124
Conversation
vpc() and vpc_cens() gain a `bin_by_strata` argument. When TRUE, automatic binning methods are applied to each stratum's data separately. Manual per-stratum bins can be given as a list in `bins` (named by stratum or in stratum order, mixing methods and separators), which implies binning by stratum; `n_bins` can also be given per stratum. Bin boundaries are now looked up through a shared bin_boundaries() helper, and the bin separator rug is drawn per facet when bins differ by stratum. Per-stratum bins are rejected for vpc_cat() and for "percentiles" bins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 1: 🔁 changes requested
The standard suite passes (435 tests), but per-stratum binning is silently ineffective for vpc_cens() color stratification. Missing strata and the unsupported percentile method are additional edge-case gaps.
| Severity | Location | Finding |
|---|---|---|
| major | R/vpc_cens.R:124 |
stratify_color is incorporated into the local stratify variable only after format_vpc_input_data() creates the internal strat column. Since per-stratum binning groups exclusively on that column, vpc_cens(..., stratify_color = "GRP", bin_by_strata = TRUE) silently returns only a pooled bins[["1"]]; with facet and color stratification, it bins only by the facet variable. Build the effective stratification before formatting and test color-only plus facet-and-color cases. |
| minor | R/define_bins.R:75 |
Subsetting with obs[as.character(obs$strat) == s, ] retains all-NA rows when the stratification variable contains NA. Automatic methods then receive missing idv values and can fail (for example, bins = "time" errors because the range is non-finite). strata_levels() also omits the missing stratum, so manual binning leaves those rows unbinned. Handle missing strata explicitly or reject them with a clear validation error. |
| minor | R/define_bins.R:70 |
The existing percentiles automatic binning method is explicitly rejected under bin_by_strata, despite the issue requesting support for automatic binning approaches. Either calculate its separators independently per stratum or clearly document this limitation in the public parameter documentation. |
The coder agent is addressing these findings.
- vpc_cens(): combine `stratify_color` into the stratification before the
data are formatted, so the internal `strat` column (used for binning and
the calculations) includes the color variable. Previously it was ignored
for color-only stratification and mislabeled for facet + color.
- Missing values of the stratification variable are binned as their own
stratum ("NA") instead of yielding NA rows / unbinned records, and the bin
separator rug is drawn in the NA panel.
- Document that bins = "percentiles" is not available with bin_by_strata.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 2: 🔁 changes requested
The targeted binning tests and full suite pass (455 tests; six pre-existing plot warnings), but the new censored color-stratification path has incorrect plot semantics.
| Severity | Location | Finding |
|---|---|---|
| major | R/vpc_cens.R:86 |
stratify_color is folded into stratify, so the plotting code treats the color variable as a facet variable. For example, vpc_cens(..., stratify_color = "GRP", bin_by_strata = TRUE) produces separate GRP panels with fixed-color lines; with both facet and color strata, both variables become facets. This does not honor stratify_color’s documented behavior. The added test only checks that ggplot_build() succeeds, so it misses the semantic error. |
| minor | R/define_bins.R:106 |
Missing strata are converted to the ordinary string "NA". If the data also contain a legitimate stratum whose value is literally "NA", the two distinct strata receive the same key: automatic bins are calculated from their combined records, and separate manual bin definitions cannot be supplied. Use an internal key that cannot collide with user values while retaining "NA" only as the display label. |
The coder agent is addressing these findings.
- vpc_cens(): `stratify_color` now behaves as documented. Statistics are
computed per color stratum, the observed line and simulated CI are mapped
to colour/fill (grouped by the color variable, with legend), and facets
use only `stratify`. Per-stratum bin separators are colored per color
stratum.
- Per-stratum binning uses internal keys ("v:<value>", "na" for missing),
so a missing stratum no longer collides with a literal "NA" stratum. In
`db$bins` the missing stratum has an NA name; manual bins for it can be
given with an NA name (or "NA" when no literal "NA" stratum exists).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 3: ✅ approved
Per-stratum automatic and manual binning otherwise works, and both the focused binning tests and full test suite pass. Two limited edge cases remain.
| Severity | Location | Finding |
|---|---|---|
| minor | R/add_stratification.R:20 |
With multiple stratification variables, paste() converts a missing value to the literal text "NA". Consequently (NA, "Y") and ("NA", "Y") receive the same combined stratum, so their bins and statistics are merged before the collision-free strat_key() logic runs. This contradicts the documented guarantee that missing and literal "NA" strata remain distinct. |
| minor | R/define_bins.R:77 |
"percentiles" is an existing automatic binning method, but enabling bin_by_strata rejects it outright. This leaves one valid automatic approach unsupported despite the issue requesting per-stratum automatic binning. |
Resolve DESCRIPTION/NEWS conflicts: master released 1.3.3 (outlier areas, #8) and 1.3.4 (separate observed percentile lines, #122); the per-stratum binning entry moves to 1.3.5. Add a test that outlier areas work together with per-stratum bins and vpc_cens() color stratification. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 4: 🔁 changes requested
Focused binning tests passed (173 assertions), and the full suite passed (526 assertions, 5 pre-existing plot warnings). One silent data-collapsing bug remains for two-variable strata containing both missing and literal "NA" values.
| Severity | Location | Finding |
|---|---|---|
| major | R/define_bins.R:115 |
The new keying cannot distinguish missing values when two stratification variables are used. add_stratification() first converts a tuple such as (NA, "X") to the string "NA, X", so strat_key() never sees an actual NA; that tuple then collides with a literal (“NA”, “X”). bin_by_strata = TRUE consequently derives one bin set and aggregates both groups together, contrary to the documented collision-free handling. Preserve missingness in a tuple-level key before concatenating labels. |
The coder agent is addressing these findings.
add_stratification() pasted the values of both stratification variables,
so a tuple (NA, "X") became "NA, X" and collided with the literal
("NA", "X"); both were then binned and aggregated as one stratum. Missing
values are now labeled "<NA>" via strat_value_label(), and literal values
of the form "<NA>", "<<NA>>", ... get one more pair of brackets, which
keeps the labels injective. The mapping does not depend on the data, so
observed and simulated data (stratified separately) are labeled
consistently.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 5: 🔁 changes requested
The per-stratum automatic/manual binning implementation otherwise appears correct, and the full test suite passes. One exported-API compatibility regression remains.
| Severity | Location | Finding |
|---|---|---|
| major | R/vpc.R:120 |
The new argument is inserted before the existing sim_ref parameter in the exported vpc_vpc() API. Existing positional calls supplying sim_ref now bind that data frame to bin_by_strata; since the implementation checks isTRUE(bin_by_strata), it is ignored and the call silently produces an uncorrected VPC instead of an rcVPC. Append bin_by_strata after sim_ref to preserve positional compatibility and add a regression test. |
The coder agent is addressing these findings.
`bin_by_strata` was inserted before `sim_ref`, so a positional `sim_ref` bound to `bin_by_strata` and silently produced an uncorrected VPC. Move it after `...` so it can only be passed by name, and add a regression test that a positional `sim_ref` still yields the reference-corrected VPC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 6: 🔁 changes requested
The focused and full test suites pass (544 assertions; only existing warnings), but valid two-variable strata containing the delimiter can be silently merged, producing incorrect per-stratum bins and summaries.
| Severity | Location | Finding |
|---|---|---|
| major | R/add_stratification.R:23 |
Two distinct two-variable strata can still collapse into one because the internal strat key is formed with paste(..., sep = ", "). For example, ("a, b", "c") and ("a", "b, c") both become "a, b, c". strata_levels() consequently sees one stratum, so automatic bins and statistics are silently pooled, while separate manual bins cannot be addressed. Use a collision-free internal tuple key rather than the display label. |
The coder agent is addressing these findings.
The combined stratum was formed with paste(sep = ", "), so e.g.
("a, b", "c") and ("a", "b, c") both became "a, b, c" and were binned
and summarised as one stratum. strat_value_label() now escapes
backslashes and commas in values ("\\" and "\,"), which makes the label
injective while leaving labels of ordinary values unchanged and staying
independent of the data (obs and sim are stratified separately).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 7: ✅ approved
The implementation otherwise satisfies the issue, with comprehensive coverage for automatic and manual per-stratum binning. Focused tests, the full 556-assertion suite, and additional observation-only/simulation-only probes passed.
| Severity | Location | Finding |
|---|---|---|
| minor | R/define_bins.R:158 |
A valid empty-string stratum cannot be addressed reliably by a named bins or n_bins list. Because any "" name makes the entire list count as unnamed, entries are selected positionally and can silently be assigned to the wrong strata when list order differs from factor-level order. For example, bins named c("A", "") are swapped when the strata order is c("", "A"). |
roninsightrx
left a comment
There was a problem hiding this comment.
🤖 Codex review, round 8: 🔁 changes requested
The general per-stratum binning paths and package checks pass, but automatic binning fails for valid constant-IDV strata. Full tests passed (595 assertions), and R CMD check had no errors or warnings.
| Severity | Location | Finding |
|---|---|---|
| major | R/define_bins.R:88 |
Automatic per-stratum binning crashes when any stratum contains only one unique IDV value. For example, a baseline-only stratum with idv == 0 makes bins = "time" or "data" collapse to one separator, after which cut() reports invalid number of intervals; the default "jenks" and "density" methods also error on the constant subgroup. This is a valid and plausible stratified dataset even when the overall data have multiple time points. Handle constant-IDV strata by constructing a valid single-bin interval (and add coverage for it) before calling auto_bin()/cut(). |
The coder agent is addressing these findings.
Closes #11
Added per-stratum binning to vpc() and vpc_cens(): new
bin_by_strataargument applies automatic binning per stratum, andbins/n_binsaccept per-stratum lists for manual binning; bin bounds and plot bin-separator rugs are resolved per stratum. Added tests in test-binning.R, NEWS entry, and bumped version to 1.3.3.🤖 Implemented by Claude Code and reviewed by Codex, orchestrated by nightshift. Review rounds are posted as comments below.