我运行了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}
我怎么能做到这一点?
先谢谢你了!
1条答案
按热度按时间gcuhipw91#
stargazer
是我进行这种标准误差调整的首选包。如果你在stargazer命令中执行
type="text"
,你会看到这个表: