Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions R/DuckDBTable-matrixStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ NULL
call("ifelse", has_na, NA_real_, expr)
}

# Number of non-missing elements along a margin of extent k: k minus the
# stored rows whose value is NA
.nonMissing <- function(y, k) {
n_na <- call("sum", call("as.integer", call("is.na", y)), na.rm = TRUE)
call("(", call("-", k, n_na))
}

#' @importClassesFrom DuckDBDataFrame DuckDBTable
#' @importFrom dplyr group_by n summarize
#' @importFrom DuckDBDataFrame tblconn
Expand Down Expand Up @@ -267,21 +274,24 @@ function(x, na.rm = FALSE, dims = 1, fill = 0, ..., useNames = TRUE) {
lst <- .marginSetup(x, dims = dims, margin = margin)
keycols <- lst$keycols; groups <- lst$groups; along <- lst$along; k <- lst$k
datacols <- x@datacols
# with na.rm = TRUE, stored NA values are dropped from the count too
if (fill == 0) {
aggr <- lapply(datacols, function(y)
.na_rm_guard(call("/", call("sum", y, na.rm = TRUE), k),
.na_rm_guard(call("/",
call("coalesce", call("sum", y, na.rm = TRUE), 0),
.nonMissing(y, k)),
y, na.rm))
} else {
aggr <- lapply(datacols, function(y)
.na_rm_guard(
call("/",
call("(",
call("+",
call("sum", y, na.rm = TRUE),
call("coalesce", call("sum", y, na.rm = TRUE), 0),
call("*",
fill,
call("(", call("-", k, call("n")))))),
k),
.nonMissing(y, k)),
y, na.rm))
}
aggr <- setNames(aggr, names(datacols))
Expand Down Expand Up @@ -423,17 +433,21 @@ setMethod("colSums", "DuckDBTable", function(x, na.rm = FALSE, dims = 1, fill =
conn <- tblconn(x, select = FALSE)
aggr <- setNames(lapply(names(datacols), function(nm) {
y <- datacols[[nm]]
n <- call("n")
# n counts the stored non-NA values and kk all non-NA elements, so
# that with na.rm = TRUE the stored NA values are left out entirely
n <- call("count", y)
kk <- .nonMissing(y, k)
sum_y <- call("sum", y, na.rm = TRUE)
s2 <- call("coalesce", call("var_samp", y), 0)
nfill <- call("(", call("-", k, n))
nfill <- call("(", call("-", k, call("n")))

d <- call("(", call("-", call("(", call("/", sum_y, n)), fill))
fill_term <- call("/", call("*", call("*", d, d), call("*", n, nfill)), k)
d <- call("coalesce",
call("(", call("-", call("(", call("/", sum_y, n)), fill)), 0)
fill_term <- call("/", call("*", call("*", d, d), call("*", n, nfill)), kk)
s2_term <- call("*", call("(", call("-", n, 1L)), s2)
sum_sq_dev_full <- call("+", s2_term, fill_term)

expr <- call("/", call("(", sum_sq_dev_full), k - 1L)
expr <- call("/", call("(", sum_sq_dev_full), call("(", call("-", kk, 1L)))
.na_rm_guard(expr, y, na.rm)
}), names(datacols))

Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-DuckDBTable-matrixStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,32 @@ test_that("na.rm semantics are preserved by the merged single-pass formula", {
df_true <- .collect_margin(ans_true, "idx1")
expect_false(is.na(df_true$value[df_true$idx1 == "r1"]))
})

test_that("na.rm = TRUE leaves stored NA values out of means and variances", {
m <- matrix(c(1, 0, 3, 4,
2, 2, NA, 0,
0, 5, 1, 1), nrow = 3, byrow = TRUE,
dimnames = list(idx1 = paste0("r", 1:3), idx2 = paste0("c", 1:4)))
df <- do.call(expand.grid, c(dimnames(m), stringsAsFactors = FALSE))
df$value <- as.vector(m)
df <- df[is.na(df$value) | df$value != 0, ]
tf <- tempfile(fileext = ".parquet")
on.exit(unlink(tf))
arrow::write_parquet(df, tf)
mat <- as(DuckDBArray(tf, datacol = "value", keycols = dimnames(m)), "DuckDBMatrix")

expect_equal(as.vector(rowMeans(mat, na.rm = TRUE)),
as.vector(rowMeans(m, na.rm = TRUE)))
expect_equal(as.vector(colMeans(mat, na.rm = TRUE)),
as.vector(colMeans(m, na.rm = TRUE)))
expect_equal(as.vector(rowVars(mat, na.rm = TRUE)),
as.vector(MatrixGenerics::rowVars(m, na.rm = TRUE)))
expect_equal(as.vector(colVars(mat, na.rm = TRUE)),
as.vector(MatrixGenerics::colVars(m, na.rm = TRUE)))
expect_equal(as.vector(rowSds(mat, na.rm = TRUE)),
as.vector(MatrixGenerics::rowSds(m, na.rm = TRUE)))

# without na.rm the margins with an NA are still NA
expect_true(is.na(rowMeans(mat)[2L]))
expect_true(is.na(colVars(mat)[3L]))
})