Search code examples
rstargazer

How to move the Observations row manually in a stargazer table in R and to stack entries?


I have an example regression table in R that was created using the stargazer package:

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
se1 <- round(coeftest(lm.D9, vcov = vcovHC(lm.D9, type="HC1"))[,2],4)
stargazer(mod1,
          se=list(se1),keep.stat="n", type="text",
          add.lines = list(c("Method","OLS"),
                           c("HR Robust","True True True")))

with the Stargazer output:

========================================
                 Dependent variable:    
             ---------------------------
                       workedm          
----------------------------------------
morekids              -0.115***         
                       (0.002)          
                                        
Constant              0.575***          
                       (0.001)          
                                        
----------------------------------------
Method                   OLS            
HR Robust               True True True            
Observations           266,928          
========================================
Note:        *p<0.1; **p<0.05; ***p<0.01

I would like to shift the Observations row above Method and HR Robust. Additionally, I would like to indent each "True True True" portion so that each True is on top of the other:

...
Method                   OLS            
HR Robust               True 
                        True 
                        True            
Observations           266,928   
...

Is there a way to do this? Thanks!


Solution

  • You could use the table.layout argument to customize the order of the elements you want to show in your output:

    a character string that specifies which parts of the table should be included in the output, in the order provided by the user. Each letter in the string indicates a particular part of the table, as specified by the table layout characters. For instance, table.layout = "#tn" will report the model numbers, coefficient table and notes only.

    To get the TRUE below each other, you could simply create two vectors with some empty value to get them below in each. Here is some reproducible code (note I use se1 as model as example):

    library(stargazer)
    library(lmtest)
    library(sandwich)
    group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
    weight <- c(ctl, trt)
    lm.D9 <- lm(weight ~ group)
    se1 <- round(coeftest(lm.D9, vcov = vcovHC(lm.D9, type="HC1"))[,2],4)
    stargazer(lm.D9,
              se=list(se1), keep.stat="n", type="text",
              add.lines = list(c("Method","OLS"),
                               c("HR Robust","True"),
                               c("", "True"),
                               c("", "True")),
              table.layout = "=ld-t-sa=n")
    #> 
    #> ========================================
    #>                  Dependent variable:    
    #>              ---------------------------
    #>                        weight           
    #> ----------------------------------------
    #> groupTrt               -0.371           
    #>                        (0.311)          
    #>                                         
    #> Constant              5.032***          
    #>                        (0.184)          
    #>                                         
    #> ----------------------------------------
    #> Observations             20             
    #> Method                   OLS            
    #> HR Robust               True            
    #>                         True            
    #>                         True            
    #> ========================================
    #> Note:        *p<0.1; **p<0.05; ***p<0.01
    

    Created on 2023-04-15 with reprex v2.0.2

    As you can see we now have the order observations, method and HR Robust.


    Here you can find the characters to create the order with each description:

    Table Layout Characters
    "-" single horizontal line
    "=" double horizontal line
    "-!"    mandatory single horizontal line
    "=!"    mandatory double horizontal line
    "l" dependent variable caption
    "d" dependent variable labels
    "m" model label
    "c" column labels
    "#" model numbers
    "b" object names
    "t" coefficient table
    "o" omitted coefficient indicators
    "a" additional lines
    "n" notes
    "s" model statistics