当使用lm_robust和texreg时,仅获得观测数:

xghobddn  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(142)

我运行了4个回归,规格不同,但每个模型都有x作为回归变量。
然后我使用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}

我怎么能做到这一点?
先谢谢你了!

gcuhipw9

gcuhipw91#

stargazer是我进行这种标准误差调整的首选包。

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

相关问题