A table of results for Bayesian mixed-effects models: Grouping variables and specifying random slopes

Here I share the format applied to tables presenting the results of Bayesian models in Bernabeu (2022; the table for frequentist models is covered in this other post). The sample table presents a Bayesian mixed-effects model that was fitted using the R package brms (Bürkner et al., 2022). The mixed effects were driven by the maximal principle (Brauer & Curtin, 2018). The format of the table resembles one of the examples published by the American Psychological Association. However, there are also deviations from those examples. For instance, in the present table, the effects are grouped under informative labels to facilitate the readers’ comprehension, using the kableExtra package (Zhu, 2022). Furthermore, the random slopes are specified using superscript letters and a footnote. The table can be reproduced using the materials at https://osf.io/gt5uf.

Loading packages and the results of the models

library(dplyr)
library(knitr)
library(tibble)
library(stringr)
library(lmerTest)
library(kableExtra)

# Load Bayesian results summary

semanticpriming_summary_weaklyinformativepriors_exgaussian = 
  readRDS(gzcon(url('https://github.com/pablobernabeu/language-sensorimotor-simulation-PhD-thesis/blob/main/semanticpriming/bayesian_analysis/results/semanticpriming_summary_weaklyinformativepriors_exgaussian.rds?raw=true')))

Adjusting the names of the effects

First, to facilitate the understanding of the results, the original names of the effects will be adjusted in the brms summary.

# Rename effects in plain language and specify the random slopes
# (if any) for each effect, in the footnote. For this purpose, 
# superscripts are added to the names of the appropriate effects.
# 
# In the interactions below, word-level variables are presented 
# first for the sake of consistency (the order does not affect 
# the results in any way). Also in the interactions, double 
# colons are used to inform the 'bayesian_model_table' 
# function that the two terms in the interaction must be split 
# into two lines.

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_attentional_control'] = 'Attentional control'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_attentional_control'] = 'Attentional control'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_vocabulary_size'] = 'Vocabulary size <sup>a</sup>'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_recoded_participant_gender'] = 'Gender <sup>a</sup>'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_target_word_frequency'] = 'Word frequency'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_target_number_syllables'] = 'Number of syllables'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_word_concreteness_diff'] = 'Word-concreteness difference'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_cosine_similarity'] = 'Language-based similarity <sup>b</sup>'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_visual_rating_diff'] = 'Visual-strength difference <sup>b</sup>'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_recoded_interstimulus_interval'] = 'Stimulus onset asynchrony (SOA) <sup>b</sup>'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_word_concreteness_diff:z_vocabulary_size'] = 
  'Word-concreteness difference :: Vocabulary size'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_word_concreteness_diff:z_recoded_interstimulus_interval'] = 
  'Word-concreteness difference : SOA'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_word_concreteness_diff:z_recoded_participant_gender'] = 
  'Word-concreteness difference : Gender'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_attentional_control:z_cosine_similarity'] = 
  'Language-based similarity :: Attentional control'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_attentional_control:z_visual_rating_diff'] = 
  'Visual-strength difference :: Attentional control'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_vocabulary_size:z_cosine_similarity'] = 
  'Language-based similarity :: Vocabulary size'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_vocabulary_size:z_visual_rating_diff'] = 
  'Visual-strength difference :: Vocabulary size'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_recoded_participant_gender:z_cosine_similarity'] = 
  'Language-based similarity : Gender'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_recoded_participant_gender:z_visual_rating_diff'] = 
  'Visual-strength difference : Gender'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_cosine_similarity:z_recoded_interstimulus_interval'] = 
  'Language-based similarity : SOA <sup>b</sup>'

rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed)[
  rownames(semanticpriming_summary_weaklyinformativepriors_exgaussian$fixed) == 
    'z_visual_rating_diff:z_recoded_interstimulus_interval'] = 
  'Visual-strength difference : SOA <sup>b</sup>'

bayesian_model_table()

In Bernabeu (2022), the following custom function was used.

The above function was used to render a PDF output. In the current scenario, however, we have an HTML output. In the above function, the code used for the \(\widehat{R}\) tailored to the HTML output (&Rcirc;) does not render properly.

Instead, the LaTeX code $\\widehat{R}$ must be used. Therefore, we’ll correct this error and load the function below.

# Function used in the manuscript to present summaries from 'brms' models 
# in APA-formatted tables. The only obligatory argument to be supplied is 
# a summary of a 'brms' model.

