我正在用大量时间和位置固定效果在R中进行回归。我尝试将一个不错的汇总表输出到Latex中。我从stargazer
包切换为huxtable
,因为在省略固定效果时,stargazer
的表现不一致(请参见此question)。
这是一个简单的示例:
library(huxtable)
reg1 <- lm(mpg ~ disp, data = mtcars)
reg2 <- lm(mpg ~ disp + factor(gear) + factor(carb), data = mtcars)
huxreg(reg1, reg2)
huxreg
的输出是:
> huxreg(reg1, reg2)
────────────────────────────────────────────────────
(1) (2)
───────────────────────────────────
(Intercept) 29.600 *** 25.533 ***
(1.230) (2.996)
disp -0.041 *** -0.018
(0.005) (0.011)
factor(gear)4 3.988
(2.495)
factor(gear)5 5.391 *
(2.591)
factor(carb)2 -1.979
(1.667)
factor(carb)3 -4.161
(2.131)
factor(carb)4 -6.199 *
(2.221)
factor(carb)6 -8.557 *
(3.653)
factor(carb)8 -10.389 *
(4.268)
───────────────────────────────────
N 32 32
R2 0.718 0.828
logLik -82.105 -74.186
AIC 170.209 168.372
────────────────────────────────────────────────────
*** p < 0.001; ** p < 0.01; * p < 0.05.
Column names: names, model1, model2
这是所需的输出:
────────────────────────────────────────────────────
(1) (2)
───────────────────────────────────
(Intercept) 29.600 *** 25.533 ***
(1.230) (2.996)
disp -0.041 *** -0.018
(0.005) (0.011)
───────────────────────────────────
Gear FE No Yes
Carb FE No Yes
───────────────────────────────────
N 32 32
R2 0.718 0.828
logLik -82.105 -74.186
AIC 170.209 168.372
────────────────────────────────────────────────────
*** p < 0.001; ** p < 0.01; * p < 0.05.
Column names: names, model1, model2
我知道我可以简单地使用add_rows()
编辑huxtable,但是我正在寻找一种更强大的解决方案,该解决方案允许使用正则表达式(例如stargazer的omit.labels
选项)查找行名。
我以this为灵感,亲自写下了答案。
函数check_factors()
确定模型中是否存在特定变量,然后使用sapply()
创建在表中添加的行。但是,这不是完全自动的,因为我仍然必须检查omit_coef
列出的所有变量以后是否都由check_factors()
测试。可以省略变量,然后忘记添加相应的行。
library(huxtable)
reg1 <- lm(mpg ~ disp, data = mtcars)
reg2 <- lm(mpg ~ disp + factor(gear) + factor(carb), data = mtcars)
huxreg(reg1, reg2)
gear_factors <- tidy(reg2) %>%
filter(str_detect(term, "factor\\(gear\\)")) %>% ## in R, you have to escape the escape, hence \\
pull(term)
carb_factors <- tidy(reg2) %>%
filter(str_detect(term, "factor\\(carb\\)")) %>%
pull(term)
check_factors <- function(model, factors) {
return(all(factors %in% (tidy(model) %>% pull(term))))
}
models_report <- list(reg1 , reg2)
huxreg(models_report,
omit_coefs = c(gear_factors, carb_factors)) %>%
# add the rows with with True/false returned by check_factors() replased with "Yes"/"No"
add_rows(rbind(c("Gear FE",
ifelse(sapply(models_report,
check_factors,
factors=gear_factors),
"Yes", "No")),
c("Carb FE",
ifelse(sapply(models_report,
check_factors,
factors=carb_factors),
"Yes", "No"))),
copy_cell_props = FALSE, # this will prevent horizontal lines from appearing
after = nrow(.) - 5)
这将产生下表:
────────────────────────────────────────────────────
(1) (2)
───────────────────────────────────
(Intercept) 29.600 *** 25.533 ***
(1.230) (2.996)
disp -0.041 *** -0.018
(0.005) (0.011)
───────────────────────────────────
Gear FE No Yes
Carb FE No Yes
N 32 32
R2 0.718 0.828
logLik -82.105 -74.186
AIC 170.209 168.372
────────────────────────────────────────────────────
*** p < 0.001; ** p < 0.01; * p < 0.05.
Column names: names, model1, model2