pythonstatisticslatexstatsmodelslinearmodels

Output linearmodels regression summary as latex


How can I print out the summary table of a fitted linearmodels object as latex?

For example, how can I print res as latex code?

# Libraries
import pandas as pd
from linearmodels.panel import PanelOLS
from linearmodels.datasets import wage_panel

# Load silly data
df = wage_panel.load()

# Set indexes
df = df.set_index(['nr','year'])

# Fit silly model
mod = PanelOLS(dependent=df['lwage'], exog=df[['hours','married','educ']], time_effects=True)
res = mod.fit()

# Print results
print(res)
                          PanelOLS Estimation Summary                           
================================================================================
Dep. Variable:                  lwage   R-squared:                        0.0933
Estimator:                   PanelOLS   R-squared (Between):              0.7056
No. Observations:                4360   R-squared (Within):               0.0373
Date:                Thu, Nov 04 2021   R-squared (Overall):              0.6764
Time:                        23:48:11   Log-likelihood                   -3055.6
Cov. Estimator:            Unadjusted                                           
                                        F-statistic:                      149.14
Entities:                         545   P-value                           0.0000
Avg Obs:                       8.0000   Distribution:                  F(3,4349)
Min Obs:                       8.0000                                           
Max Obs:                       8.0000   F-statistic (robust):             149.14
                                        P-value                           0.0000
Time periods:                       8   Distribution:                  F(3,4349)
Avg Obs:                       545.00                                           
Min Obs:                       545.00                                           
Max Obs:                       545.00                                           
                                                                                
                             Parameter Estimates                              
==============================================================================
            Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI
------------------------------------------------------------------------------
hours      -7.571e-05  1.357e-05    -5.5805     0.0000     -0.0001  -4.911e-05
married        0.1573     0.0157     10.017     0.0000      0.1265      0.1881
educ           0.0765     0.0042     18.048     0.0000      0.0682      0.0848
==============================================================================

F-test for Poolability: 41.795
P-value: 0.0000
Distribution: F(7,4349)

Included effects: Time

Solution

  • You can do this with the summary attribute. Just note that you'll have to use the booktabs package in Latex.

    print(res.summary.as_latex())
    

    Which prints:

    \begin{center} \begin{tabular}{lclc} \toprule \textbf{Dep. Variable:} & lwage & \textbf{ R-squared: } & 0.0933 \ \textbf{Estimator:} & PanelOLS & \textbf{ R-squared (Between):} & 0.7056 \ \textbf{No. Observations:} & 4360 & \textbf{ R-squared (Within):}
    & 0.0373 \ \textbf{Date:} & Thu, Nov 04 2021 & \textbf{ R-squared (Overall):} & 0.6764 \ \textbf{Time:} & 23:50:24 & \textbf{ Log-likelihood } & -3055.6 \ \textbf{Cov. Estimator:} & Unadjusted & \textbf{ } &
    \ \textbf{} & & \textbf{ F-statistic: } & 149.14 \ \textbf{Entities:}
    & 545 & \textbf{ P-value } & 0.0000 \ \textbf{Avg Obs:} & 8.0000 & \textbf{ Distribution: } & F(3,4349) \ \textbf{Min Obs:}
    & 8.0000 & \textbf{ } &
    \ \textbf{Max Obs:} & 8.0000 & \textbf{ F-statistic (robust):} & 149.14 \ \textbf{}
    & & \textbf{ P-value } & 0.0000 \ \textbf{Time periods:} & 8 & \textbf{ Distribution: } & F(3,4349) \ \textbf{Avg Obs:}
    & 545.00 & \textbf{ } &
    \ \textbf{Min Obs:} & 545.00 & \textbf{
    } & \ \textbf{Max Obs:} & 545.00
    & \textbf{ } & \ \textbf{}
    & & \textbf{ } &
    \ \bottomrule \end{tabular} \begin{tabular}{lcccccc} & \textbf{Parameter} & \textbf{Std. Err.} & \textbf{T-stat} & \textbf{P-value} & \textbf{Lower CI} & \textbf{Upper CI} \ \midrule \textbf{hours} & -7.571e-05 & 1.357e-05 & -5.5805 & 0.0000 & -0.0001 &
    -4.911e-05 \ \textbf{married} & 0.1573 & 0.0157 & 10.017 & 0.0000 & 0.1265 &
    0.1881 \ \textbf{educ} & 0.0765 & 0.0042 & 18.048 & 0.0000 & 0.0682 &
    0.0848 \ \bottomrule \end{tabular} %\caption{PanelOLS Estimation Summary} \end{center}

    F-test for Poolability: 41.795 \newline P-value: 0.0000 \newline Distribution: F(7,4349) \newline \newline Included effects: Time