Skip to content

fix(means_ratio): use addition in Lajeunesse (2015) Eq. 5 variance adjustment - #722

Merged
mattansb merged 2 commits into
easystats:mainfrom
finite-sample:fix/means-ratio-jvar-sign
Nov 29, 2026
Merged

mattansb merged 2 commits into
easystats:mainfrom
finite-sample:fix/means-ratio-jvar-sign

Conversation

@soodoku

@soodoku soodoku commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Description

In .logrom_calc() (R/means_ratio.R:235), the small-sample variance adjustment term (Jvar) for means_ratio(..., adjust = TRUE) currently subtracts the second group's fourth-order term instead of adding it:

Jvar <- 0.5 * (sd1^4 / (n1^2 * m1^4) - sd2^4 / (n2^2 * m2^4))

In Lajeunesse (2015, Ecology, Eq. 5), the bias-corrected variance estimator is symmetric in the two groups and adds both terms:

$$v_{\text{RR}^\Delta} = \frac{SD_1^2}{n_1 \bar{X}_1^2} + \frac{SD_2^2}{n_2 \bar{X}_2^2} + \frac{1}{2}\left(\frac{SD_1^4}{n_1^2 \bar{X}_1^4} + \frac{SD_2^4}{n_2^2 \bar{X}_2^4}\right)$$

Because of the minus sign, var_rom is not invariant to swapping x and y, so means_ratio(x, y, adjust = TRUE) and means_ratio(y, x, adjust = TRUE) produce non-reciprocal confidence intervals, and the adjustment underestimates sampling variance whenever group 2's squared coefficient of variation term exceeds group 1's.

Proposed Changes

  • R/means_ratio.R: change - to + in Jvar inside .logrom_calc().
  • tests/testthat/test-rom.R:
    • Add a regression test verifying exact symmetry of var_rom and reciprocal CIs for means_ratio(x, y, adjust = TRUE) vs means_ratio(y, x, adjust = TRUE), plus exact agreement with Lajeunesse (2015, Eq. 5).
    • Update three existing test expectations (mtcars swapped CIs and sleep paired CIs) that previously pinned values computed with the - sign; updated values were verified directly against Lajeunesse (2015, Eq. 5).

Minimal Reproducible Example

x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)

means_ratio(x, y, adjust = TRUE)
means_ratio(y, x, adjust = TRUE)
  • Before (main):
    • means_ratio(x, y, adjust = TRUE): 1.31 [0.82, 2.10] (var_rom = 0.0577)
    • means_ratio(y, x, adjust = TRUE): 0.76 [0.47, 1.23] (var_rom = 0.0590; reciprocal of CI_high is 0.8132 != 0.8174)
  • After (this PR):
    • means_ratio(x, y, adjust = TRUE): 1.31 [0.81, 2.11] (var_rom = 0.0593)
    • means_ratio(y, x, adjust = TRUE): 0.76 [0.47, 1.23] (var_rom = 0.0593; exact reciprocals to 1e-12)

Verification

  • testthat::test_file("tests/testthat/test-rom.R"): 39/39 passed.
  • Revert check (reverting only R/means_ratio.R): 5 failures in test-rom.R; restoring R/means_ratio.R returns to 39/39 passed.
  • Formatted with air format and checked with lintr (0 lints).

This PR was assisted by Gemini 4 Neon via Antigravity CLI; I have reviewed and verified the change.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

There are no active review issues for the small-sample variance adjustment correction.

No open issues 🎉

What changed in this PR

Corrects the small-sample variance adjustment in means_ratio(..., adjust = TRUE) to match Lajeunesse (2015, Eq. 5), making the variance symmetric when groups are swapped.

Changes:

  • Adds both groups’ fourth-order variance terms instead of subtracting the second.
  • Adds regression checks for symmetric variance, reciprocal confidence intervals, and agreement with Eq. 5.
  • Updates existing adjusted confidence interval expectations.
File Description
tests/​testthat/​test-rom.R Adds symmetry and formula regression tests; updates adjusted confidence interval expectations.
R/​means_ratio.R Corrects the sign in the small-sample variance adjustment.

Review effort: Balanced

@mattansb
mattansb merged commit 1d0d2f7 into easystats:main Nov 29, 2026
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.

3 participants