我尝试使用-stargazer-函数将三个回归包含到一个表中。我有以下代码:
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")
生成的表报告标准错误,而不是我创建的t统计量。这很可能是由于底部的-stargazer-功能所致。我已经为它查找目录,但仍然不知道如何使它执行我想要的操作。任何帮助将不胜感激!
答案 0 :(得分:1)
如here所述,您可以使用report
指定所需的值(自stargazer
5.0起)。根据您的情况,删除se = NULL
和t = list(t1, t2, t3)
并放入:
report = ('c*t')
例如:
stargazer(r1, r2, r3,
report = ('c*t'),
align=TRUE,
type="html",
nobs=TRUE,
out="StargazerTest.txt")
编辑:由于您需要使用可靠的标准误差,因此应使用函数coeftest
(库lmtest
),而不是手动计算可靠的标准误差。以下是您其中一项回归的示例:
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")
请注意,foo
给出的t值与t1
相同,但还会显示系数,se等,从而使stargazer
正常工作