freetable 3.0.0
Convert regression results (statsmodels or pyfixest) to publication-ready LaTeX tables
Requires Python: >=3.8

# freetable

![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)

Convert regression results (statsmodels or pyfixest) 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)](https://blog.max.pm/r_not_yours/).

<img src="example.png" alt="Example regression table output" width="500">

## Installation

```bash
pip install -U freetable
```

You'll also need either statsmodels or pyfixest (or both):
```bash
pip install statsmodels  # for statsmodels support
pip install pyfixest     # for pyfixest support
```

Or use optional dependency groups:
```bash
pip install freetable[statsmodels]  # includes statsmodels
pip install freetable[pyfixest]     # includes pyfixest
pip install freetable[all]          # includes both
```

## 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 (for statsmodels support) OR
- pyfixest (for pyfixest support)

**LaTeX packages:**
- threeparttable
- siunitx
- booktabs
- tabularx
- graphicx (only required if using `resize=True`)

Add these to your LaTeX document preamble:

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

Or just use [paper](https://github.com/mrpg/paper)!

## Supported Libraries

freetable supports regression results from:
- **statsmodels** - OLS, WLS, GLS, and other linear models
- **pyfixest** - High-performance fixed effects estimation

You can use models from either library interchangeably, or even mix them in the same table!

## Usage

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

### Basic example (statsmodels)

```python
import pandas as pd
import statsmodels.formula.api as smf
from freetable import tabularx

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 = tabularx(
    [m1, m2],
    model_names=["Model 1", "Model 2"],
    caption="Regression Results",
    label="tab:regression",
    digits=3,
    rename={"x1[T.Male]": "Male", "x2": "Age"},
    stars=(0.1, 0.05, 0.01),  # Order doesn't matter - automatically sorted
)

print(latex)
```

*Did you know?* You can replace `tabularx` by `tabularray`!

### Advanced example with all features

```python
import pandas as pd
import statsmodels.formula.api as smf
from freetable import tabularx

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 = tabularx(
    [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:

```bash
pip install -Ue ".[dev]"
```

Run tests:

```bash
pytest
```

Run type checking:

```bash
mypy src/freetable/
```

## License

Everything in this repository is licensed under [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/), or (at your option) any later version. See `LICENSE` for details. Note that there is **absolutely no warranty**.