Skip to content

Commit 8516a91

Browse files
Fix survey comparisons NA filtering and bump version
1 parent 915178f commit 8516a91

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: marginaleffects
22
Title: Predictions, Comparisons, Slopes, Marginal Means, and Hypothesis Tests
3-
Version: 0.31.0.3
3+
Version: 0.31.0.4
44
Authors@R:
55
c(person(given = "Vincent",
66
family = "Arel-Bundock",

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Bugs:
88

99
* Error when merging the original data back into `comparisons()` when the data includes some list columns. The problem is that `data.table` does not support that column type. We now return the original data.table error as a warning, and do not merge the data back. Thanks to @raffaem for report #1638.
1010
* Improve warning message for hypothesis string order. Thanks to @zakarydraper for report #1640.
11+
* `comparisons()` with survey-weighted ordinal regressions failed because `stats::na.omit()` discarded every row when auxiliary columns were all `NA`, and the remaining objects fell out of alignment. We now filter using a shared index so hi/lo predictions, weights, and posterior draws stay synchronized.
1112

1213
## 0.31.0
1314

R/get_comparisons.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ compare_hi_lo_bayesian <- function(out, draws, draws_hi, draws_lo, draws_or, by,
298298
# drop missing otherwise get_averages() fails when trying to take a
299299
# simple mean
300300
idx_na <- !is.na(out$predicted_lo)
301-
out <- stats::na.omit(out, cols = "predicted_lo")
301+
# Build a single index and reuse it so `out` and the posterior draws remain aligned
302+
# while removing rows with missing `predicted_lo`.
303+
out <- out[idx_na]
302304

303305
# TODO: performance is probably terrrrrible here, but splitting is
304306
# tricky because grouping rows are not always contiguous, and the order
@@ -366,7 +368,11 @@ compare_hi_lo_bayesian <- function(out, draws, draws_hi, draws_lo, draws_or, by,
366368

367369

368370
compare_hi_lo_frequentist <- function(out, idx, cross, variables, fun_list, elasticities, newdata) {
369-
out <- stats::na.omit(out, cols = "predicted_lo")
371+
# When survey weights add all-NA aux columns, stats::na.omit() (which ignores
372+
# `cols`) was zeroing out `out`. Build one index and reuse it so downstream
373+
# operations rely on the same filtered rows.
374+
idx_pred <- !is.na(out$predicted_lo)
375+
out <- out[idx_pred]
370376
# We want to write the "estimate" column in-place because it safer
371377
# than group-merge; there were several bugs related to this in the past.
372378
# safefun() returns 1 value and NAs when the function retunrs a
@@ -402,13 +408,16 @@ compare_hi_lo_frequentist <- function(out, idx, cross, variables, fun_list, elas
402408
out <- subset(out, select = idx)
403409
}
404410
}
405-
out <- stats::na.omit(out, cols = "estimate")
411+
out <- out[!is.na(estimate)]
406412

407413
return(out)
408414
}
409415

410416

411417
compare_hi_lo <- function(hi, lo, y, n, term, cross, wts, tmp_idx, newdata, variables, fun_list, elasticities) {
418+
if (n == 0 || length(hi) == 0) {
419+
return(numeric(0))
420+
}
412421
tn <- term[1]
413422
eps <- variables[[tn]]$eps
414423
# when cross=TRUE, sanitize_comparison enforces a single function

0 commit comments

Comments
 (0)