Search code examples
rregressionstargazer

Grouping variables in Stargazer?


I have many models with several different outcomes and they all use the same independent variables.

A very basic example using the "salaries" dataset from the cars package

str(Salaries)

model1 <- glm(rank ~ discipline+sex, family='binomial', data=Salaries)

model2 <-  glm(discipline ~ rank+sex, family='binomial', data=Salaries)

stargazer(model1, model2,
          ci = T,
          apply.coef = exp,
          title = 'Binomial logistic regression', 
          digits = 2,
          model.names = T,
          font.size= "tiny",
          type = 'text', 
          out = 'test.html')

Gives me this

Binomial logistic regression
==============================================
                      Dependent variable:     
                  ----------------------------
                       rank       discipline  
                     logistic      logistic   
                       (1)            (2)     
----------------------------------------------
disciplineB           0.61**                  
                   (0.06, 1.16)               
                                              
rankAssocProf                       0.81**    
                                 (0.11, 1.52) 
                                              
rankProf                            0.57**    
                                 (0.01, 1.13) 
                                              
sexMale              2.14***        1.14***   
                   (1.38, 2.90)  (0.47, 1.82) 
                                              
Constant             3.36***        1.60***   
                   (2.59, 4.14)  (0.85, 2.35) 
                                              
----------------------------------------------
Observations           397            397     
Log Likelihood       -176.86        -271.21   
Akaike Inf. Crit.     359.72        550.42    
==============================================
Note:              *p<0.1; **p<0.05; ***p<0.01

But I would like to be able to group the variables (and add a named reference) that are the same and also categorise them so it looks like this:

Binomial logistic regression
==============================================
                      Dependent variable:     
                  ----------------------------
                       rank       discipline  
                     logistic      logistic   
                       (1)            (2)     
----------------------------------------------
Occupation variables
——————————————————
**Discipline** (ref = A) 

B               0.61**                  
                   (0.06, 1.16)               
                                              

**rank** (ref = AsstProf) 

AssocProf                      0.81**    
                                 (0.11, 1.52) 
                                              
Prof                            0.57**    
                                 (0.01, 1.13) 
Sociodemographic
——————————————————
**Sex** (ref = Female)
Male              2.14***        1.14***   
                   (1.38, 2.90)  (0.47, 1.82) 
                                              
Constant             3.36***        1.60***   
                   (2.59, 4.14)  (0.85, 2.35) 
                                              
----------------------------------------------
Observations           397            397     
Log Likelihood       -176.86        -271.21   
Akaike Inf. Crit.     359.72        550.42    
==============================================
Note:              *p<0.1; **p<0.05; ***p<0.01

Solution

  • I don't know if you can achieve this with stargazer, but you can do it relatively easily by using the modelsummary package (Disclaimer: I am the author) and the kableExtra package.

    See the documentation for how to further customize your tables: https://vincentarelbundock.github.io/modelsummary/

    library(modelsummary)
    library(kableExtra)
    
    mod1 <- lm(mpg ~ hp + drat + factor(cyl), mtcars)
    mod2 <- lm(wt ~ hp + drat + factor(cyl), mtcars)
    models <- list("A" = mod1, "B" = mod2)
    
    modelsummary(models) |>
        group_rows(start_row = 1, end_row = 6, group_label = "blah") |>
        group_rows(start_row = 7, end_row = 10, group_label = "cyl")
    

    enter image description here