You need to enable JavaScript to run this app. Stata esttab导出LaTeX回归表按模型分组列及编译报错解决
优惠活动
大模型
产品
解决方案
定价
更多

Stata esttab导出LaTeX回归表按模型分组列及编译报错解决

问题根源定位
  • 编译报错的直接触发点是代码行内三个\cmidrule(lr){@spam}无效占位符:@spam不是合法的列范围参数,是estout(esttab的底层程序)自动生成分组规则时替换参数失败留下的无效标记,LaTeX在扫描\cmidrule的参数时找不到合法列区间,最终报“文件结束扫描命令”的错误。
  • 本质是esttab导出配置不完整,没有正确告知程序每个分组跨哪几列,导致自动生成的分组表头、分割线参数失效。
第一步:直接修复现有LaTeX代码实现分组效果

仅需把原来错误的cmidrule行替换为对应正确列范围的代码,即可快速完成分组排版:

  1. 你的表格总共包含7列:1列变量名文本列 + 6列回归结果数值列,三个分组的跨列范围分别为:
  • OLS组:覆盖第2、3列(对应Reg1、Reg2)
  • First Stage组:覆盖第4、5列(对应Reg3、Reg4)
  • Second Stage组:覆盖第6、7列(对应Reg5、Reg6)
  1. 替换后的表头行核心代码为:
& \multicolumn{2}{c}{OLS} & \multicolumn{2}{c}{First Stage} & \multicolumn{2}{c}{Second Stage} \\
\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}
  1. 替换后可直接编译的完整表格代码如下:
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{IV Regression Results, 1990\label{tab2}}
\begin{tabular}{l*{6}{D{.}{.}{-1}}}
\toprule
                    &\multicolumn{2}{c}{OLS}                    &\multicolumn{2}{c}{First Stage}            &\multicolumn{2}{c}{Second Stage}           \\
\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}
                    &\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}\\
\midrule
Housing Supply Elasticity&     -0.0179         &     -0.0149\sym{**} &      0.0320         &     0.00967         &      0.0320         &     0.00967         \\
                    &    (0.0116)         &   (0.00642)         &    (0.0373)         &    (0.0244)         &    (0.0373)         &    (0.0244)         \\
\addlinespace
Population          &    5.69e-08\sym{***}&    2.83e-08\sym{***}&    5.58e-08\sym{***}&    2.78e-08\sym{***}&    5.58e-08\sym{***}&    2.78e-08\sym{***}\\
                    &  (1.26e-08)         &  (6.56e-09)         &  (1.05e-08)         &  (6.84e-09)         &  (1.05e-08)         &  (6.84e-09)         \\
\addlinespace
WRLURI              &     -0.0757\sym{***}&     -0.0503\sym{***}&      0.0742         &      0.0235         &      0.0742         &      0.0235         \\
                    &    (0.0203)         &    (0.0126)         &     (0.109)         &    (0.0710)         &     (0.109)         &    (0.0710)         \\
\addlinespace
Constant            &       0.269\sym{***}&       0.580\sym{***}&       0.167\sym{**} &       0.530\sym{***}&       0.167\sym{**} &       0.530\sym{***}\\
                    &    (0.0346)         &    (0.0198)         &    (0.0801)         &    (0.0524)         &    (0.0801)         &    (0.0524)         \\
\midrule
Observations        &         228         &         228         &         228         &         228         &         228         &         228         \\
\(R^{2}\)           &       0.188         &       0.130         &           .         &       0.017         &           .         &       0.017         \\
Adjusted \(R^{2}\)  &       0.177         &       0.119         &           .         &       0.004         &           .         &       0.004         \\
\bottomrule
\multicolumn{7}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{7}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\
\end{tabular}
\end{table}
第二步:配置Stata端esttab参数,实现自动导出正确的分组表格

不需要每次手动修改LaTeX代码,只要调整esttab的配置,程序会自动生成带正确分组的LaTeX代码,不会再出现@spam占位符:

  • 核心参数是mgroups,用来定义顶层分组标签,搭配pattern指定每个分组对应的列范围,搭配erepeat自动生成分组下方的分割线。
  • 对应“3个分组、每组2个回归”的需求,正确的esttab导出代码示例:
* 先依次运行6个回归,用est store存好结果
reg isolation90 housing_elasticity pop wrluri
est store m1
reg dissim90 housing_elasticity pop wrluri
est store m2
ivreghdfe isolation90 (housing_elasticity = supply_iv) pop wrluri
est store m3
ivreghdfe dissim90 (housing_elasticity = supply_iv) pop wrluri
est store m4
ivreghdfe outcome (housing_elasticity = supply_iv) pop wrluri if outcome_isol
est store m5
ivreghdfe outcome (housing_elasticity = supply_iv) pop wrluri if outcome_dissim
est store m6

* 导出LaTeX表格
esttab m1 m2 m3 m4 m5 m6 using "iv_results.tex", replace ///
    booktabs ///
    mtitles("Isolation, 1990" "Dissimilarity, 1990" "Isolation, 1990" "Dissimilarity, 1990" "Isolation, 1990" "Dissimilarity, 1990") ///
    mgroups("OLS" "First Stage" "Second Stage", ///
        pattern(1 0 1 0 1 0)  ///
        prefix(\multicolumn{@span}{c}{) suffix(}) span ///
        erepeat(\cmidrule(lr){@span})  ///
    ) ///
    keep(housing_elasticity pop wrluri _cons) ///
    se star(* 0.1 ** 0.05 *** 0.01) ///
    label nonotes ///
    addnotes("Standard errors in parentheses" "* p<0.10, ** p<0.05, *** p<0.01")
  • 容易踩的配置坑:
    • 必须加span参数,否则estout不会自动替换@span占位符,最终导出的代码就会出现无效@spam标记。
    • pattern参数必须和分组列数严格匹配:每组2个回归时,pattern序列就是1 0 1 0 1 0,不能错位,否则生成的列范围会偏移。
    • 如果esttab还是导出带@spam的代码,先更新estout包:运行ssc install estout, replace即可解决旧版本参数不兼容的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.27 00:15:03