1

I have three regressions that I am trying to include into one table using the -stargazer- function. I have the following code:

library(Jmisc)
library(tidyverse)
library(sandwich)
library(lmtest)
library(multiwayvcov)
library(stargazer)

set.seed(123)
df <- data.frame(
  x1 = rnorm(10, mean=0, sd=1),
  x2 = rnorm(10, mean=0, sd=1),
  y = rnorm(10, mean=0, sd=1)
)

r1 <- lm(y ~ x1 + x2, df)
cov1 <- vcovHC(r1, type="HC1", cluster="clustervar")
robust.se1 <- sqrt(diag(cov1))
t1 <- coef(r1)/robust.se1

r2 <- lm(y ~ x1, df)
cov2 <- vcovHC(r2, type="HC1", cluster="clustervar")
robust.se2 <- sqrt(diag(cov2))
t2 <- coef(r2)/robust.se2

r3 <- lm(y ~ x2, df)
cov3 <- vcovHC(r3, type="HC1", cluster="clustervar")
robust.se3 <- sqrt(diag(cov3))
t3 <- coef(r3)/robust.se2

stargazer(r1, r2, r3, 
se = NULL, 
t = list(t1, t2, t3), 
align=TRUE, 
type="html", 
nobs=TRUE, 
out="StargazerTest.txt")

The table that is produced reports standard errors as opposed to the t-statistics I created. This is most likely due to the -stargazer- function at the bottom. I have looked up the directory for it and still don't understand how to get it to do what I want.

halfer
  • 19,824
  • 17
  • 99
  • 186
Brennan
  • 419
  • 5
  • 17
  • 1
    You need to make this more reproducible for Stack Overflow, what is `coef`, how did you create it, what do you mean by "does not work"? Be more specific. Could you make an example that we can copypaste into the console ready-to-run? You need a vector, though, of type `v <- c(1, 1, 1) / 1:3` and put it into a list `list(v)`. – jay.sf Oct 15 '19 at 20:39
  • 1
    Sorry, I have updated above to clarify. coef was just a call to the coefficients from that particular regression. It should be much clearer how I use it above now. It is now reproducible. What would I need a vector for here? The t statistics? – Brennan Oct 15 '19 at 21:01

1 Answers1

2

As explained here, you can specify the values you want with report (since stargazer 5.0). In your case, remove se = NULL and t = list(t1, t2, t3) and put:

report = ('c*t')

such as:

stargazer(r1, r2, r3, 
          report = ('c*t'), 
          align=TRUE, 
          type="html", 
          nobs=TRUE, 
          out="StargazerTest.txt")

Edit: since you need to use the robust standard error, you should use the function coeftest (library lmtest) instead of computing the robust standard error manually. Below is an example on one of your regressions:

library(Jmisc)
library(tidyverse)
library(sandwich)
library(lmtest)
library(multiwayvcov)
library(stargazer)

set.seed(123)
df <- data.frame(
  x1 = rnorm(10, mean=0, sd=1),
  x2 = rnorm(10, mean=0, sd=1),
  y = rnorm(10, mean=0, sd=1)
)

r1 <- lm(y ~ x1 + x2, df)
cov1 <- vcovHC(r1, type="HC1", cluster="clustervar")
robust.se1 <- sqrt(diag(cov1))
t1 <- coef(r1)/robust.se1

foo <- coeftest(r1, vcov = vcovHC(r1, type = "HC1"))

stargazer(foo,  
          report = ('c*t'), 
          align=TRUE, 
          type="html", 
          nobs=TRUE,
          out="StargazerTest.txt")

Notice that foo gives the same t-values than t1 but also displays coefficients, se, etc. which allows stargazer to work properly

bretauv
  • 7,756
  • 2
  • 20
  • 57
  • Thank you, will this make use of the robust standard errors I computed though? I don't directly see how they would be called on here – Brennan Oct 15 '19 at 21:17
  • 1
    hum, I don't think that the robust se are taken into account here. I've found [this](https://economictheoryblog.com/2018/05/18/robust-standard-errors-in-stargazer/) but it does not solve your original problem, I will continue to search – bretauv Oct 15 '19 at 21:21
  • yeah that is what I had previously because I just wanted to use robust standard errors. I mean I use them in the denominator of when I calculate my t statistics but I think the -report( )- just calculates them based off the regular standard errors computed in the regression – Brennan Oct 15 '19 at 21:23
  • Works perfectly, thanks I will use this moving forward! – Brennan Oct 15 '19 at 22:32
  • 1
    Happy to help :) – bretauv Oct 16 '19 at 04:32