使用lm_robust时,和texreg只得到观测数:

问题描述 投票:0回答:1

我正在运行 4 个回归,具有不同的规格,但每个模型都有

x
作为回归变量。我正在使用
estimatr
包中的标准错误统计类型和集群

然后我使用

texreg
来获得格式良好的回归表,这是我的可重现示例:

library(estimatr)
library(texreg)

# Load data
data <- data.frame(
  x = rnorm(100),
  z = rnorm(100),
  b = rbinom(100, 1, 0.5),
  n = rpois(100, 10)
)
data$y <- 0.5 + 2 * data$x + 0.1 * data$z + 0.5 * data$b - 0.1 * (data$x - 0.5) ^ 2 + rnorm(100)

# Define models
model1 <- lm_robust(y ~ x, data = data, clusters = data$b, se_type = "stata")
model2 <- lm_robust(y ~ x + z, data = data, clusters = data$b, se_type = "stata")
model3 <- lm_robust(y ~ x + b, data = data, clusters = data$z, se_type = "stata")
model4 <- lm_robust(y ~ x + z + b, data = data, clusters = data$n, se_type = "stata")

# Create table with screenreg
texreg(
  list(model1, model2, model3, model4), table = FALSE,
  custom.model.names = c("Model 1", "Model 2", "Model 3", "Model 4"),
  custom.coef.map =  list("x" = "only coeff"),
  include.ci = FALSE, include.adjr = FALSE, include.rsquared = FALSE, inlcude.rmse = FALSE,
  include.nobs = TRUE,
  digits = 3
)

正如您在

include.x
部分所见,我只包括
n.obs
。我只是想要那个。但似乎代码忽略了那部分,因为这是我的结果。

\begin{tabular}{l c c c c}
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 \\
\hline
only coeff & $2.394^{*}$ & $2.397^{*}$ & $2.379^{***}$ & $2.382^{***}$ \\
           & $(0.095)$   & $(0.092)$   & $(0.100)$     & $(0.083)$     \\
\hline
R$^2$      & $0.840$     & $0.843$     & $0.855$       & $0.861$       \\
Adj. R$^2$ & $0.838$     & $0.840$     & $0.852$       & $0.857$       \\
Statistic  & $634.115$   & $$          & $282.093$     & $440.925$     \\
P Value    & $0.025$     & $$          & $0.000$       & $0.000$       \\
DF Resid.  & $1.000$     & $1.000$     & $99.000$      & $14.000$      \\
nobs       & $100$       & $100$       & $100$         & $100$         \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
\end{tabular}

我认为它与

lm_robust
功能有关(因为它只适用于
lm
),但我真的需要
lm_robust
因为标准错误(我需要它们与stata中获得的相同)

此外,我收到此警告:

Warning messages:
1: In data.frame(gof.names = colnames(out), gof = as.numeric(out),  :
  NAs introduced by coercion
2: In doTryCatch(return(expr), name, parentenv, handler) :
  texreg used the broom package to extract the following GOF measures, but could not cast them to numeric type: se_type
3: In data.frame(gof.names = colnames(out), gof = as.numeric(out),  :
  NAs introduced by coercion
4: In doTryCatch(return(expr), name, parentenv, handler) :
  texreg used the broom package to extract the following GOF measures, but could not cast them to numeric type: Statistictexreg used the broom package to extract the following GOF measures, but could not cast them to numeric type: P Valuetexreg used the broom package to extract the following GOF measures, but could not cast them to numeric type: se_type
5: In data.frame(gof.names = colnames(out), gof = as.numeric(out),  :
  NAs introduced by coercion
6: In doTryCatch(return(expr), name, parentenv, handler) :
  texreg used the broom package to extract the following GOF measures, but could not cast them to numeric type: se_type
7: In data.frame(gof.names = colnames(out), gof = as.numeric(out),  :
  NAs introduced by coercion
8: In doTryCatch(return(expr), name, parentenv, handler) :
  texreg used the broom package to extract the following GOF measures, but could not cast them to numeric type: se_type

我想要的结果是:

\begin{tabular}{l c c c c}
\hline
 & Model 1 & Model 2 & Model 3 & Model 4 \\
\hline
only coeff & $2.394^{*}$ & $2.397^{*}$ & $2.379^{***}$ & $2.382^{***}$ \\
           & $(0.095)$   & $(0.092)$   & $(0.100)$     & $(0.083)$     \\
\hline
nobs       & $100$       & $100$       & $100$         & $100$         \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
\end{tabular}

我怎样才能做到这一点?

提前致谢!

r latex regression
1个回答
0
投票

stargazer
是我进行此类标准误差调整的首选软件包。这是制作您想要的表格的示例代码,包括 Stata 风格的集群 SE。

library(lmtest)
library(sandwich)
library(stargazer)

set.seed(44)

# Load data
data <- data.frame(
  x = rnorm(100),
  z = rnorm(100),
  b = rbinom(100, 1, 0.5),
  n = rpois(100, 10)
)
data$y <- 0.5 + 2 * data$x + 0.1 * data$z + 0.5 * data$b - 0.1 * (data$x - 0.5) ^ 2 + rnorm(100)

# Define models
model1 <- lm(y ~ x,     data = data)
model2 <- lm(y ~ x + z, data = data)
model3 <- lm(y ~ x + b, data = data)
model4 <- lm(y ~ x + z + b, data = data)

# Make clustered SEs
se1 = as.vector(coeftest(model1,vcov = vcovCL,cluster = ~b, type="HC1")[,"Std. Error"])
se2 = as.vector(coeftest(model2,vcov = vcovCL,cluster = ~b,  type="HC1")[,"Std. Error"])
se3 = as.vector(coeftest(model3,vcov = vcovCL,cluster = ~z,  type="HC1")[,"Std. Error"])
se4 = as.vector(coeftest(model4,vcov = vcovCL,cluster = ~n,  type="HC1")[,"Std. Error"])

stargazer(model1,model2,model3,model4,type="latex",
          se=list(se1,se2,se3,se4), omit=c("Constant","z","b"),
          omit.stat = c("f","ser","adj.rsq","rsq"))

如果你在 stargazer 命令中执行

type="text"
,你会看到这个表:

================================================
                     Dependent variable:        
             -----------------------------------
                              y                 
               (1)      (2)      (3)      (4)   
------------------------------------------------
x            2.071*** 2.079*** 2.123*** 2.135***
             (0.103)  (0.124)  (0.149)  (0.111) 
                                                
------------------------------------------------
Observations   100      100      100      100   
================================================
Note:                *p<0.1; **p<0.05; ***p<0.01
© www.soinside.com 2019 - 2024. All rights reserved.