bayesian_model_table = 
  
  function(model_summary, show_intercept = TRUE, select_effects = NULL, 
           order_effects = NULL, format = NULL, 
           
           # If interaction_symbol_x = TRUE, replace double colons with 
           # times symbols followed by line breaks and indentation. 
           # Then, replace single colons with times symbols.
           interaction_symbol_x = FALSE,
           
           caption = 'Summary of the lmerTest model.') {
    
    require(dplyr)
    require(knitr)
    require(tibble)
    require(stringr)
    require(lmerTest)
    require(kableExtra)
    
    # Create data frame
    model_summary = 
      data.frame(Effect = rownames(model_summary$fixed), 
                 Estimate = model_summary$fixed$Estimate, 
                 SE = model_summary$fixed$Est.Error, 
                 CrI_2.5 = model_summary$fixed$`l-95% CI`, 
                 CrI_97.5 = model_summary$fixed$`u-95% CI`, 
                 Rhat = model_summary$fixed$Rhat,
                 row.names = NULL)
    
    # Process credible intervals and present both inside square brackets
    
    model_summary$CrI_2.5 = model_summary$CrI_2.5 %>% 
      # Round off and keep trailing zeros
      sprintf('%.2f', .) %>% 
      # Remove minus sign from pure zeros
      sub('-0.00', '0.00', .)
    
    model_summary$CrI_97.5 = model_summary$CrI_97.5 %>% 
      # Round off and keep trailing zeros
      sprintf('%.2f', .) %>% 
      # Remove minus sign from pure zeros
      sub('-0.00', '0.00', .)
    
    model_summary$CrI_95 = paste0('[', model_summary$CrI_2.5, ', ', 
                                  model_summary$CrI_97.5, ']')
    
    # If show_intercept = FALSE, remove it
    if(isFALSE(show_intercept)) {
      model_summary = model_summary %>% filter(!grepl('Intercept', Effect))
      
      # Put 'Intercept' in parentheses
    } else if(!is.null(model_summary[model_summary$Effect == 'Intercept', 'Effect'])) {
      model_summary[model_summary$Effect == 'Intercept', 'Effect'] = '(Intercept)'
    }
    
    # If select_effects was supplied, apply it and order effects accordingly
    if(!is.null(select_effects)) {
      model_summary = model_summary %>% filter(Effect %in% select_effects) %>%
        arrange(factor(Effect, levels = select_effects))
    }
    
    # If order_effects was supplied, apply order
    if(!is.null(order_effects)) {
      model_summary = model_summary %>%
        arrange(factor(Effect, levels = order_effects))
    }
    
    # Round other values
    
    model_summary$Estimate = model_summary$Estimate %>% as.numeric %>% 
      # Round off and keep trailing zeros
      sprintf('%.2f', .) %>% 
      # Remove minus sign from pure zeros
      sub('-0.00', '0.00', .)
    
    model_summary$SE = model_summary$SE %>% as.numeric %>% 
      # Round off and keep trailing zeros
      sprintf('%.2f', .)
    
    model_summary$Rhat = model_summary$Rhat %>% as.numeric %>% 
      # Round off and keep trailing zeros
      sprintf('%.2f', .)
    
    # Order columns
    model_summary = model_summary %>% select(Effect, Estimate, SE, CrI_95, Rhat)
    
    # Right-align all columns after first one
    align = c('l', 'r', 'r', 'r', 'r')
    
    # Establish latex or HTML format: if no format supplied, 
    # try to obtain it from knitr, or apply HTML
    if(missing(format) || is.null(format)) {
      if(knitr::is_latex_output()) {
        format = 'latex'
      } else format = 'html'
    }
    
    # HTML format
    if(format == 'html') {
      
      # If interaction_symbol_x = TRUE, replace double colons with times 
      # symbols followed by line breaks and indentation. Then, replace 
      # single colons with times symbols.
      if(interaction_symbol_x) {
        model_summary$Effect = model_summary$Effect %>% 
          gsub('::', ' &times; <br> &nbsp;&nbsp;', .) %>%
          gsub(':', ' &times; ', .)
      }
      
      # Output table
      model_summary %>% 
        
        # Remove header of first column and rename other headers
        rename(' ' = 'Effect', '&beta;' = 'Estimate', '<i>SE</i>' = 'SE', 
               '95% CrI' = 'CrI_95', '$\\widehat{R}$' = 'Rhat') %>%
        
        # Present table
        kbl(digits = 2, booktabs = TRUE, escape = FALSE, align = align,
            
            # Caption of the table (default unless specified)
            caption = caption, 
            
            # Disable occasional line gap (https://stackoverflow.com/a/49018919/7050882)
            linesep = '') %>%
        
        # Apply nice kableExtra format
        kable_styling() %>%
        
        # Center-align header row
        row_spec(0, align = 'c')
      
      # LaTeX format
    } else {
      
      # If interaction_symbol_x = TRUE, replace double colons with times 
      # symbols followed by line breaks and indentation. Then, replace 
      # single colons with times symbols.
      if(interaction_symbol_x) {
        model_summary$Effect = model_summary$Effect %>% 
          gsub('::', ' $\\\\times$ \n \\\\hspace{0.3cm}', .) %>%
          gsub(':', ' $\\\\times$ ', .)
      }
      
      model_summary$Effect = model_summary$Effect %>%
        
        # Escape underscores to avoid error in table
        str_replace_all('_', '\\\\_') %>%
        
        # Allow line breaks in the names of the effects
        # (used in the interactions)
        kableExtra::linebreak(align = 'l')
      
      # Output table
      model_summary %>% 
        
        # Remove header of first column and rename other headers
        rename(' ' = 'Effect', '$\\upbeta$' = 'Estimate', '$SE$' = 'SE', 
               '95\\% CrI' = 'CrI_95', '$\\widehat R$' = 'Rhat') %>%
        
        # Present table
        kbl(digits = 2, booktabs = TRUE, escape = FALSE, align = align,
            
            # Caption of the table (default unless specified)
            caption = caption, 
            
            # Disable occasional line gap (https://stackoverflow.com/a/49018919/7050882)
            linesep = '') %>%
        
        # Apply nice kableExtra format
        kable_styling() %>%
        
        # Center-align header row
        row_spec(0, align = 'c')
    }
  }

