Skip to main content

A Package To Make Publication Quality Latex Tables From Python Regression Output

Project description

Pystout

A Package To Make Publication Quality Latex Tables From Python Regression Output

Installation

pip install pystout

Background

This package was built to emulate the features of estout or esttab in Stata although it lacks a lot of their functionality. This package has been tested with statsmodels OLS and linearmodels OLS, IV2SLS and PanelOLS. Current limitations are that it does not have full support for all relevant model statistics, though this will be added in future releases.

Existing Issues

  1. No option to look for categorical variables (panel or time dummies) and say "Yes" or "No"
  2. Currently treats "const" and "Intercept" as two separate variables if you mix and match sm.OLS() and sm.OLS.from_formula(), for example.
  3. Does not support full list of relevant statistics or estout options.

How to Use

If you specify a label and a title, the output is a table object; otherwise, the output is a tabular.

The normal use would be as follows (also in testing_pystout.py):

# Testing pystout
import statsmodels.api as sm
import linearmodels as ln
from pystout import pystout

dta = sm.datasets.webuse('auto')
dta.loc[:,'const'] = 1

y = dta.price

# =============================================================================
# First three models are from statsmodels
# =============================================================================
X = dta[['const','mpg','displacement']]
model1 = sm.OLS(y,X).fit()

X = dta[['const','mpg','displacement','turn']]
model2 = sm.OLS(y,X).fit()

X = dta[['displacement','const','turn']]
model3 = sm.OLS(y,X).fit()

# =============================================================================
# Next 2 are an ols and a tsls from linearmodels
# =============================================================================
X = dta[['displacement','const','turn','gear_ratio']]
model4 = ln.OLS(y,X).fit()

model5 = ln.IV2SLS(dependent=dta.price,endog=dta.mpg,
                   exog=dta.filter(['const','turn','displacement']),
                   instruments=dta.length).fit()

# =============================================================================
# Print result
# =============================================================================
pystout(models=[model1,model2,model3,model4,model5],
        file='test_table.tex',
        addnotes=['Here is a little note','And another one'],
        digits=2,
        endog_names=['Custom','Header','Please','Thanks','Again'],
        varlabels={'const':'Constant','displacement':'Disp','mpg':'MPG'},
        addrows={'Test':['A','Test','Row','Here','Too']},
        mgroups={'Statsmodels':[1,3],'L OLS':4,'L TSLS':5},
        modstat={'nobs':'Obs','rsquared_adj':'Adj. R\sym{2}','fvalue':'F-stat'}
        )

This produces the following table once compiled:

Alt text

Options

Pystout has the following options:

models:         A list of models to print.
                Currently must be fitted from statsmodels.OLS().fit() or linearmodels.

file:           This is the file name (including path) to write to.

exogvars:       If none, pull all exogenous variable names.
                These will be ordered in order of model (the constant will be put last).
                If ordered list, then only pull these variables from each model, if exist.

endog_names:    False generates numbered columns, True generates columns.
                Based on the exog_names in the models, passing a list makes custom column names.

varlabels:      Dictionary, or NoneType -- Custom labels for variables in table.
                Works for exog/endog variables.

stars:          Either False/None or a dictionary like: {.1:'+',.05:'*',.01:'**'}.

digits:         Number of digits to round all items to (default=2).

modstat:        You can add custom options from sm (F-stat, R-squared, Adjusted R-Squared)
                Should be a dictionary of {'Name':'statsmodel statistic'}.
                Currently only accepts: fvalue, fvalue_robust, rsquared, rsquared_adj,
                rsquared_within, nobs, aic, bic.

addnotes:       Add notes to the bottom of the table.
                (input is a list; each new element is a new line of comment).

addrows:        Add a row to the bottom of the dataframe, these will be above stats.
                Format is: {row name:[row,contents,as,list]}. (Default is an empty dictionary).
                List must be the same dimension as models (to preserve columns)

mgroups:        A dictionary that defines both the groups and what is in it.
                For example, mgroups={'Group 1':1,'Group 2':[2,5],'':[6,8]}.
                The keys are the group header (must be strings), and values are a list
                (corresponding to the min and max) or integer that defines the regression
                columns of group. You must specify a complete set of groups though
                you can define one as blank (as shown) this will cause that section
                to not have a header or a line underneath it.

footnotesize:   Currently accepts 'footnotesize' or 'scriptsize' or 'tiny'.
                Automatically compresses vertical space between separate footnotes.

title:          A Latex table caption that will be shown at the top of the table.

label:          A label to be used for referring to table in Latex,
                e.g. use \\ref{label} to refer to the table

tableopts:      The options declared for the table as in \begin{table}[tableopts]

scientific_notation:
                If True scientific notation will be used if value is less than 10**(-digits)

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

pystout-0.0.8.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

pystout-0.0.8-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file pystout-0.0.8.tar.gz.

File metadata

  • Download URL: pystout-0.0.8.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for pystout-0.0.8.tar.gz
Algorithm Hash digest
SHA256 f0c8e4e3448b7f53af67ae0f19b7405fae4c81e1b796402f09e428160a5bae8b
MD5 7fa62d3e4ec0468f484bfb4d9c161d15
BLAKE2b-256 a408b5e95085f4f1616749578ca31215c25659f139abbe7f96b28564e5468f71

See more details on using hashes here.

File details

Details for the file pystout-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: pystout-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for pystout-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 5d6d37ac791b90a4c9cb2658edc94f0cc8198e69255604c49014378fe9a5f3b9
MD5 78eebf0be813230ac9aa8620eb6779ca
BLAKE2b-256 026c3c5a93f598902d07398181521a5d4c1431ad2858575362b52eb736fb1e97

See more details on using hashes here.

Supported by

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