Learn R Programming

⚠️There's a newer version (1.19.0) of this package.Take me there.

broom.helpers

The broom.helpers package provides suite of functions to work with regression model broom::tidy() tibbles. The suite includes functions to group regression model terms by variable, insert reference and header rows for categorical variables, add variable labels, and more.

Installation

This package is still experimental and under development.

To install it for testing purpose, use:

devtools::install_github("larmarange/broom.helpers")

Examples

all-in-one wrapper

mod1 <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris)
library(broom.helpers)
mod1 %>% tidy_plus_plus()
#> # A tibble: 4 x 14
#>   term  variable var_label var_class var_type contrasts reference_row label
#>   <chr> <chr>    <chr>     <chr>     <chr>    <chr>     <lgl>         <chr>
#> 1 Sepa~ Sepal.W~ Sepal.Wi~ numeric   continu~ <NA>      NA            Sepa~
#> 2 Spec~ Species  Species   factor    categor~ contr.tr~ TRUE          seto~
#> 3 Spec~ Species  Species   factor    categor~ contr.tr~ FALSE         vers~
#> 4 Spec~ Species  Species   factor    categor~ contr.tr~ FALSE         virg~
#> # ... with 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> #   p.value <dbl>, conf.low <dbl>, conf.high <dbl>

mod2 <- glm(
  response ~ poly(age, 3) + stage + grade * trt,
  na.omit(gtsummary::trial),
  family = binomial,
  contrasts = list(
    stage = contr.treatment(4, base = 3),
    grade = contr.sum
  )
)
mod2 %>% 
  tidy_plus_plus(
    exponentiate = TRUE,
    variable_labels = c(age = "Age (in years)"),
    add_header_rows = TRUE,
    show_single_row = "trt"
  )
#> # A tibble: 17 x 15
#>    term  variable var_label var_class var_type header_row contrasts
#>    <chr> <chr>    <chr>     <chr>     <chr>    <lgl>      <chr>    
#>  1 <NA>  age      Age (in ~ nmatrix.3 continu~ TRUE       <NA>     
#>  2 poly~ age      Age (in ~ nmatrix.3 continu~ FALSE      <NA>     
#>  3 poly~ age      Age (in ~ nmatrix.3 continu~ FALSE      <NA>     
#>  4 poly~ age      Age (in ~ nmatrix.3 continu~ FALSE      <NA>     
#>  5 <NA>  stage    T Stage   factor    categor~ TRUE       contr.tr~
#>  6 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#>  7 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#>  8 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#>  9 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#> 10 <NA>  grade    Grade     factor    categor~ TRUE       contr.sum
#> 11 grad~ grade    Grade     factor    categor~ FALSE      contr.sum
#> 12 grad~ grade    Grade     factor    categor~ FALSE      contr.sum
#> 13 grad~ grade    Grade     factor    categor~ FALSE      contr.sum
#> 14 trtD~ trt      Chemothe~ character categor~ NA         contr.tr~
#> 15 <NA>  grade:t~ Grade * ~ <NA>      interac~ TRUE       <NA>     
#> 16 grad~ grade:t~ Grade * ~ <NA>      interac~ FALSE      <NA>     
#> 17 grad~ grade:t~ Grade * ~ <NA>      interac~ FALSE      <NA>     
#> # ... with 8 more variables: reference_row <lgl>, label <chr>, estimate <dbl>,
#> #   std.error <dbl>, statistic <dbl>, p.value <dbl>, conf.low <dbl>,
#> #   conf.high <dbl>

fine control

mod1 %>%
  # perform initial tidying of model
  tidy_and_attach() %>%
  # add reference row
  tidy_add_reference_rows() %>%
  # add term labels
  tidy_add_term_labels() %>%
  # remove intercept
  tidy_remove_intercept()
#> # A tibble: 4 x 12
#>   term  variable var_label var_class var_type contrasts reference_row label
#>   <chr> <chr>    <chr>     <chr>     <chr>    <chr>     <lgl>         <chr>
#> 1 Sepa~ Sepal.W~ Sepal.Wi~ numeric   continu~ <NA>      NA            Sepa~
#> 2 Spec~ Species  Species   factor    categor~ contr.tr~ TRUE          seto~
#> 3 Spec~ Species  Species   factor    categor~ contr.tr~ FALSE         vers~
#> 4 Spec~ Species  Species   factor    categor~ contr.tr~ FALSE         virg~
#> # ... with 4 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> #   p.value <dbl>

