Skip to content

cd_compare(): defaults + window-vs-window p-value (#20, #43)#73

Merged
NewGraphEnvironment merged 7 commits into
mainfrom
20-43-cd-compare-defaults-pvalue
May 10, 2026
Merged

cd_compare(): defaults + window-vs-window p-value (#20, #43)#73
NewGraphEnvironment merged 7 commits into
mainfrom
20-43-cd-compare-defaults-pvalue

Conversation

@NewGraphEnvironment
Copy link
Copy Markdown
Owner

Summary

Two tightly coupled cd_compare() improvements landed together
on one branch (issue #20 explicitly says "Tightly coupled with
#43; both should land together"):

  • Research climate departure comparison framing for cd_compare defaults #20 — defaults. window_a = 2015:2025, window_b = 1951:1980
    — the framing both regional vignettes had settled on (recent
    decade vs WMO-style standard normal). First-time users get a
    sensible answer with cd::cd_compare(ts).
  • cd_compare(): add p-value for window-vs-window comparison #43 — p-value. New test = "t" argument runs Welch two-sample
    t-test on the annual values within each window and emits a
    p_value column. test = "wilcox" runs Mann-Whitney U;
    test = NULL skips the test and drops the column. Rows where
    either window has < 8 non-NA years get p_value = NA and a
    single batched warning naming affected variable/period rows.
    Independence-between-years assumption documented in roxygen.

Default is test = "t" so output gains the column by default —
hence minor bump (0.2.80.3.0) at merge.

Why two p-values

The Mann-Kendall p-value from cd_trend() was being spliced into
the vignette comparison table as a proxy for "is this window
shift real?" — but the MK test answers a different question
("monotonic trend across the full series?"). Step changes /
U-shapes can produce significant Δ p (windows) with non-significant
trend p, and vice versa. Both vignettes now show both p-values
side-by-side: Δ p (windows) + Trend p (75-yr). The
narrative reframes the two-test framing in one paragraph.

Vignette wiring

  • Compare-recipe chunk drops to bare cd::cd_compare(ts)
    defaults carry the call. Cleaner cd showcase.
  • Compare-table chunk recomputes cmp from the cached ts so
    the new p_value column flows through without re-running the
    heavy precompute scripts (the cached regional$cmp predates
    this PR).
  • data-raw/<vignette>_vignette_data.R updated for next refresh.

Test plan

  • devtools::test() → 181 PASS / 0 FAIL (12 cd_compare tests
    including step-change → tiny p, iid noise → large p,
    wilcox, NULL → original 6-col schema, < 8-year guard
    fires once with warning, multi-variable per-row p-values)
  • lintr::lint_package() clean (no new lints from
    cd_compare; pre-existing .data pronoun + vignette caption
    length unchanged)
  • Both regional vignettes render locally; HTML contains both
    Δ p (windows) and Trend p (75-yr) column headers
  • CI: R-CMD-check + pkgdown green

Fixes #20
Fixes #43

Relates to NewGraphEnvironment/sred-2025-2026#23

🤖 Generated with Claude Code

NewGraphEnvironment and others added 7 commits May 9, 2026 13:50
Combined planning baseline for cd_compare defaults (#20) and
window-vs-window p-value (#43). Issues are explicitly coupled in
#20's issue body ('Tightly coupled with #43; both should land
together'), so single branch / single PR.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Stale Rd regenerations from earlier .R-file edits. No source change —
just devtools::document() catching up so subsequent runs don't surface
noise.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Wire the de-facto framing both regional vignettes have settled on
into the function signature so first-time users get a sensible
answer without supplying both windows. Roxygen documents the why —
cumulative-impact comparison ('how much warmer is the recent
decade') vs rate-of-change (cd_trend's job), with 1951–1980 as
the WMO-style standard normal reference.

Toy example_catalog only spans 1951:1960 so the in-Rd examples
keep explicit windows; a comment in the example block points users
at the live-catalog defaults.

Fixes #20

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
New `test` argument (default "t") emits a p_value column on the
output. `test = "t"` runs Welch's two-sample t-test on the
annual values in each window; `test = "wilcox"` runs
Mann-Whitney U; `test = NULL` skips the test entirely and drops
the column.

Rows where either window has < 8 non-NA years get
`p_value = NA` and a single batched warning naming the affected
variable/period rows (no per-row warning spam).

The window-vs-window p-value answers a different question than
the cd_trend Mann-Kendall test: "do the two windows differ" vs
"is there a monotonic trend across the full series." Step
changes and U-shapes can produce significant window differences
without monotonic trends, and vice versa. Vignettes will report
both p-values side-by-side in Phase 3.

Roxygen documents the independence assumption; for series with
strong autocorrelation the t-test is mildly anti-conservative —
the Wilcoxon alternative is more robust to non-Gaussian tails
but shares the independence assumption.

Fixes #43

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ettes

Both vignettes:
- Compare-recipe chunk now shows bare `cd::cd_compare(ts)` —
  defaults (2015:2025 vs 1951:1980, Welch t) carry the call.
  Cleaner cd showcase; reader sees the recipe in 1 line.
- Compare-table chunk recomputes `cmp` from cached `ts` so the
  new `p_value` column flows through without re-running the
  heavy precompute scripts. The cached `regional` predates
  #43 so doesn't have the column.
- Table swaps the synthesized Mann-Kendall `trend_p` proxy for
  the real window-vs-window `Δ p (windows)` (Welch). `Trend p
  (75-yr)` stays alongside — different question, both belong.
- One-paragraph narrative reframes the two p-values: "do the
  windows differ?" (window p) vs "is there a monotonic ramp?"
  (trend p). Step changes vs gradual ramps read differently.

`data-raw/<vignette>_vignette_data.R` updated so the next
precompute run uses cd_compare defaults.

Both vignettes render clean locally with both column headers
present in the rendered HTML.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Repo pattern: NEWS + DESCRIPTION bump lands as a separate
'Release v0.X.Y' commit on main post-merge, driven by
/gh-pr-merge. The branch itself stays release-prep clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The cumulative-impact comparison (mean(2015-2025) - mean(1951-1980))
is the headline number this package produces — it was buried 300+
lines down between Snowpack and Spatial Pattern, and titled with
methodological framing ('Recent vs Pre-warming') rather than
something that telegraphs the answer.

Two changes in both vignettes:

- Rename: 'Recent vs Pre-warming' -> 'Recent Decade vs Pre-Warming
  Reference'. Adds 'decade' + 'reference' so the heading reads as
  a comparison label rather than a methods stub.
- Reorder: place directly after Trends, before Daytime Highs and
  Overnight Lows. Trends still introduces the 1951-1980 reference
  and the two-test framing the table relies on, then the headline
  cumulative number lands; DTR / Snowpack / Spatial Pattern read
  as drill-down on the headline rather than a long detour before it.

Both vignettes render clean; new heading visible in HTML, old
heading absent.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@NewGraphEnvironment NewGraphEnvironment merged commit 338c7e5 into main May 10, 2026
1 check passed
@NewGraphEnvironment NewGraphEnvironment deleted the 20-43-cd-compare-defaults-pvalue branch May 10, 2026 07:09
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.

cd_compare(): add p-value for window-vs-window comparison Research climate departure comparison framing for cd_compare defaults

1 participant