Image credit: Windows theme

Organize Regression Tables in R Markdowm

There are various ways to organize your regression output tables in the project. The R packages such as stargazer and pander give simple solutions. I give an example of using the stargazer package to present a regression table in word documents.

In Rstudio, go to Tools and Install Packages. Install the stargazer package. Let us use the subprime mortgage dataset as an example in the RMD file. Please download the data here.

# Load data.
Mortgage = read.csv("subprime.csv", header = TRUE)

# Change variable names.
colnames(Mortgage) = c("APR", "LTV", "Credit_Score", "Stated_Income", "Home_Value")

# Run a multiple regression and save the output to the object reg_output.
reg_output = lm(APR ~ LTV + Credit_Score + Stated_Income + Home_Value, data = Mortgage)

# Load the stargazer library.
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
stargazer(reg_output, type = "text")
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                 APR            
## -----------------------------------------------
## LTV                          -1.589***         
##                               (0.520)          
##                                                
## Credit_Score                 -0.018***         
##                               (0.001)          
##                                                
## Stated_Income                 0.0004           
##                               (0.003)          
##                                                
## Home_Value                    -0.001           
##                               (0.001)          
##                                                
## Constant                     23.725***         
##                               (0.686)          
##                                                
## -----------------------------------------------
## Observations                    372            
## R2                             0.463           
## Adjusted R2                    0.457           
## Residual Std. Error      1.244 (df = 367)      
## F Statistic           79.142*** (df = 4; 367)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

In preparing the document, you may want to hide the code and suppress any messages such as warnings and citations from loading the packages. To do that, set the option echo = FALSE and use suppressMessages() around the library() line.

# Load the library and supress all messages.
suppressMessages(library(stargazer))

stargazer(reg_output, type = "text", title = "Regression Results")
## 
## Regression Results
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                 APR            
## -----------------------------------------------
## LTV                          -1.589***         
##                               (0.520)          
##                                                
## Credit_Score                 -0.018***         
##                               (0.001)          
##                                                
## Stated_Income                 0.0004           
##                               (0.003)          
##                                                
## Home_Value                    -0.001           
##                               (0.001)          
##                                                
## Constant                     23.725***         
##                               (0.686)          
##                                                
## -----------------------------------------------
## Observations                    372            
## R2                             0.463           
## Adjusted R2                    0.457           
## Residual Std. Error      1.244 (df = 367)      
## F Statistic           79.142*** (df = 4; 367)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

This will give a regression output table in your word document.

You will need to run a simple linear regression to analyze the marginal effect and it is better to also present the related regression output in the word document. stargazer makes it easy to present multiple regression tables. Suppose we want to analyze the marginal effect of credit score on APR.

# Run the simple linear regression for marginal analysis.
reg_output_simple =lm(APR ~ Credit_Score, data = Mortgage)

# Combine the two regressions in one output table.
stargazer(reg_output, reg_output_simple, type = "text",title = "Regression Results")
## 
## Regression Results
## ====================================================================
##                                   Dependent variable:               
##                     ------------------------------------------------
##                                           APR                       
##                               (1)                     (2)           
## --------------------------------------------------------------------
## LTV                        -1.589***                                
##                             (0.520)                                 
##                                                                     
## Credit_Score               -0.018***               -0.021***        
##                             (0.001)                 (0.001)         
##                                                                     
## Stated_Income               0.0004                                  
##                             (0.003)                                 
##                                                                     
## Home_Value                  -0.001                                  
##                             (0.001)                                 
##                                                                     
## Constant                   23.725***               23.591***        
##                             (0.686)                 (0.656)         
##                                                                     
## --------------------------------------------------------------------
## Observations                  372                     372           
## R2                           0.463                   0.448          
## Adjusted R2                  0.457                   0.447          
## Residual Std. Error    1.244 (df = 367)         1.256 (df = 370)    
## F Statistic         79.142*** (df = 4; 367) 300.736*** (df = 1; 370)
## ====================================================================
## Note:                                    *p<0.1; **p<0.05; ***p<0.01

This saves space and makes the results easy to compare.