首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我将木星笔记本转换成PDF时,我能很好地打印linearmodels.panel.results.compare()的输出吗?

当我将木星笔记本转换成PDF时,我能很好地打印linearmodels.panel.results.compare()的输出吗?
EN

Stack Overflow用户
提问于 2022-01-04 23:25:28
回答 2查看 573关注 0票数 1

我使用Python来分析木星笔记本中的数据,我将这些数据转换为PDF与共同作者(jupyter nbconvert --to pdf)共享。我经常使用linearmodels.panel.results.compare()来比较来自linearmodels包的面板回归估计值。但是,PDF转换过程将compare()输出转换为对PDF太宽的固定宽度字体(我将提供下面的代码):

当我将木星笔记本转换成PDF时,我能很好地打印compare()的输出吗?

一种可能的解决方案是将compare()输出转换为数据帧。选项pd.options.display.latex.repr = True漂亮打印数据帧时,我转换为PDF。例如:

在笔记本中,compare()输出格式很好,看起来像一个数据框架。但是,它不是数据框架,我没有将它转换为数据框架。

是否有一种替代的解决方案来比较linearmodels包输出的漂亮打印结果?

下面是生成上述表的代码(复制并粘贴到木星笔记本代码单元格中):

代码语言:javascript
复制
import pandas as pd
from linearmodels.panel import FamaMacBeth
from linearmodels.panel.results import compare

pd.options.display.latex.repr = True

from statsmodels.datasets import grunfeld
df = grunfeld.load_pandas().data
df.set_index(['firm','year'], inplace=True)

display(df.head())

table = {
    '(1)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit(),
    '(2)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit(),
    '(3)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit(),
    '(4)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit()
}

display(compare(table))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-05 10:17:37

compare返回一个PanelModelComparison。这个类有一个属性summary,它返回一个与状态模型中可用的Summary对象几乎相同的linearmodels.compat.statsmodels.SummarySummary实例有一个方法as_latex(),它将表转换为LaTeX。

代码语言:javascript
复制
import pandas as pd
from linearmodels.panel import FamaMacBeth
from linearmodels.panel.results import compare

pd.options.display.latex.repr = True

from statsmodels.datasets import grunfeld
df = grunfeld.load_pandas().data
df.set_index(['firm','year'], inplace=True)

display(df.head())

table = {
    '(1)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit(),
    '(2)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit(),
    '(3)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit(),
    '(4)': FamaMacBeth.from_formula(formula='value ~ 1 + invest + capital', data=df).fit()
}

display(compare(table))
comparrison = compare(table)
summary = comparrison.summary
print(summary.as_latex())

这个指纹

代码语言:javascript
复制
\begin{center}
\begin{tabular}{lcccc}
\toprule
                               &         \textbf{(1)}        &         \textbf{(2)}        &         \textbf{(3)}        &         \textbf{(4)}         \\
\midrule
\textbf{Dep. Variable}         &            value            &            value            &            value            &            value             \\
\textbf{Estimator}             &         FamaMacBeth         &         FamaMacBeth         &         FamaMacBeth         &         FamaMacBeth          \\
\textbf{No. Observations}      &             220             &             220             &             220             &             220              \\
\textbf{Cov. Est.}             &  Fama-MacBeth Standard Cov  &  Fama-MacBeth Standard Cov  &  Fama-MacBeth Standard Cov  &  Fama-MacBeth Standard Cov   \\
\textbf{R-squared}             &            0.6964           &            0.6964           &            0.6964           &            0.6964            \\
\textbf{R-Squared (Within)}    &           -1.8012           &           -1.8012           &           -1.8012           &           -1.8012            \\
\textbf{R-Squared (Between)}   &            0.8660           &            0.8660           &            0.8660           &            0.8660            \\
\textbf{R-Squared (Overall)}   &            0.6964           &            0.6964           &            0.6964           &            0.6964            \\
\textbf{F-statistic}           &            248.83           &            248.83           &            248.83           &            248.83            \\
\textbf{P-value (F-stat)}      &            0.0000           &            0.0000           &            0.0000           &            0.0000            \\
\textbf{=====================} & =========================== & =========================== & =========================== & ===========================  \\
\textbf{Intercept}             &            114.16           &            114.16           &            114.16           &            114.16            \\
\textbf{ }                     &           (3.8390)          &           (3.8390)          &           (3.8390)          &           (3.8390)           \\
\textbf{capital}               &            0.1457           &            0.1457           &            0.1457           &            0.1457            \\
\textbf{ }                     &           (0.8510)          &           (0.8510)          &           (0.8510)          &           (0.8510)           \\
\textbf{invest}                &            6.3899           &            6.3899           &            6.3899           &            6.3899            \\
\textbf{ }                     &           (11.618)          &           (11.618)          &           (11.618)          &           (11.618)           \\
\bottomrule
\end{tabular}
%\caption{Model Comparison}
\end{center}

T-stats reported in parentheses
票数 3
EN

Stack Overflow用户

发布于 2022-01-05 16:11:24

下面是使用Kevin .的.summary.as_latex()的另一种方法,下面的函数使用compare().summary创建数据框架,jupyter nbconvert --to pdf将其转换为表。

代码语言:javascript
复制
from io import StringIO
import warnings

def compare_df(x, fit_stats=['Estimator', 'R-squared', 'No. Observations']):
    with warnings.catch_warnings():
        warnings.simplefilter(action='ignore', category=FutureWarning)
        y = pd.read_csv(StringIO(compare(x, stars=True).summary.as_csv()), skiprows=1, skipfooter=1, engine='python')
    z = pd.DataFrame(
        data=y.iloc[:, 1:].values,
        index=y.iloc[:, 0].str.strip(),
        columns=pd.MultiIndex.from_arrays(
            arrays=[y.columns[1:], y.iloc[0][1:]],
            names=['Model', 'Dep. Var.']
        )
    )
    return pd.concat([z.iloc[11:], z.loc[fit_stats]])

PDF输出如下:

注意,此解决方案需要pd.options.display.latex.repr = True

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70586193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档