Skip to content

Fix ldaFunction score sign for reference-class features - #107

Open
LiNk-NY wants to merge 1 commit into
develfrom
fix-lda-score-sign
Open

LiNk-NY wants to merge 1 commit into
develfrom
fix-lda-score-sign

Conversation

@LiNk-NY

@LiNk-NY LiNk-NY commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Closes #104

Problem

ldaFunction() combined a signed class-mean difference with an unsigned LDA coefficient:

coeff[v] <- abs(scal[v])
lda.means.diff <- lda.fit$means[2, ] - lda.fit$means[1, ]
res <- (lda.means.diff + coeff) / 2

For features enriched in the first (reference) level, the score became (coeff - |diff|) / 2 instead of -(|diff| + coeff) / 2. The magnitude was too small, and when coeff > |diff| the sign flipped, assigning the feature to the wrong class. Results therefore depended on the factor level order.

Fix

Average the magnitudes as in LEfSe and apply the sign of the mean difference:

res <- sign(lda.means.diff) * (abs(lda.means.diff) + coeff) / 2

Tests

  • The existing ldaFunction unit test encoded the biased value. Its two features are mirror images, yet the one enriched in class A scored 0 while the one enriched in B scored 9. It now expects -9 / 9.
  • Added a check that reversing the factor levels flips only the sign.
  • testthat suite passes locally.

Impact on real data

On the zeller14 CRC vs control example, the same 61 features are selected, with no sign changes and a maximum score change of 0.0002 (log10 scale). At relative-abundance scale (1e6), |diff| is usually much larger than coeff, so the existing expected values in test-lefser.R still hold. With the fix, results are now exactly invariant to level order (previously up to 0.0002 off). Larger effects are expected when coeff is comparable to |diff|, e.g. low-abundance features or rescaled inputs.

Version: is not bumped.

🤖 Generated with Claude Code

ldaFunction() added the signed class-mean difference to the unsigned
(absolute) scaled LDA coefficient. For features enriched in the first
factor level this gave (coeff - |diff|) / 2 instead of
-(|diff| + coeff) / 2, shrinking the score or flipping it to the other
class when coeff > |diff|, so results depended on level order.

Average the magnitudes as in LEfSe and apply the sign of the mean
difference. Update the ldaFunction unit test, which encoded the biased
value (0 instead of -9), and add a level-reversal check.

Closes #104

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread NEWS.md Resolved
@claude

claude Bot commented Dec 1, 2026

Copy link
Copy Markdown

Code change in ldaFunction() looks correct (sign of mean diff × averaged magnitudes; downstream sign()*log10(1+abs()) is consistent) and the tests cover it. One item worth raising:

  1. Version not bumped. This changes numerical output, so for Bioconductor bump Version: in DESCRIPTION and add a matching NEWS header; otherwise the fix won't be distributed. Not bumping is not a good idea for a results-changing bug fix.

No exported-function documentation changes are needed (ldaFunction is internal, @noRd).

This branch has not been deployed

No deployments
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.

ldaFunction(): score sign/magnitude biased toward the second class level (signed mean difference added to unsigned coefficient)

1 participant