mod2 %>%
  # perform initial tidying of model
  tidy_and_attach(exponentiate = TRUE) %>%
  # add variable labels, including a custom value for age
  tidy_add_variable_labels(labels = c(age = "Age in years")) %>%
  # add reference rows for categorical variables
  tidy_add_reference_rows() %>%
  # add a, estimate value of reference terms
  tidy_add_estimate_to_reference_rows(exponentiate = TRUE) %>%
  # add header rows for categorical variables
  tidy_add_header_rows()
#> # A tibble: 20 x 13
#>    term  variable var_label var_class var_type header_row contrasts
#>    <chr> <chr>    <chr>     <chr>     <chr>    <lgl>      <chr>    
#>  1 (Int~ <NA>     (Interce~ <NA>      interce~ NA         <NA>     
#>  2 <NA>  age      Age in y~ nmatrix.3 continu~ TRUE       <NA>     
#>  3 poly~ age      Age in y~ nmatrix.3 continu~ FALSE      <NA>     
#>  4 poly~ age      Age in y~ nmatrix.3 continu~ FALSE      <NA>     
#>  5 poly~ age      Age in y~ nmatrix.3 continu~ FALSE      <NA>     
#>  6 <NA>  stage    T Stage   factor    categor~ TRUE       contr.tr~
#>  7 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#>  8 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#>  9 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#> 10 stag~ stage    T Stage   factor    categor~ FALSE      contr.tr~
#> 11 <NA>  grade    Grade     factor    categor~ TRUE       contr.sum
#> 12 grad~ grade    Grade     factor    categor~ FALSE      contr.sum
#> 13 grad~ grade    Grade     factor    categor~ FALSE      contr.sum
#> 14 grad~ grade    Grade     factor    categor~ FALSE      contr.sum
#> 15 <NA>  trt      Chemothe~ character categor~ TRUE       contr.tr~
#> 16 trtD~ trt      Chemothe~ character categor~ FALSE      contr.tr~
#> 17 trtD~ trt      Chemothe~ character categor~ FALSE      contr.tr~
#> 18 <NA>  grade:t~ Grade * ~ <NA>      interac~ TRUE       <NA>     
#> 19 grad~ grade:t~ Grade * ~ <NA>      interac~ FALSE      <NA>     
#> 20 grad~ grade:t~ Grade * ~ <NA>      interac~ FALSE      <NA>     
#> # ... with 6 more variables: reference_row <lgl>, label <chr>, estimate <dbl>,
#> #   std.error <dbl>, statistic <dbl>, p.value <dbl>

Copy Link

Version

Install

install.packages('broom.helpers')

Monthly Downloads

16,005

Version

1.0.0

License

GPL-3

Maintainer

Joseph Larmarange

Last Published

September 18th, 2020

Functions in broom.helpers (1.0.0)

model_identify_variables

Identify for each coefficient of a model the corresponding variable
model_get_model_frame

Get the model frame of a model
model_get_xlevels

Get xlevels used in the model
model_get_model_matrix

Get the model matrix of a model
model_list_terms_levels

List levels of categorical terms
tidy_add_contrasts

Add contrasts type for categorical variables
model_get_contrasts

Get contrasts used in the model
reexports

Objects exported from other packages
model_list_contrasts

List contrasts used by a model
tidy_add_estimate_to_reference_rows

Add an estimate value to references rows for categorical variables
model_list_variables

List all the variables used in a model
tidy_add_variable_labels

Add variable labels
tidy_plus_plus

Tidy a model and compute additional informations
tidy_identify_variables

Identify the variable corresponding to each model coefficient
tidy_add_header_rows

Add header rows variables with several terms
tidy_attach_model

Attach a full model to the tibble of model terms
tidy_add_term_labels

Add term labels
tidy_add_reference_rows

Add references rows for categorical variables
tidy_remove_intercept

Remove intercept(s)