观星者在观察下降时分离变量
Stargazer pulls apart variables when observations dropped
我使用 stargazer 为多个模型创建 table。它们实际上是相同的模型,但第一个基于所有观察,而另一个分别丢弃不同的观察。所有变量的名称都相同,所以令我感到惊讶的是,当我将 table 导出到 Latex 时,有两行重复,一行用于虚拟变量,另一行用于交互项。
真正奇怪的是我无法复制结果,但我会 post 一个最小的工作示例。也许你可以根据我的描述帮助我。
这是我的 MWE 的代码:
library(tibble)
library(stargazer)
df <- as_tibble(data.frame(first = rnorm(100, 50), second = rnorm(100, 30), third = rnorm(100, 100), fourth = c(rep(0, 50), rep(1, 50))))
model.1 <- lm(first ~ second + third + fourth + third*fourth, data = df)
model.2 <- lm(first ~ second + third + fourth + third*fourth, data = df[!rownames(df) %in% "99",])
stargazer(model.1, model.2)
我现在 post Latex 输出包括我试图修复的错误(使用这个代码片段它似乎工作得很好)。
我想要的,当然是这个片段生成的代码(我觉得很愚蠢,无法重现):
您可以使用 coefficients()
查看模型系数的名称。确保它们是相同的,即 identical(names(model.1), names(model.2))
然后使用 stargazer
的 keep
语句来确保你得到你想要的系数,
此处与上面的示例保持选定的变量;
coefficients(model.1)
#> (Intercept) second third fourth third:fourth
#> 57.27352606 0.02674072 -0.08236250 20.23596216 -0.20288137
coefficients(model.2)
#> (Intercept) second third fourth third:fourth
#> 57.06149556 0.03305134 -0.08214812 20.85087288 -0.20885718
identical(names(model.1), names(model.2))
#> [1] TRUE
我正在使用 type = "text"
使它对 SO 更友好,但我想它与 LaTeX 一样,
stargazer(model.1, model.2, type = "text", keep=c("third","third:fourth"))
#>
#> =========================================================
#> Dependent variable:
#> -------------------------------------
#> first
#> (1) (2)
#> ---------------------------------------------------------
#> third -0.082 -0.082
#> (0.166) (0.167)
#>
#> third:fourth -0.203 -0.209
#> (0.222) (0.223)
#>
#> ---------------------------------------------------------
#> Observations 100 99
#> R2 0.043 0.044
#> Adjusted R2 0.002 0.004
#> Residual Std. Error 1.044 (df = 95) 1.047 (df = 94)
#> F Statistic 1.056 (df = 4; 95) 1.089 (df = 4; 94)
#> =========================================================
#> Note: *p<0.1; **p<0.05; ***p<0.01
但如果我们找不到重现您的问题的方法,可能很难排除这是本地问题。
我使用 stargazer 为多个模型创建 table。它们实际上是相同的模型,但第一个基于所有观察,而另一个分别丢弃不同的观察。所有变量的名称都相同,所以令我感到惊讶的是,当我将 table 导出到 Latex 时,有两行重复,一行用于虚拟变量,另一行用于交互项。
真正奇怪的是我无法复制结果,但我会 post 一个最小的工作示例。也许你可以根据我的描述帮助我。
这是我的 MWE 的代码:
library(tibble)
library(stargazer)
df <- as_tibble(data.frame(first = rnorm(100, 50), second = rnorm(100, 30), third = rnorm(100, 100), fourth = c(rep(0, 50), rep(1, 50))))
model.1 <- lm(first ~ second + third + fourth + third*fourth, data = df)
model.2 <- lm(first ~ second + third + fourth + third*fourth, data = df[!rownames(df) %in% "99",])
stargazer(model.1, model.2)
我现在 post Latex 输出包括我试图修复的错误(使用这个代码片段它似乎工作得很好)。
我想要的,当然是这个片段生成的代码(我觉得很愚蠢,无法重现):
您可以使用 coefficients()
查看模型系数的名称。确保它们是相同的,即 identical(names(model.1), names(model.2))
然后使用 stargazer
的 keep
语句来确保你得到你想要的系数,
此处与上面的示例保持选定的变量;
coefficients(model.1)
#> (Intercept) second third fourth third:fourth
#> 57.27352606 0.02674072 -0.08236250 20.23596216 -0.20288137
coefficients(model.2)
#> (Intercept) second third fourth third:fourth
#> 57.06149556 0.03305134 -0.08214812 20.85087288 -0.20885718
identical(names(model.1), names(model.2))
#> [1] TRUE
我正在使用 type = "text"
使它对 SO 更友好,但我想它与 LaTeX 一样,
stargazer(model.1, model.2, type = "text", keep=c("third","third:fourth"))
#>
#> =========================================================
#> Dependent variable:
#> -------------------------------------
#> first
#> (1) (2)
#> ---------------------------------------------------------
#> third -0.082 -0.082
#> (0.166) (0.167)
#>
#> third:fourth -0.203 -0.209
#> (0.222) (0.223)
#>
#> ---------------------------------------------------------
#> Observations 100 99
#> R2 0.043 0.044
#> Adjusted R2 0.002 0.004
#> Residual Std. Error 1.044 (df = 95) 1.047 (df = 94)
#> F Statistic 1.056 (df = 4; 95) 1.089 (df = 4; 94)
#> =========================================================
#> Note: *p<0.1; **p<0.05; ***p<0.01
但如果我们找不到重现您的问题的方法,可能很难排除这是本地问题。