Skip to content

Fix add_pvalue() matching Gray-test p-value to plotted outcome by name (#277) - #278

Merged
ddsjoberg merged 3 commits into
mainfrom
fix/277-add-pvalue-outcome-matching
Jul 24, 2026
Merged

Fix add_pvalue() matching Gray-test p-value to plotted outcome by name (#277)#278
ddsjoberg merged 3 commits into
mainfrom
fix/277-add-pvalue-outcome-matching

Conversation

@ddsjoberg

Copy link
Copy Markdown
Collaborator

What

Fixes #277 — for competing-risks fits, add_pvalue() could report the Gray-test p-value for the wrong competing event.

Why

In update_add_pvalue() (R/add_pvalue.R), the outcome position was derived from the row order of tidycmprsk::tidy(survfit)$outcome and then reused to index the wide p.value_* columns produced by tidycmprsk::glance(). Those two orderings are independent (tidy orders by first appearance of the label; glance numbers columns by failcode), so when they diverged the reported p-value belonged to a different outcome than the one plotted.

Reproduced by re-leveling death_cr so the factor order differs from the failcode order: choosing "death from cancer" (tidy position 1) wrongly pulled p.value_1 (0.766, belonging to "death other causes") instead of the correct p.value_2 (0.159).

How

The chosen outcome is now matched by name against glance()'s own outcome_* columns, and the paired p.value_* column is pulled. This removes any dependency on tidy() ordering. The now-unused tidycmprsk::tidy() call and position calculation in the single-outcome-check block were removed; the multi-outcome abort is unchanged.

Tests

  • Added a regression test in tests/testthat/test-add_pvalue.R using a fit where tidy() and glance() orderings differ. It asserts each plotted outcome reports its own p-value, that the two outcomes' values are not swapped, and includes a sanity check that the orderings genuinely differ (so the test actually exercises the bug).
  • Existing competing-risks tests only checked that some p-value string was present, so the bug previously passed them.

Reviewer notes

  • The fix assumes the plotted outcome label matches a glance() outcome_* value exactly; the existing single-outcome guard and ggcuminc() outcome validation keep the no-match path unreachable in normal use.
  • NEWS.md updated with a bullet under the development version.

🤖 Generated with Claude Code

ddsjoberg and others added 2 commits July 22, 2026 15:16
#277)

For competing-risks fits, add_pvalue() derived the outcome position from the
row order of tidycmprsk::tidy() and reused that integer to index the wide
p.value_* columns from tidycmprsk::glance(). Those two orderings are
independent, so when they diverged the reported p-value belonged to a
different competing event.

The chosen outcome is now matched by name against glance()'s own outcome_*
columns, and the paired p.value_* column is pulled. Adds a regression test
using a fit where tidy() and glance() orderings differ.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Documents package architecture (add-on functions as ggplot2 layers via
ggplot_add.* S3 methods), dependency handling, cli messaging convention,
roxygen/testthat/vdiffr practices, the devtools workflow, and a pre-PR
checklist.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ddsjoberg

Copy link
Copy Markdown
Collaborator Author

Thanks for report @zhaohongxin0 ! Can you take a look at this PR, please? I think it should address the issue

@zhaohongxin0

Copy link
Copy Markdown

Thank you for the quick fix. I tested the PR head (ggsurvfit 1.2.0.9001) in an isolated R library using both the reproducible example from #277 and an independent competing-risk dataset.

The plotted outcome now receives the correct Gray-test p-value:

  • Early recurrence: expected 0.496117, displayed 0.496117
  • Late recurrence: expected 0.744450, displayed 0.744450

Both values exactly matched the corresponding outcome-specific values from tidycmprsk::glance(). The current CRAN release (ggsurvfit 1.2.0) selected the competing event's p-value in the same analyses.

The fix works as intended in my external validation. Thank you again.

@ddsjoberg
ddsjoberg merged commit 6f615e6 into main Jul 24, 2026
10 checks passed
@ddsjoberg
ddsjoberg deleted the fix/277-add-pvalue-outcome-matching branch July 24, 2026 04:31
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.

add_pvalue() swaps Gray test p-values when tidy and glance outcome orders differ

2 participants