Skip to content

leave stored na values out of na.rm means and variances - #2

Closed
buyan-kh wants to merge 1 commit into
Genentech:mainfrom
buyan-kh:na-rm-margin-denominator
Closed

buyan-kh wants to merge 1 commit into
Genentech:mainfrom
buyan-kh:na-rm-margin-denominator

Conversation

@buyan-kh

Copy link
Copy Markdown

fixes #1

with na.rm = TRUE the means and variances now divide by the number of cells that are not NA, instead of the full row or column length.

added a test that checks rowMeans, colMeans, rowVars, colVars and rowSds against base R and matrixStats on a small matrix with one NA, and that na.rm = FALSE still gives NA. full test suite passes locally.

aboyounp-gne added a commit that referenced this pull request Nov 21, 2026
… (0.99.16)

With na.rm = TRUE, rowMeans, colMeans, rowVars, colVars, rowSds and
colSds dropped stored NA values from the sum but not from the count. A
mean was divided by too many elements, and a variance used the full
extent of the margin, so the results disagreed with matrixStats whenever
the data held an NA.

- Add .nonMissing(), the number of elements of a margin that are not NA:
  the extent minus the stored rows whose value is NA. The means and
  variances divide by it, and the variance counts the stored non-NA
  values with count(y) instead of n().
- A sum with no value left is coalesced to 0, so a margin with nothing
  left gives 0/0 = NaN for the mean, as base R does.
- A variance over fewer than two values is NA. DuckDB returns NaN for
  0/0, which made an all-NA margin come back as NaN where matrixStats
  gives NA.
- The inner subtraction is parenthesized explicitly, since dbplyr drops
  the parentheses of a nested subtraction.

Builds on the proposal in #2 (count the non-NA values in the
denominator), and adds the guard for margins with too few values that
the proposal lacked.

Tests compare the six functions with matrixStats on matrices with
stored NA values, an all-NA margin, and a margin with a single value,
with NA and NaN compared exactly. Both new tests fail on the previous
code. Suite: 3486 expectations, 0 failures at 1 and 8 threads.
@aboyounp-gne

Copy link
Copy Markdown
Collaborator

Thank you for this, @buyan-kh. Counting the non-NA values in the
denominator is the approach that landed in
8dc62b1, and your
test is included.

I'm closing the PR because the same change is now on main. It differs
from yours in two places. A variance over fewer than two values is now
guarded to return NA: DuckDB returns NaN for 0/0, so an all-NA margin
came back as NaN where matrixStats gives NA. The count is also written
with explicit parentheses, because dbplyr drops the parentheses of a
nested subtraction and rendered it as k - n - count. Both are covered by
new tests that compare the six functions with matrixStats, with NA and
NaN compared exactly.

Issue #1 is closed by the same commit. Please open an issue if you see
any case where the result differs from matrixStats.

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.

rowMeans and friends with na.rm = TRUE still count the NA cells

2 participants