Skip to content

Commit 1a5680a

Browse files
committed
Print sample sizes
1 parent b4c12c3 commit 1a5680a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

R/loo_compare.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,15 @@ loo_compare_checks <- function(loos) {
186186
stop("All inputs should have class 'loo'.", call.=FALSE)
187187
}
188188

189-
Ns <- sapply(loos, function(x) nrow(x$pointwise))
190-
if (!all(Ns == Ns[1L])) {
191-
stop("Not all models have the same number of data points.", call.=FALSE)
189+
Ns <- vapply(loos, function(x) nrow(x$pointwise), integer(1))
190+
if (any(Ns != Ns[1L])) {
191+
stop(
192+
paste0(
193+
"All models must have the same number of observations, but models have inconsistent observation counts: ",
194+
paste(paste0("'", find_model_names(loos), "' (", Ns, ")"), collapse = ", ")
195+
),
196+
call. = FALSE
197+
)
192198
}
193199

194200
## warnings

tests/testthat/test_compare.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ test_that("loo_compare throws appropriate errors", {
1818
expect_error(loo_compare(w1, list(1, 2, 3)), "class 'loo'")
1919
expect_error(loo_compare(w1), "requires at least two models")
2020
expect_error(loo_compare(x = list(w1)), "requires at least two models")
21-
expect_error(loo_compare(w1, w3), "same number of data points")
22-
expect_error(loo_compare(w1, w2, w3), "same number of data points")
21+
expect_error(
22+
loo_compare(w1, w3),
23+
"All models must have the same number of observations, but models have inconsistent observation counts: 'model1' (32), 'model2' (31)",
24+
fixed = TRUE
25+
)
26+
expect_error(
27+
loo_compare(w1, w2, w3),
28+
"All models must have the same number of observations, but models have inconsistent observation counts: 'model1' (32), 'model2' (32), 'model3' (31)",
29+
fixed = TRUE
30+
)
2331
})
2432

2533
test_that("loo_compare throws appropriate warnings", {

0 commit comments

Comments
 (0)