rstargazer

How can I display descriptive statistics next to each other by using stargazer?


My dataset has a dummy variable which divides the data set into two groups. I would like to display the descriptive statistics for both next to each other, like:

example

using stargazer. Is this possible?

For example, if there is the mtcars data set and the variable $am divides the dataset into two groups, how can I display the one group on the left side and the other group on the other side?

Thank you!

I was able to display the two statistics below each other (I had to make two separate datasets for each group), but never next to each other.

treated <- mtcars[mtcars$am == 1,]
control <- mtcars[mtcars$am == 0,]

stargazer(treated, control, keep=c("mpg", "cyl", "disp", "hp"), 
          header=FALSE, title="Descriptive statistics", digits=1, type="text")

Descriptive statistics below each other


Solution

  • Someone should point out if I'm mistaken, but I don't believe that stargazer will allow for the kind of nested tables you are looking for. However, there are other packages like modelsummary, gtsummary, and flextable that can produce tables similar to stargazer. I have included examples below using select mtcars variables summarized by am. Personally, I prefer gtsummary due to its flexibility.

    library(tidyverse)
    data(mtcars)
    
    ### modelsummary
    # not great since it treats `cyl` as a continuous variable
    # https://vincentarelbundock.github.io/modelsummary/articles/datasummary.html
    library(modelsummary)
    datasummary_balance(~am, data = mtcars, dinm = FALSE)
    
    ### gtsummary
    # based on example 3 from here
    # https://www.danieldsjoberg.com/gtsummary/reference/add_stat_label.html
    library(gtsummary)
    mtcars %>%
      select(am, mpg, cyl, disp, hp) %>%
      tbl_summary(
        by = am, 
        missing = "no",
        type = list(mpg ~ 'continuous2',
                    cyl ~ 'categorical',
                    disp ~ 'continuous2',
                    hp ~ 'continuous2'),
        statistic = all_continuous2() ~ c("{mean} ({sd})", "{median}")
        ) %>%
      add_stat_label(label = c(mpg, disp, hp) ~ c("Mean (SD)", "Median")) %>%
      modify_footnote(everything() ~ NA)
    
    ### flextable
    # this function only works on continuous vars, so I removed `cyl`
    # https://davidgohel.github.io/flextable/reference/continuous_summary.html
    library(flextable)
    mtcars %>%
      select(am, mpg, cyl, disp, hp) %>%
      continuous_summary(
        by = "am",
        hide_grouplabel = FALSE,
        digits = 3
      )