【问题标题】:Statsmodel summary_col latex format errorStatsmodel summary_col 乳胶格式错误
【发布时间】:2025-12-16 10:55:01
【问题描述】:

我正在使用 statsmodel 的 summary_col 给我一个输出表,它总结了两个回归输出。

代码是

res3 = summary_col([res1,res2],stars=True,float_format='%0.2f',
              info_dict={'R2':lambda x: "{:.2f}".format(x.rsquared)})
f = open('res3.tex', 'w')
f.write(res3.as_latex())
f.close()

我使用 res3.tex 文件作为另一个 tex 文件的输入,然后生成结果。当我使用 as_latex() 将表格转换为 LaTeX 格式时出现问题。 The table header shifts to the side in the tex file and looks like this.

res3.tex 文件有以下乳胶代码

\begin{table}
\caption{}
\begin{center}
    \begin{tabular}{lcc}
        \hline
        & investment I & investment II  \\
        \hline
        \hline
    \end{tabular}
    \begin{tabular}{lll}
        GDP      & 1.35***      & 1.19***        \\
        & (0.24)       & (0.23)         \\
        bsent    & 0.28***      & 0.26***        \\
        & (0.06)       & (0.06)         \\
        rate     & -0.22*       & -0.65***       \\
        & (0.13)       & (0.19)         \\
        research &              & 0.80***        \\
        &              & (0.27)         \\
        R2       & 0.76         & 0.80           \\
        \hline
    \end{tabular}
\end{center}\end{table}

问题似乎是由于多个表格环境引起的。有没有办法在不手动更改 res3 文件(中间文件)的情况下将投资标头放在表的顶部?

【问题讨论】:

  • 如果有人知道所需 Latex 的修复程序,那么 statsmodels 的问题或拉取请求会有所帮助。

标签: python latex


【解决方案1】:

您首先必须在 Latex 的序言中添加一些内容。 在这里您可能可以找到问题的答案Outputting Regressions as Table in Python (similar to outreg in stata)?

res3 = summary_col([res1,res2],stars=True,float_format='%0.2f',
              info_dict={'R2':lambda x: "{:.2f}".format(x.rsquared)})

beginningtex = """\\documentclass{report}
\\usepackage{booktabs}
\\begin{document}"""
endtex = "\end{document}"

f = open('myreg.tex', 'w')
f.write(beginningtex)
f.write(res3.as_latex())
f.write(endtex)
f.close()

感谢@BKay

【讨论】: