Skip to main content

Nicely formatted regression reporting

Project description

Stargazer

This is a python port of the R stargazer package that can be found on CRAN. I was disappointed that there wasn't equivalent functionality in any python packages I was aware of so I'm re-implementing it here.

There is an experimental function in the statsmodels.regression.linear_model.OLSResults.summary2 that can report single regression model results in HTML/CSV/LaTeX/etc, but it still didn't quite fulfill what I was looking for.

The python package is object oriented now with chained commands to make changes to the rendering parameters, which is hopefully more pythonic and the user doesn't have to put a bunch of arguments in a single function.

Installation

You can install this package through PyPi with pip install stargazer or just clone the repo and take the stargazer.py file since it's the only one in the package.

Dependencies

It depends on statsmodels, which in turn depends on several other libraries like pandas, numpy, etc

Editing Features

This library implements many of the customization features found in the original package. Examples of most can be found in the examples jupyter notebook and a full list of the methods/features is here below:

  • title: custom title
  • show_header: display or hide model header data
  • show_model_numbers: display or hide model numbers
  • custom_columns: custom model names and model groupings
  • significance_levels: change statistical significance thresholds
  • significant_digits: change number of significant digits
  • show_confidence_intervals: display confidence intervals instead of variance
  • dependent_variable_name: rename dependent variable
  • rename_covariates: rename covariates
  • covariate_order: reorder covariates
  • reset_covariate_order: reset covariate order to original ordering
  • show_degrees_of_freedom: display or hide degrees of freedom
  • custom_note_label: label notes section at bottom of table
  • add_custom_notes: add custom notes to section at bottom of the table
  • add_line: add a custom line to the table
  • append_notes: display or hide statistical significance thresholds

These features are agnostic of the rendering type and will be applied whether the user outputs in HTML, LaTeX, etc

Example

Here is an examples of how to quickly get started with the library. More examples can be found in the examples.ipynb file in the github repo. The examples all use the scikit-learn diabetes dataset, but it is not a dependency for the package.

OLS Models Preparation

import pandas as pd
from sklearn import datasets
import statsmodels.api as sm
from stargazer.stargazer import Stargazer

diabetes = datasets.load_diabetes()
df = pd.DataFrame(diabetes.data)
df.columns = ['Age', 'Sex', 'BMI', 'ABP', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6']
df['target'] = diabetes.target

est = sm.OLS(endog=df['target'], exog=sm.add_constant(df[df.columns[0:4]])).fit()
est2 = sm.OLS(endog=df['target'], exog=sm.add_constant(df[df.columns[0:6]])).fit()


stargazer = Stargazer([est, est2])

HTML Example

stargazer.render_html()
Dependent variable: target
(1)(2)
ABP416.674***397.583***
(69.495)(70.870)
Age37.24124.704
(64.117)(65.411)
BMI787.179***789.742***
(65.424)(66.887)
S1197.852
(143.812)
S2-169.251
(142.744)
Sex-106.578*-82.862
(62.125)(64.851)
const152.133***152.133***
(2.853)(2.853)
Observations442442
R20.4000.403
Adjusted R20.3950.395
Residual Std. Error59.976 (df=437)59.982 (df=435)
F Statistic72.913*** (df=4; 437)48.915*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01

LaTeX Example

stargazer.render_latex()

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

stargazer-0.0.7.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

stargazer-0.0.7-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file stargazer-0.0.7.tar.gz.

File metadata

  • Download URL: stargazer-0.0.7.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for stargazer-0.0.7.tar.gz
Algorithm Hash digest
SHA256 ba40f35a492377cc1a1dcc55c45a63e177a6fc0d51c926b50a1b70b23a93c72f
MD5 d72c60665ca02413707a14aff341292f
BLAKE2b-256 a9aa60b4de7d3db9145ad9a20531a4339e3517ce075425bd066103b06340358f

See more details on using hashes here.

File details

Details for the file stargazer-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: stargazer-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for stargazer-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0bd47e7fd0a41317724743a63f6c92472d27adf8ab12d7e4d2682fc31aa51bca
MD5 f01791c14cc584def55bf9037e872d08
BLAKE2b-256 af490f0bc34f94e6ba65f8e5cc06208d11759153f02fb61f4a9f9db98e86bb68

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