当使用lm_robust时,以及texreg仅获取观察数:

huangapple go评论98阅读模式
英文:

When using lm_robust, and texreg get only the number of observations:

问题

Here's the translated part of your request:

  1. 我正在运行4个回归模型,具有不同的规格,但每个模型都将 `x` 作为回归器。我正在使用`estimatr`包中的标准错误类型和来自`estimatr`包的聚类。
  2. 然后我使用`texreg`来获得一个格式良好的回归表,这是我的可重现示例:
  3. ```R
  4. library(estimatr)
  5. library(texreg)
  6. # 加载数据
  7. data <- data.frame(
  8. x = rnorm(100),
  9. z = rnorm(100),
  10. b = rbinom(100, 1, 0.5),
  11. n = rpois(100, 10)
  12. )
  13. data$y <- 0.5 + 2 * data$x + 0.1 * data$z + 0.5 * data$b - 0.1 * (data$x - 0.5) ^ 2 + rnorm(100)
  14. # 定义模型
  15. model1 <- lm_robust(y ~ x, data = data, clusters = data$b, se_type = "stata")
  16. model2 <- lm_robust(y ~ x + z, data = data, clusters = data$b, se_type = "stata")
  17. model3 <- lm_robust(y ~ x + b, data = data, clusters = data$z, se_type = "stata")
  18. model4 <- lm_robust(y ~ x + z + b, data = data, clusters = data$n, se_type = "stata")
  19. # 使用screenreg创建表格
  20. texreg(
  21. list(model1, model2, model3, model4), table = FALSE,
  22. custom.model.names = c("Model 1", "Model 2", "Model 3", "Model 4"),
  23. custom.coef.map = list("x" = "only coeff"),
  24. include.ci = FALSE, include.adjr = FALSE, include.rsquared = FALSE, inlcude.rmse = FALSE,
  25. include.nobs = TRUE,
  26. digits = 3
  27. )

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

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

我认为这与 lm_robust 函数有关(因为它在只有 lm 的情况下可以正常工作),但我确实需要 lm_robust,因为标准误差需要与 stata 中获得的相同。

此外,我收到以下警告:

  1. 警告消息:
  2. 1: data.frame(gof.names = colnames(out), gof = as.numeric(out),中引入了 NAs
  3. 2: doTryCatch(return(expr), name, parentenv, handler) 中:
  4. texreg broom 包提取了以下 GOF 度量,但无法将其转换为数字类型:se_type
  5. 3: data.frame(gof.names = colnames(out),gof = as.numeric(out),中引入了 NAs
  6. 4: doTryCatch(return(expr), name, parentenv, handler) 中:
  7. texreg broom 包提取了以下 GOF 度量,但无法将其转换为数字类型:Statistic
  8. 5: data.frame(gof.names = colnames(out), gof = as.numeric(out),中引入了 NAs
  9. 6: doTryCatch(return(expr), name, parentenv, handler) 中:
  10. texreg broom 包提取了以下 GOF 度量,但无法将其转换为数字类型:se_type
  11. 7: data.frame(gof.names = colnames(out), gof = as.numeric(out),中引入了 NAs
  12. 8: doTryCatch(return(expr), name, parentenv, handler) 中:
  13. texreg broom 包提取了以下 GOF 度量,但无法将其转换为数字类型:se_type

我希望的结果是:

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

如何实现这一点?谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. I am running 4 regressions, with different specifications, but every model has `x` as a regressor. I am using the standard errors stata type and clusters from the `estimatr` package
  4. Then I am using `texreg` for getting a nicely formatted regression table, here is my reproducible example:

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
)

  1. As you can see in the `include.x` part I am only including the `n.obs`. I just want that. But it seems the code ignores that part, because this is my result.

\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}

  1. I think it has to do with the `lm_robust` function (because it works with just `lm`), but I really need the `lm_robust` because of standard errors (I need them to be identical to the obtained in stata)
  2. Furthermore I get this warnings:

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

  1. My desired result would be:

\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. How can I accomplish this?
  2. Thanks in advance !
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. `stargazer`是我用来进行标准误差调整的首选包。以下是创建您想要的表格的示例代码,包括Stata风格的集群标准误差:
  7. ```R
  8. library(lmtest)
  9. library(sandwich)
  10. library(stargazer)
  11. set.seed(44)
  12. # 加载数据
  13. data <- data.frame(
  14. x = rnorm(100),
  15. z = rnorm(100),
  16. b = rbinom(100, 1, 0.5),
  17. n = rpois(100, 10)
  18. )
  19. data$y <- 0.5 + 2 * data$x + 0.1 * data$z + 0.5 * data$b - 0.1 * (data$x - 0.5) ^ 2 + rnorm(100)
  20. # 定义模型
  21. model1 <- lm(y ~ x, data = data)
  22. model2 <- lm(y ~ x + z, data = data)
  23. model3 <- lm(y ~ x + b, data = data)
  24. model4 <- lm(y ~ x + z + b, data = data)
  25. # 制作集群标准误差
  26. se1 = as.vector(coeftest(model1,vcov = vcovCL,cluster = ~b, type="HC1")[,"Std. Error"])
  27. se2 = as.vector(coeftest(model2,vcov = vcovCL,cluster = ~b, type="HC1")[,"Std. Error"])
  28. se3 = as.vector(coeftest(model3,vcov = vcovCL,cluster = ~z, type="HC1")[,"Std. Error"])
  29. se4 = as.vector(coeftest(model4,vcov = vcovCL,cluster = ~n, type="HC1")[,"Std. Error"])
  30. stargazer(model1,model2,model3,model4,type="latex",
  31. se=list(se1,se2,se3,se4), omit=c("Constant","z","b"),
  32. omit.stat = c("f","ser","adj.rsq","rsq"))

