Brmshypothesis object to a pretty table in R Markdown

Hi Stan community,

Has anyone yet attempted to produce a nice knitr::kable-type table from a brmshypothesis object? Any tips on embellishing the R output of hypothesis() within R Markdown would be greatly appreciated.

Thanks!

I’m not sure if there’s any special infrastructure for making nice tables from hypothesis objects, but you could do something like the following: In the code below, we extract the hypothesis results as a data frame. From there you can use any R table-generation package. The examples below use kable/kableExtra and gt.

---
title: "Model Results"
output: bookdown::html_document2
date: '2022-05-17'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(knitr) # For kable tables
library(kableExtra) # For kable tables
library(gt) # For gt tables
library(tidyverse)
library(brms)
library(scales)
```

```{r}
fit <- brm(formula = count ~ zAge + zBase + Trt, 
           data = epilepsy, 
           prior = prior(normal(0,1), class="b"),
           backend="cmdstanr", 
           file="test-model")

# Get hypotheses for all fixed effects
hh = hypothesis(fit, paste(rownames(fixef(fit))[-1], "< 0")) 

# Extract data frame of hypothesis results
tab = hh$hypothesis %>% select(-Star)

# Set column alignments
a = map_chr(tab, ~ifelse(class(.x)=="numeric", "r","l"))

tab = tab %>% 
  mutate(across(where(is.numeric), ~comma(., accuracy=0.01))) %>% 
  rename_all(~gsub("\\.", " ", .))
```

```{r tab1}
tab %>% 
  kbl(caption="Model results with kable", align=a) %>% 
  kable_classic(full_width=FALSE, html_font="Cambria")
```

```{r tab2}
tab %>% 
  kbl(caption= "Model results with kable", align=a) %>% 
  kable_styling(full_width=FALSE, 
                bootstrap_options = c("striped", "hover", "condensed"))
```

```{r tab3}
tab %>% 
  gt(caption=md("Model results with the `gt` package")) %>% 
  cols_align(
    columns=-1,
    align="right"
  )
```

Which gives the following html output:

1 Like

Thanks so much, this is exactly what I was missing!