Search code examples
rgtsummary

Adding total N's f variable to tbl_regression using gtsummary package


I am trying to add a column that has total number of observations in each variable in my tbl_regression table. In tbl_summary, the function add_n() allows you to do this. However, in tbl_regession this option is not straight forward.

I have tried to do this by including N argument in the modify_header function in tbl_regression, but here the issue is that the levels within the variables have the N's of the variable as well. For example, using the code below, the Species variable has 150 observations and all of the levels of Species have N's equal to 150 (see image below). Is there any way of having N's for the Species variable but not for the levels? Any help is kindly appreciated.

# load packages
library(gtsummary)
theme_gtsummary_compact()

# build model
mod <- lm(Petal.Width ~ Species + Petal.Length, data = iris) %>%
  tbl_regression(exponentiate = TRUE) %>%
  modify_header(update = list(
  estimate ~ '**Coefficient**',
  ci~ '**95% CI**',
  N ~ '**N**'
))

mod

enter image description here


Solution

  • The add_n() function includes the number of observations from the model, and can also report the Ns within a categorical variable. Here are a few examples. All examples are printed with kable instead of gt to accurately appear in the reprex.

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.4.0'
    
    # build model
    tbl <- 
      lm(Petal.Width ~ Species + Petal.Length, data = iris) %>%
      tbl_regression() %>%
      bold_labels()
    
    # add N to the header
    tbl %>%
      modify_header(update = label ~ "**Characteristic (N = {n})**") %>%
      as_kable()
    
    Characteristic (N = 150) Beta 95% CI p-value
    Species
    setosa
    versicolor 0.44 0.23, 0.64 <0.001
    virginica 0.84 0.55, 1.1 <0.001
    Petal.Length 0.23 0.16, 0.30 <0.001
    # add N to variable label rows
    tbl %>%
      add_n() %>%
      as_kable()
    
    Characteristic N Beta 95% CI p-value
    Species 150
    setosa
    versicolor 0.44 0.23, 0.64 <0.001
    virginica 0.84 0.55, 1.1 <0.001
    Petal.Length 150 0.23 0.16, 0.30 <0.001
    # add N to variable label and level rows
    tbl %>%
      add_n(location = "level") %>%
      as_kable()
    
    Characteristic N Beta 95% CI p-value
    Species
    setosa 50
    versicolor 50 0.44 0.23, 0.64 <0.001
    virginica 50 0.84 0.55, 1.1 <0.001
    Petal.Length 150 0.23 0.16, 0.30 <0.001

    Created on 2021-04-15 by the reprex package (v2.0.0)