You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用fixest的etable垂直堆叠不同样本的同因变量feols回归结果?

使用fixest::etable实现多样本多因变量回归结果的垂直堆叠LaTeX表格

核心实现代码

先完成回归模型拟合,再通过etable的参数配置实现Panel划分和去重因变量名:

library(fixest)
library(dplyr)

# 拟合两个样本的多因变量回归
est1 <- feols(c(y1,y2,y3,y4) ~ x1, data %>% filter(sample=="A"))
est2 <- feols(c(y1,y2,y3,y4) ~ x1, data %>% filter(sample=="B"))

# 生成带Panel的LaTeX表格
etable(est1, est2,
       # 按模型组拆分出Panel A和Panel B
       split = list("Panel A: Sample A" = 1:4, "Panel B: Sample B" = 5:8),
       # 隐藏重复的因变量名称行,仅保留第一个
       drop = ~ y2 + y3 + y4,
       # 自定义因变量列标题,替代默认的y1
       dict = c(y1 = "Dependent Variables"),
       # 输出LaTeX格式
       tex = TRUE,
       # 指定要显示的拟合统计量(按需调整)
       fitstat = c("r2", "n"),
       # 关闭默认的模型编号列
       model.names = FALSE,
       # 可选:调整LaTeX表格的对齐方式
       align = "lcc")

IV回归场景适配

如果是IV回归(用户提到modelsummary无法导出完整统计量的场景),etable可以直接处理fixest_multi对象,只需添加IV专属统计量即可:

# IV回归示例
est1_iv <- feols(c(y1,y2,y3,y4) ~ x1 | x2 ~ z1, data %>% filter(sample=="A"))
est2_iv <- feols(c(y1,y2,y3,y4) ~ x1 | x2 ~ z1, data %>% filter(sample=="B"))

etable(est1_iv, est2_iv,
       split = list("Panel A: Sample A" = 1:4, "Panel B: Sample B" = 5:8),
       drop = ~ y2 + y3 + y4,
       dict = c(y1 = "Dependent Variables"),
       tex = TRUE,
       # 添加第一阶段F值等IV统计量
       fitstat = c("r2", "n", "ivfstat"),
       model.names = FALSE)

关键参数说明

  • split:将两个模型的8个回归结果(各4个因变量)划分为两组,对应Panel A和B的内容
  • drop:移除重复的因变量名称行,避免y2/y3/y4重复显示
  • dict:将第一个因变量的名称替换为更通用的表头,匹配所有因变量结果行
  • fitstat:自定义显示的统计量,支持fixest内置的所有拟合指标(如adj.r2、f等)

进阶LaTeX格式调整

如需添加标题、标签或设置表格宽度,可通过tex.args传递参数:

etable(est1, est2,
       split = list("Panel A: Sample A" = 1:4, "Panel B: Sample B" = 5:8),
       drop = ~ y2 + y3 + y4,
       dict = c(y1 = "Dependent Variables"),
       tex = TRUE,
       fitstat = c("r2", "n"),
       model.names = FALSE,
       tex.args = list(caption = "Regression Results by Sample Group",
                       label = "tab:sample_regressions",
                       width = "\\textwidth"))

内容的提问来源于stack exchange,提问作者Dawit G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:01:11