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).

Installation

pip install -U git+https://github.com/mrpg/freetable.git@master

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()
m2 = smf.ols("outcome ~ treatment + control1", data=df).fit()
m3 = smf.ols("outcome ~ treatment + control1 + control2", data=df).fit()

# 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.0.0.tar.gz (11.6 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.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for freetable-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1fbb5f9f83fa95e291a77976ca8218ab1eef35a12023ee82801080df1e8460f4
MD5 a5f047a4d094fea108a2e539427891f2
BLAKE2b-256 d0cacb940d45cf8d1bb4f8a59180b81971747b8046d35cc3b2e31858691a3713

See more details on using hashes here.

File details

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

File metadata

  • Download URL: freetable-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3bfcf757f6f0ef17688f49bf2e1db3e0b033721e0da4124fb25039a0958d6be6
MD5 14a7167d4a5edbc9130938a0e7049b51
BLAKE2b-256 cf20202763216f76f92a03960f6cd2be0980efc7b87db644e6a99afbaad09238

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