R

Formatted summary tables R

One of the slightly annoying issues I've had with using the summary() function in R are the multiple steps it takes to get the parameters from a fitted model into a format that's useful for pasting into a manuscript.

After a lot of trial and error, the workflow I settled on used Daniel Lüdecke's sjstats and sjPlot packages. sjPlot has the very helpful plot_model() function and tab_model() functions, that make it very easy to plot marginal effects and interactions, or render HTML tables that can be cut and pasted.

However, sjstats also used to contained std_beta(), a function would could be passed a fitted model object (e.g. from lmer() ) and would returned standardized coefficients, errors, and 95% confidence intervals. std_beta() has not been deprecated in favor of standardize_parameters() from the effectsize package.

Long story short, I've adapted my workflow to use effectsize and it's companion package parameters to quickly extract and combine unstandardized and standardized parameters estimates, then use from tidyverse to wrangle them into one big tibble fit for exporting. The code below is just a snippet of R functions. I might roll them into the github repository with a bit more flourish eventually.

require(tidyverse)
require(effectsize)
require(parameters)

# Function takes a fitted model (e.g. from lmer) and returns a
# tibble with unstandardized coefficients, standardized coefficients,
# and 95% confidence intervals.
get_coefs <- function(mod) {
  # unstandardized
  params <- model_parameters(mod)
  # standardized
  params_std <- standardize_parameters(mod)
  # merge, rename, and recalculate
  params_std <- params_std %>% tibble() %>% select(-CI) %>% 
    rename(Std_CI_low = CI_low, Std_CI_high = CI_high) %>% 
    mutate(Std_SE = (Std_CI_high - Std_CI_low) / 3.92)
  # The '/ 3.92' calculates the "standardized standard error" 
  # from the 95% confidence interval using algebra

  coefs <- cbind(params, params_std[,-1])
  rownames(coefs) <- coefs$Parameter
  return(coefs)
}

I've been using this primarily to conduct a meta-analysis of several studies. With the metafor package, it's straightforward, as long as you have easy access to standardized effect size estimates and standard error estimates. The get_coefs() above makes it easy to extract those values from a fitted model. I then wrote some helper functions to make it easier to pull out the info I need for the meta analysis.

# Convenience to extract standardized beta
extract_eff <- function(coefs, label) {
  eff <- coefs[label, 'Std_Coefficient']
  return(eff)
}

# Convenience to extract standardized standard error
extract_se <- function(coefs, label) {
  se <- coefs[label, 'Std_SE']
  return(se)
}

# Convenience to extract both standardized beta and SE
extract_param <- function(coefs, label) {
  out <- c('eff' = extract_eff(coefs, label), 
           'se' = extract_se(coefs, label))
  return(out)
}

3 Comments