9191# '
9292# ' comp <- loo_compare(loo1, loo2, loo3)
9393# ' print(comp, digits = 2)
94+ # ' print(comp, simplify = FALSE) # full table
9495# '
9596# ' # can use a list of objects with custom names
9697# ' # the names will be used in the output
@@ -170,7 +171,13 @@ loo_compare.default <- function(x, ...) {
170171# ' @param p_worse For the print method only, should we include the normal
171172# ' approximation based probability of each model having worse performance than
172173# ' the best model? The default is `TRUE`.
173- print.compare.loo <- function (x , ... , digits = 1 , p_worse = TRUE ) {
174+ # ' @param simplify For the print method only, should the output be simplified to
175+ # ' only include the model names, ELPD differences, and (when `p_worse = TRUE`)
176+ # ' diagnostic columns? The default is `TRUE`. Set to `FALSE` to also print the
177+ # ' available estimate columns (pointwise ELPD, LOOIC/WAIC, and their standard
178+ # ' errors).
179+ print.compare.loo <- function (x , ... , digits = 1 , p_worse = TRUE ,
180+ simplify = TRUE ) {
174181 if (inherits(x , " old_compare.loo" )) {
175182 return (unclass(x ))
176183 }
@@ -181,19 +188,35 @@ print.compare.loo <- function(x, ..., digits = 1, p_worse = TRUE) {
181188 print(as.data.frame(x ))
182189 return (x )
183190 }
184- x2 <- cbind(
185- model = x $ model ,
186- .fr(x [, c(" elpd_diff" , " se_diff" )], digits )
191+ base_cols <- c(" model" , " elpd_diff" , " se_diff" )
192+ diag_cols <- c(" p_worse" , " diag_diff" , " diag_elpd" )
193+ show_diag <- p_worse && " p_worse" %in% colnames(x )
194+
195+ estimate_cols <- setdiff(colnames(x ), c(base_cols , diag_cols ))
196+ estimate_cols <- estimate_cols [vapply(x [estimate_cols ], is.numeric , logical (1 ))]
197+
198+ cols <- c(
199+ base_cols ,
200+ if (show_diag ) diag_cols ,
201+ if (! simplify ) estimate_cols
187202 )
188- if (p_worse && " p_worse" %in% colnames(x )) {
189- x2 <- cbind(
190- x2 ,
191- p_worse = .fr(x [, " p_worse" ], digits = 2 ),
192- diag_diff = x [, " diag_diff" ],
193- diag_elpd = x [, " diag_elpd" ]
194- )
203+ cols <- intersect(cols , colnames(x ))
204+
205+ x2 <- x [, cols , drop = FALSE ]
206+
207+ fmt_cols <- setdiff(cols , c(" model" , " diag_diff" , " diag_elpd" ))
208+ if (length(fmt_cols )) {
209+ if (" p_worse" %in% fmt_cols ) {
210+ x2 $ p_worse <- .fr(x2 $ p_worse , digits = 2 )
211+ fmt_cols <- setdiff(fmt_cols , " p_worse" )
212+ }
213+ if (length(fmt_cols )) {
214+ x2 [fmt_cols ] <- .fr(x2 [fmt_cols ], digits )
215+ }
195216 }
196- print(x2 , quote = FALSE , row.names = FALSE )
217+ # Use `as.data.frame(x2)` here to drop "compare.loo"
218+ # so print() uses print.data.frame.
219+ print(as.data.frame(x2 ), quote = FALSE , row.names = FALSE )
197220
198221 # show glossary for diagnostic flags
199222 has_diag <- any(nzchar(x [[" diag_diff" ]], keepNA = FALSE ), na.rm = TRUE ) ||
0 commit comments