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
  • tabularx
  • graphicx (only required if using resize=True)

Add these to your LaTeX document preamble:

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

Or just use paper!

Usage

See examples/ for the output generated by the following code.

Basic example

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

df = pd.read_csv("output.csv")

# Fit your models
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

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

df = pd.read_csv("data.csv")

# 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=False,
)

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-2.0.0.tar.gz (123.8 kB view details)

Uploaded Source

Built Distribution

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

freetable-2.0.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for freetable-2.0.0.tar.gz
Algorithm Hash digest
SHA256 7586f9d369404e2ae1dd0e13c124d24adcdae615f395b4b3e00533984da6305b
MD5 c478ef6c8a7258bf413a9f52d8f79287
BLAKE2b-256 25d689c5576811f9c507b3d88d2137fe097f2d5244ca3d358e3ef8214b301abb

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for freetable-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3cbe57f74023f3fe3d27a7a7e5d29c6a430948f82c98a9e61c96fe11e94fa47
MD5 69074b03e2ae98de6c66151c8965720d
BLAKE2b-256 46e90ea23f3ca603fd1fdbb9822f272f4c2fe19613d477e6b46a4d1c69259aaf

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