I am working with the lm
command and the coeftest
command. I want to put them together into a stargazer
table. The problem is that the stargazer
output is incomplete.
Here is what I have so far. The left side is the OLS model. On the right side is the OLS model passed through the coeftest
command.
=================================================
Dependent Variable:
-----------------------------
VAP Turnout (%)
(1) (2)
-------------------------------------------------
X -0.010 -0.010
(0.031) (0.035)
Constant 495.158*** 495.158***
(17.493) (20.561)
-------------------------------------------------
Observations 1,000
Adjusted R2 -0.001
Residual Std. Error 287.218 (df = 998)
=================================================
Note: *p<0.1; **p<0.05; ***p<0.01
VAP Turnout (%)
back to the center in the stargazer table?Observations
, Adjusted R2
, and Residual Std. Error
into the blank area under model 2 (the OLS object passed through the coeftest
command).Thank you for your help!
Here is the code to reproduce the table:
#Packages
library(stargazer)
library(lmtest)
library(sandwich)
#Set Seed
set.seed(1993)
#Create Sample Data
X <- sample(seq(1, 1000), 1000, replace = T)
Y <- sample(seq(1, 1000), 1000, replace = T)
Cluster <- sample(letters, 1000, replace = T)
df <- data.frame(X, Y, Cluster)
#Model 1 - OLS
Base <- lm(Y ~ X, data = df)
#Model 2 - Coeftest
Coeff_Test <- coeftest(Base, vcovCL, cluster = df$Cluster)
#Stargazer table
stargazer(list(Base, Coeff_Test), type = "text",omit=c("Cluster"),
model.names = FALSE,
dep.var.caption = c("Dependent Variable:"),
dep.var.labels = c("VAP Turnout (%)"), #Write the DV Here
omit.stat = c("f","rsq"))
Extract the standard errors and p-values from the coeftest
using the tidy
function in the broom
package. From there, you can manually input both the standard errors and the p-values into the stargazer function using the se
and p
arguments.
Note that when you use the NULL
in the se
and p
arguments, it manually defaults to whatever object is listed in the corresponding position. In this case, we use it in the first position to default to the base object but input the standard errors and p-values manually into the second position.
Lastly, since you are simply clustering the standard errors, the coefficients and model specification values stay the same. So, we can
can put the corresponding model, the object base
, into the second argument, so it feeds in the coefficients and model specification values while we override the se and p-values ourselves.
#Packages
library(stargazer)
library(lmtest)
library(sandwich)
#Set Seed
set.seed(1993)
#Create Sample Data
X <- sample(seq(1, 1000), 1000, replace = T)
Y <- sample(seq(1, 1000), 1000, replace = T)
Cluster <- sample(letters, 1000, replace = T)
df <- data.frame(X, Y, Cluster)
#Model 1 - OLS
Base <- lm(Y ~ X, data = df)
#Model 2 - Coeftest
Coeff_Test <- coeftest(Base, vcovCL, cluster = df$Cluster)
#Make into a DF and extract the SE and P-Values
library(broom)
Coeff_Test_DF <- tidy(Coeff_Test)
#Coeff_Test_DF$std.error
#Coeff_Test_DF$p.value
#Stargazer table
stargazer(list(Base, Base), type = "text",omit=c("Cluster"),
model.names = FALSE,
se = list(NULL, Coeff_Test_DF$std.error),
p = list(NULL, Coeff_Test_DF$p.value),
dep.var.caption = c("Dependent Variable:"),
dep.var.labels = c("VAP Turnout (%)"), #Write the DV Here
omit.stat = c("f","rsq"))
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 495.1580645 20.5610666 24.0823 <0.0000000000000002 ***
X -0.0098368 0.0346685 -0.2837 0.7767
---
===========================================================
Dependent Variable:
----------------------------
VAP Turnout (%)
(1) (2)
-----------------------------------------------------------
X -0.010 -0.010
(0.031) (0.035)
Constant 495.158*** 495.158***
(17.493) (20.561)
-----------------------------------------------------------
Observations 1,000 1,000
Adjusted R2 -0.001 -0.001
Residual Std. Error (df = 998) 287.218 287.218
===========================================================
Note: *p<0.1; **p<0.05; ***p<0.01