The function in use

# Create table (using custom function from the 'R_functions' folder)
bayesian_model_table(
  semanticpriming_summary_weaklyinformativepriors_exgaussian,
  order_effects = c('(Intercept)',
                    'Attentional control',
                    'Vocabulary size <sup>a</sup>',
                    'Gender <sup>a</sup>',
                    'Word frequency',
                    'Number of syllables',
                    'Word-concreteness difference',
                    'Language-based similarity <sup>b</sup>',
                    'Visual-strength difference <sup>b</sup>',
                    'Stimulus onset asynchrony (SOA) <sup>b</sup>',
                    'Word-concreteness difference :: Vocabulary size',
                    'Word-concreteness difference : SOA',
                    'Word-concreteness difference : Gender',
                    'Language-based similarity :: Attentional control',
                    'Visual-strength difference :: Attentional control',
                    'Language-based similarity :: Vocabulary size',
                    'Visual-strength difference :: Vocabulary size',
                    'Language-based similarity : Gender',
                    'Visual-strength difference : Gender',
                    'Language-based similarity : SOA <sup>b</sup>',
                    'Visual-strength difference : SOA <sup>b</sup>'),
  
  # Replace colons in the names of interactions with times symbols
  interaction_symbol_x = TRUE, 
  
  # No title
  caption = NULL) %>%
  
  # Group predictors under headings
  pack_rows('Individual differences', 2, 4) %>% 
  pack_rows('Target-word lexical covariates', 5, 6) %>% 
  pack_rows('Prime--target relationship', 7, 9) %>% 
  pack_rows('Task condition', 10, 10) %>% 
  pack_rows('Interactions', 11, 21) %>% 
  
  # Apply white background to override default shading in HTML output
  row_spec(1:21, background = 'white') %>%
  
  # Highlight covariates
  row_spec(c(2, 5:7, 11:15), background = '#FFFFF1') %>%
  
  # Format
  kable_classic(full_width = FALSE, html_font = 'Cambria') %>%
  
  # Footnote describing abbreviations, random slopes, etc. 
  # LaTeX code used to format the text.
  footnote(escape = FALSE, threeparttable = TRUE, general_title = '<br>', 
           general = paste('*Note*. &beta; = Estimate based on $z$-scored predictors; *SE* = standard error;',
                           'CrI = credible interval. Yellow rows contain covariates. Some interactions are ',
                           'split over two lines, with the second line indented. <br>', 
                           '<sup>a</sup> By-word random slopes were included for this effect.',
                           '<sup>b</sup> By-participant random slopes were included for this effect.'))
β SE 95% CrI \(\widehat{R}\)
(Intercept) 0.00 0.00 [0.00, 0.01] 1.00
Individual differences
Attentional control 0.00 0.00 [0.00, 0.01] 1.00
Vocabulary size a -0.01 0.00 [-0.01, 0.00] 1.00
Gender a 0.00 0.00 [0.00, 0.01] 1.00
Target-word lexical covariates
Word frequency -0.11 0.00 [-0.12, -0.11] 1.00
Number of syllables 0.07 0.00 [0.06, 0.07] 1.00
Prime–target relationship
Word-concreteness difference 0.01 0.00 [0.00, 0.01] 1.00
Language-based similarity b -0.06 0.00 [-0.07, -0.06] 1.00
Visual-strength difference b 0.01 0.00 [0.01, 0.01] 1.00
Task condition
Stimulus onset asynchrony (SOA) b 0.03 0.01 [0.02, 0.04] 1.00
Interactions
Word-concreteness difference ×
   Vocabulary size
