Skip to content

Commit dc9db69

Browse files
committed
cleanup print method
1 parent 8f521a5 commit dc9db69

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

R/loo_compare.R

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,18 @@ print.compare.loo <- function(x, ..., digits = 1, p_worse = TRUE) {
183183
if (!inherits(x, "data.frame")) {
184184
class(x) <- c(class(x), "data.frame")
185185
}
186-
xcopy <- x
187-
if (NCOL(xcopy) >= 2) {
188-
xcopy <- xcopy[, c("elpd_diff", "se_diff")]
189-
}
190-
xcopy <- cbind(model = x$model, .fr(xcopy, digits))
186+
x2 <- cbind(
187+
model = x$model,
188+
.fr(x[, c("elpd_diff", "se_diff")], digits)
189+
)
191190
if (p_worse && "p_worse" %in% colnames(x)) {
192-
xcopy <- cbind(
193-
xcopy,
194-
p_worse = .fr(x[, "p_worse"], 2),
191+
x2 <- cbind(
192+
x2,
193+
p_worse = .fr(x[, "p_worse"], digits = 2),
195194
diag_pnorm = x[, "diag_pnorm"]
196195
)
197196
}
198-
print(xcopy, quote = FALSE)
197+
print(x2, quote = FALSE)
199198
invisible(x)
200199
}
201200

tests/testthat/_snaps/compare.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
1 model1 0.0 0.0 NA
4747
2 model2 -4.1 0.1 1.00 N < 100
4848

49+
---
50+
51+
Code
52+
print(comp2, p_worse = FALSE)
53+
Output
54+
model elpd_diff se_diff
55+
1 model1 0.0 0.0
56+
2 model2 -4.1 0.1
57+
4958
# loo_compare returns expected result (3 models)
5059

5160
WAoAAAACAAQEAgACAwAAAAMTAAAACwAAABAAAAADAAQACQAAAAZtb2RlbDEABAAJAAAABm1v

tests/testthat/test_compare.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ test_that("loo_compare returns expected results (2 models)", {
9090
expect_equal(colnames(comp2), comp_colnames)
9191
expect_snapshot_value(comp2, style = "serialize")
9292
expect_snapshot(print(comp2))
93+
expect_snapshot(print(comp2, p_worse = FALSE))
9394

9495
# specifying objects via ... and via arg x gives equal results
9596
expect_equal(comp2, loo_compare(x = list(w1, w2)))

0 commit comments

Comments
 (0)