如果在stargazer命令中使用type="text",您将看到以下表格:

  1. ================================================
  2. Dependent variable:
  3. -----------------------------------
  4. y
  5. (1) (2) (3) (4)
  6. ------------------------------------------------
  7. x 2.071*** 2.079*** 2.123*** 2.135***
  8. (0.103) (0.124) (0.149) (0.111)
  9. ------------------------------------------------
  10. Observations 100 100 100 100
  11. ================================================
  12. Note: *p<0.1; **p<0.05; ***p<0.01
英文:

stargazer is my go-to package for such standard error adjustments. Here's example code making the table you want, including Stata-style clustered SEs.

  1. library(lmtest)
  2. library(sandwich)
  3. library(stargazer)
  4. set.seed(44)
  5. # Load data
  6. data &lt;- data.frame(
  7. x = rnorm(100),
  8. z = rnorm(100),
  9. b = rbinom(100, 1, 0.5),
  10. n = rpois(100, 10)
  11. )
  12. data$y &lt;- 0.5 + 2 * data$x + 0.1 * data$z + 0.5 * data$b - 0.1 * (data$x - 0.5) ^ 2 + rnorm(100)
  13. # Define models
  14. model1 &lt;- lm(y ~ x, data = data)
  15. model2 &lt;- lm(y ~ x + z, data = data)
  16. model3 &lt;- lm(y ~ x + b, data = data)
  17. model4 &lt;- lm(y ~ x + z + b, data = data)
  18. # Make clustered SEs
  19. se1 = as.vector(coeftest(model1,vcov = vcovCL,cluster = ~b, type=&quot;HC1&quot;)[,&quot;Std. Error&quot;])
  20. se2 = as.vector(coeftest(model2,vcov = vcovCL,cluster = ~b, type=&quot;HC1&quot;)[,&quot;Std. Error&quot;])
  21. se3 = as.vector(coeftest(model3,vcov = vcovCL,cluster = ~z, type=&quot;HC1&quot;)[,&quot;Std. Error&quot;])
  22. se4 = as.vector(coeftest(model4,vcov = vcovCL,cluster = ~n, type=&quot;HC1&quot;)[,&quot;Std. Error&quot;])
  23. stargazer(model1,model2,model3,model4,type=&quot;latex&quot;,
  24. se=list(se1,se2,se3,se4), omit=c(&quot;Constant&quot;,&quot;z&quot;,&quot;b&quot;),
  25. omit.stat = c(&quot;f&quot;,&quot;ser&quot;,&quot;adj.rsq&quot;,&quot;rsq&quot;))

If you do type=&quot;text&quot; in the stargazer command, you'll see this table:

  1. ================================================
  2. Dependent variable:
  3. -----------------------------------
  4. y
  5. (1) (2) (3) (4)
  6. ------------------------------------------------
  7. x 2.071*** 2.079*** 2.123*** 2.135***
  8. (0.103) (0.124) (0.149) (0.111)
  9. ------------------------------------------------
  10. Observations 100 100 100 100
  11. ================================================
  12. Note: *p&lt;0.1; **p&lt;0.05; ***p&lt;0.01

答案2

得分: 1

I figured that it was a typo in the texreg function (facepalm) this is the correct answer (I've changed to screenreg just to make the answer clearer)

这是正确的答案(我改成了screenreg只是为了让答案更清晰)

This is what R displays in the console.

这是R在控制台中显示的内容。

============================================================

Model 1 Model 2 Model 3 Model 4


only coeff 2.118 ** 2.132 ** 2.151 *** 2.164 ***

  1. (0.023) (0.006) (0.101) (0.148)

Num. obs. 100 100 100 100

N Clusters 2 2 100 15

============================================================

*** p < 0.001; ** p < 0.01; * p < 0.05

您可以通过使用include.nclusts = F来去掉N clusters参数。

英文:

I figured that it was a typo in the texreg function (facepalm) this is the correct answer (I've changed to screenreg just to make the answer clearer)

  1. screenreg(
  2. list(model1, model2, model3, model4), table = FALSE,
  3. custom.model.names = c(&quot;Model 1&quot;, &quot;Model 2&quot;, &quot;Model 3&quot;, &quot;Model 4&quot;),
  4. custom.coef.map = list(&quot;x&quot; = &quot;only coeff&quot;),
  5. include.ci = FALSE, include.adjr = FALSE, include.rsquared = FALSE, include.rmse = FALSE,
  6. include.nobs = TRUE,
  7. digits = 3
  8. )

This is what R displays in the console.

  1. ============================================================
  2. Model 1 Model 2 Model 3 Model 4
  3. ------------------------------------------------------------
  4. only coeff 2.118 ** 2.132 ** 2.151 *** 2.164 ***
  5. (0.023) (0.006) (0.101) (0.148)
  6. ------------------------------------------------------------
  7. Num. obs. 100 100 100 100
  8. N Clusters 2 2 100 15
  9. ============================================================
  10. *** p &lt; 0.001; ** p &lt; 0.01; * p &lt; 0.05

You could get rid of the N clusters argument by putting include.nclusts = F

huangapple
  • 本文由 发表于 2023年4月20日 02:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定