Skip to main content

Convert statsmodels results to publication-ready LaTeX tables

Project description

freetable

Coverage

Convert statsmodels regression results to publication-ready LaTeX tables. This package is highly opinionated and seeks to do exactly one thing well.

Moreover, unlike many similar packages, this one is completely free to use and imposes no requirements on users (read more here about problems with other packages).

Example regression table output

Installation

pip install -U freetable

Features

  • Clean, opinionated output using modern LaTeX packages
  • Automatic significance stars (default: *** p<0.001; ** p<0.01; * p<0.05)
  • Customizable significance thresholds
  • Variable renaming for clean presentation
  • Standard errors in parentheses below coefficients
  • R², adjusted R², and number of observations
  • Extra custom rows (e.g., outcome labels, SE types, subsets)
  • Grouped column headers with multicolumn support
  • Customizable table placement
  • Optional automatic resizing for wide tables
  • Professional formatting with booktabs, siunitx, and threeparttable

Requirements

Python:

  • statsmodels

LaTeX packages:

  • threeparttable
  • siunitx
  • booktabs
  • graphicx (only required if using resize=True)

Add these to your LaTeX document preamble:

\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{threeparttable}
\usepackage{graphicx}  % Only needed for resize=True

Or just use paper!

Usage

Basic example

import pandas as pd
import statsmodels.formula.api as smf
from freetable import table

# Fit your models
df = pd.read_csv("data.csv")
m1 = smf.ols("y ~ x1", data=df).fit()
m2 = smf.ols("y ~ x1 + x2", data=df).fit()

# Generate LaTeX table
latex = table(
    [m1, m2],
    model_names=["Model 1", "Model 2"],
    caption="Regression Results",
    label="tab:regression",
    digits=3,
    rename={"x1": "Gender", "x2": "Age"},
    stars=(0.1, 0.05, 0.01),  # Order doesn't matter - automatically sorted
)

print(latex)

Advanced example with all features

# Fit three models
m1 = smf.ols("outcome ~ treatment", data=df).fit(cov_type="HC3")
m2 = smf.ols("outcome ~ treatment + control1", data=df).fit(cov_type="HC3")
m3 = smf.ols("outcome ~ treatment + control1 + control2", data=df).fit(
    cov_type="cluster",
    cov_kwds={"groups": df["cluster_id"]},
)

# Create comprehensive table
latex = table(
    [m1, m2, m3],
    model_names=["Model 1", "Model 2", "Model 3"],
    caption="Treatment Effects on Outcomes",
    label="tab:treatment",
    digits=3,
    rename={"treatment": "Treatment", "control1": "Control 1", "control2": "Control 2"},
    extra_rows={
        "Outcome": ["Y", "Y", "Y"],
        "Subset": ["All", "All", "All"],
        "SE type": ["HC3", "HC3", "Clustered"],
    },
    custom_header=[("Main Effects", 2), ("Full Model", 1)],
    placement="h!",
    resize=True,
)

print(latex)

Development

This package uses the following tools for code quality and consistency:

  • black: Code formatting
  • ruff: Fast Python linting
  • isort: Import sorting
  • mypy: Static type checking
  • pytest: Testing framework

Running tests

Install development dependencies:

pip install -Ue ".[dev]"

Run tests:

pytest

Run type checking:

mypy src/freetable/

License

Everything in this repository is licensed under CC0 1.0 Universal, or (at your option) any later version. See LICENSE for details. Note that there is absolutely no warranty.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

freetable-1.1.0.tar.gz (143.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

freetable-1.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file freetable-1.1.0.tar.gz.

File metadata

  • Download URL: freetable-1.1.0.tar.gz
  • Upload date:
  • Size: 143.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for freetable-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5437f62d811cc907f6366914ea84daf72e7b96d5abfb460970709f5cf2d4192c
MD5 ad5f8668aac2031b435430610dde17fc
BLAKE2b-256 5f0950c39d93b39faba529dcad35c7b18b73e9eeb263fd6c3d85bedb609a577f

See more details on using hashes here.

File details

Details for the file freetable-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: freetable-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for freetable-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd73fd1924764135cac1a208a05b437d6eee4964e86bed6a552e4d660e1223af
MD5 44ac87a65f408a21e1758c95a4e63949
BLAKE2b-256 fcc73bf71bca85f4df613ef96076947e9880b579478d558334282d3ea09a3c39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page