Customizing the Stargazer Regression Table in R to Add Vertical Lines Between Columns

Customizing the Stargazer Regression Table in R

The Stargazer package is a popular tool for creating and customizing regression tables in R. It provides a simple and efficient way to generate high-quality tables that can be used in various contexts, such as research papers, presentations, or reports.

However, one common request from users is to add vertical lines between columns in the table. In this article, we will explore how to achieve this using the Stargazer package.

Understanding the Problem

The problem arises when you want to separate two columns in a Stargazer regression table with a vertical line. By default, the Stargazer package uses a comma-separated format for output, which makes it difficult to add vertical lines between columns.

To understand how this works, let’s take a look at an example code:

mod <- lm(data = iris, Sepal.Length ~ Species)
mod1 <- lm(data = iris, Sepal.Length ~ Petal.Width + Species)

stargazer(mod, mod1, type = "latex")

This will generate a LaTeX output that looks like this:

Dependent variable:       
  ------------------------------------------------
(1)           |          (2)          
  Petal.Width  |       0.917***        
              |        (0.194)        
              |                       
  Species     |        -0.060         
              |        (0.230)        
              |                       
  Constant    |       4.780***        
              |        (0.083)        
              |                       
  ------------------------------------------------
  Observations|          150          
  R2         |         0.669         
  Adjusted R2|         0.663         
  Residual Std. Error     0.481 (df = 146)    
  F Statistic         98.525*** (df = 3; 146)

As you can see, there are no vertical lines between the columns.

Solution

To add vertical lines between columns, we need to modify the output format of the Stargazer package. One way to achieve this is by using the capture.output function to capture the output of the stargazer function and then modifying it manually.

Here’s an example code that adds a vertical line between columns:

mod <- lm(data = iris, Sepal.Length ~ Species)
mod1 <- lm(data = iris, Sepal.Length ~ Petal.Width + Species)

mod1_sg <- capture.output(stargazer(mod, mod1, type = "text"))
library(stringr)
mod1_sg[6:25] <- paste(str_sub(mod1_sg[6:25], 1, 44), str_sub(mod1_sg[6:25], 46, 68), sep = "|")
mod1_df <- setNames(as.data.frame(noquote(mod1_sg))[-1], "")
print(mod1_df, row.names = FALSE)

This code captures the output of the stargazer function and stores it in a variable called mod1_sg. It then uses the stringr package to modify the output by adding a vertical line between columns. Finally, it prints the modified table to the console.

LaTeX Output

If you want to use LaTeX output instead, you can modify the code as follows:

mod <- lm(data = iris, Sepal.Length ~ Species)
mod1 <- lm(data = iris, Sepal.Length ~ Petal.Width + Species)

mod1_sg <- capture.output(stargazer(mod, mod1, type = "latex"))
mod1_sg <- sub("lcc", "lc|c", mod1_sg)
writeLines(mod1_sg)

This code uses the sub function to replace the string “lcc” with “lc|c” in the LaTeX output. This adds a vertical line between columns.

Conclusion

Adding vertical lines between columns in a Stargazer regression table is possible using the capture.output and stringr packages. By modifying the output format of the stargazer function, you can create tables that are more visually appealing and easier to read.

We hope this article has been helpful in demonstrating how to customize the Stargazer package for your specific needs. If you have any questions or need further assistance, please don’t hesitate to ask!


Last modified on 2024-07-15