0.00 0.00 [0.00, 0.00] 1.00
Word-concreteness difference × SOA 0.00 0.00 [0.00, 0.00] 1.00
Word-concreteness difference × Gender 0.00 0.00 [0.00, 0.00] 1.00
Language-based similarity ×
   Attentional control
0.00 0.00 [-0.01, 0.00] 1.00
Visual-strength difference ×
   Attentional control
0.00 0.00 [0.00, 0.00] 1.00
Language-based similarity ×
   Vocabulary size
0.00 0.00 [-0.01, 0.00] 1.00
Visual-strength difference ×
   Vocabulary size
0.00 0.00 [0.00, 0.00] 1.00
Language-based similarity × Gender 0.00 0.00 [-0.01, 0.00] 1.00
Visual-strength difference × Gender 0.00 0.00 [0.00, 0.00] 1.00
Language-based similarity × SOA b 0.00 0.00 [0.00, 0.00] 1.00
Visual-strength difference × SOA b 0.00 0.00 [0.00, 0.00] 1.00

Note. β = Estimate based on \(z\)-scored predictors; SE = standard error; CrI = credible interval. Yellow rows contain covariates. Some interactions are split over two lines, with the second line indented.
a By-word random slopes were included for this effect. b By-participant random slopes were included for this effect.

Shading specific rows

Shading specific rows is done differently when the output is PDF, as shown below (see p. 170 in Bernabeu, 2022).

Session info

If you encounter any blockers while reproducing the table using the materials at https://osf.io/gt5uf, my current session info may be useful.

sessionInfo()
## R version 4.3.2 (2023-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 11 x64 (build 22621)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=English_United Kingdom.utf8 
## [2] LC_CTYPE=English_United Kingdom.utf8   
## [3] LC_MONETARY=English_United Kingdom.utf8
## [4] LC_NUMERIC=C                           
## [5] LC_TIME=English_United Kingdom.utf8    
## 
## time zone: Europe/Oslo
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] kableExtra_1.3.4    lmerTest_3.1-3      lme4_1.1-35.1      
## [4] Matrix_1.6-4        stringr_1.5.1       tibble_3.2.1       
## [7] dplyr_1.1.4         knitr_1.45          xaringanExtra_0.7.0
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.8          utf8_1.2.4          generics_0.1.3     
##  [4] xml2_1.3.6          blogdown_1.18       stringi_1.8.3      
##  [7] lattice_0.22-5      digest_0.6.33       magrittr_2.0.3     
## [10] evaluate_0.23       grid_4.3.2          bookdown_0.37      
## [13] fastmap_1.1.1       jsonlite_1.8.8      httr_1.4.7         
## [16] rvest_1.0.3         fansi_1.0.6         viridisLite_0.4.2  
## [19] scales_1.3.0        numDeriv_2016.8-1.1 jquerylib_0.1.4    
## [22] cli_3.6.2           rlang_1.1.2         munsell_0.5.0      
## [25] splines_4.3.2       withr_2.5.2         cachem_1.0.8       
## [28] yaml_2.3.8          tools_4.3.2         uuid_1.1-1         
## [31] nloptr_2.0.3        minqa_1.2.6         colorspace_2.1-0   
## [34] ggplot2_3.4.4       webshot_0.5.5       boot_1.3-28.1      
## [37] vctrs_0.6.5         R6_2.5.1            lifecycle_1.0.4    
## [40] MASS_7.3-60         pkgconfig_2.0.3     pillar_1.9.0       
## [43] bslib_0.6.1         gtable_0.3.4        glue_1.6.2         
## [46] Rcpp_1.0.11         systemfonts_1.0.5   xfun_0.41          
## [49] tidyselect_1.2.0    rstudioapi_0.15.0   htmltools_0.5.7    
## [52] nlme_3.1-164        svglite_2.1.3       rmarkdown_2.25     
## [55] compiler_4.3.2

References

Bernabeu, P. (2022). Language and sensorimotor simulation in conceptual processing: Multilevel analysis and statistical power. Lancaster University. https://doi.org/10.17635/lancaster/thesis/1795

Brauer, M., & Curtin, J. J. (2018). Linear mixed-effects models and the analysis of nonindependent data: A unified framework to analyze categorical and continuous independent variables that vary within-subjects and/or within-items. Psychological Methods, 23(3), 389–411. https://doi.org/10.1037/met0000159

Bürkner, P.-C., Gabry, J., Weber, S., Johnson, A., Modrak, M., Badr, H. S., Weber, F., Ben-Shachar, M. S., & Rabel, H. (2022). Package ’brms’. CRAN. https://cran.r-project.org/web/packages/brms/brms.pdf

Zhu, H. (2022). Package ’kableExtra’. CRAN. https://cran.r-project.org/web/packages/kableExtra/kableExtra.pdf

comments powered by Disqus