import pandas as pd
import maketables as mt
# Load your data (here using a sample Stata dataset with the import_dta function that also stores variable labels)
df = mt.import_dta("https://www.stata-press.com/data/r18/auto.dta")Getting Started with MakeTables
This notebook contains interactive examples from the README to get you started with MakeTables.
Installation
First, make sure you have MakeTables installed:
pip install maketablesFor development installation:
git clone https://github.com/dsliwka/maketables.git
cd maketables
pip install -e .Descriptive Statistics Table
Let’s start with creating a descriptive statistics table using the classic auto dataset (Note that you can work with any pandas DataFrame but MakeTables also supports loading and saving dta files (Stata) as this facilitates working with variable labels).
# Create descriptive statistics table
mt.DTable(df, vars=["mpg","weight","length"], bycol=["foreign"],
caption="Descriptive Statistics")| Descriptive Statistics | ||||||
| Domestic | Foreign | |||||
|---|---|---|---|---|---|---|
| N | Mean | Std. Dev. | N | Mean | Std. Dev. | |
| Mileage (mpg) | 52.00 | 19.83 | 4.74 | 22.00 | 24.77 | 6.61 |
| Weight (lbs.) | 52.00 | 3,317.12 | 695.36 | 22.00 | 2,315.91 | 433.00 |
| Length (in.) | 52.00 | 196.13 | 20.05 | 22.00 | 168.55 | 13.68 |
Regression Tables
Using PyFixest
Use the versatile PyFixest package.
import pyfixest as pf
# Fit your models here using pyfixest
est1 = pf.feols("mpg ~ weight", data=df)
est2 = pf.feols("mpg ~ weight * length", data=df)
est3 = pf.feols("mpg ~ weight * length | foreign", data=df)
# Make the table
mt.ETable([est1, est2, est3], caption = "Regression Results",)| Regression Results | |||
| Mileage (mpg) | |||
|---|---|---|---|
| (1) | (2) | (3) | |
| coef | |||
| Weight (lbs.) | -0.006*** (0.001) |
-0.004** (0.002) |
-0.004 (0.001) |
| Length (in.) | -0.079 (0.056) |
-0.082* (0.008) |
|
| Weight (lbs.) × Length (in.) | -0.000 (0.000) |
-0.000 (0.000) |
|
| Intercept | 39.440*** (1.614) |
47.877*** (6.132) |
|
| fe | |||
| Car origin | - | - | x |
| stats | |||
| Observations | 74 | 74 | 74 |
| R2 | 0.652 | 0.661 | 0.674 |
| Significance levels: * p < 0.1, ** p < 0.05, *** p < 0.01. Format of coefficient cell: Coefficient (Std. Error) | |||
Using Statsmodels
MakeTables also works with Statsmodels for various types of regression models. Here is for instance a linear probability model and a probit.
import statsmodels.formula.api as smf
# Generate a dummy variable and label it
df["foreign_i"] = (df["foreign"] == "Foreign")*1
mt.set_var_labels(df, {"foreign_i": "Foreign (indicator)"})
# Fit your models
est1 = smf.ols("foreign_i ~ weight + length + price", data=df).fit()
est2 = smf.probit("foreign_i ~ weight + length + price", data=df).fit(disp=0)
# Make the table
mt.ETable([est1, est2], model_stats=["N","r2","pseudo_r2"],
model_heads=["OLS","Probit"],
caption="Regression Results (Statsmodels)")| Regression Results (Statsmodels) | ||
| Foreign (indicator) | ||
|---|---|---|
| OLS | Probit | |
| (1) | (2) | |
| coef | ||
| Weight (lbs.) | -0.001*** (0.000) |
-0.004*** (0.002) |
| Length (in.) | 0.007 (0.005) |
0.033 (0.045) |
| Price | 0.000*** (0.000) |
0.001*** (0.000) |
| Intercept | 0.656 (0.583) |
1.327 (4.992) |
| stats | ||
| Observations | 74 | 74 |
| R2 | 0.552 | - |
| Pseudo R2 | - | 0.606 |
| Significance levels: * p < 0.1, ** p < 0.05, *** p < 0.01. Format of coefficient cell: Coefficient (Std. Error) | ||
Combining different packages
You can also combine models from different packages in one table.
# Here we just reestimaate the linear probability model using pyfixest instead of statsmodels
est0=pf.feols("foreign_i ~ weight + length + price", data=df)
# Make the table
mt.ETable([est0, est1, est2], model_stats=["N","r2","pseudo_r2"],
model_heads=["OLS (PyFixest)","OLS (Statsmodels)","Probit"],
caption="Regression Results combining different packages")| Regression Results combining different packages | |||
| Foreign (indicator) | |||
|---|---|---|---|
| OLS (PyFixest) | OLS (Statsmodels) | Probit | |
| (1) | (2) | (3) | |
| coef | |||
| Weight (lbs.) | -0.001*** (0.000) |
-0.001*** (0.000) |
-0.004*** (0.002) |
| Length (in.) | 0.007 (0.005) |
0.007 (0.005) |
0.033 (0.045) |
| Price | 0.000*** (0.000) |
0.000*** (0.000) |
0.001*** (0.000) |
| Intercept | 0.656 (0.583) |
0.656 (0.583) |
1.327 (4.992) |
| stats | |||
| Observations | 74 | 74 | 74 |
| R2 | 0.552 | 0.552 | - |
| Pseudo R2 | - | - | 0.606 |
| Significance levels: * p < 0.1, ** p < 0.05, *** p < 0.01. Format of coefficient cell: Coefficient (Std. Error) | |||