Skip to main content

Diagnostic Plots for Lineare Regression Models. Similar to plot.lm in R.

Project description

lmdiag

Python Library providing Diagnostic Plots for Linear Regression Models. (Like plot.lm in R.)

I built this, because I missed the diagnostics plots of R for a university project. There are some substitutions in Python for individual charts, but they are spread over different libraries and sometimes don't show the exact same. My implementation tries to copycat the R-plots, but I didn't reimplement the R-code: The charts are just based on available documentation.

Installation

pip install lmdiag

Usage

lmdiag generates plots for fitted linear regression models from statsmodels or linearmodels.

You can find many examples in this jupyter notebook.

Example

import numpy as np
import statsmodels.api as sm
import lmdiag

# Fit model with random sample data
np.random.seed(20)
predictor = np.random.normal(size=30, loc=20, scale=3)
response = 5 + 5 * predictor + np.random.normal(size=30)
X = sm.add_constant(predictor)
lm = sm.OLS(response, X).fit()

# Plot lmdiag facet chart
lmdiag.style.use(style="black_and_red")  # Mimic R's plot.lm style
fig = lmdiag.plot(lm)
fig.show()

image

Methods

  • Draw matrix of all plots:

    lmdiag.plot(lm)

  • Draw individual plots:

    lmdiag.resid_fit(lm)

    lmdiag.q_q(lm)

    lmdiag.scale_loc(lm)

    lmdiag.resid_lev(lm)

  • Print description to aid plot interpretation:

    lmdiag.info() (for all plots)

    lmdiag.info('<method name>') (for individual plot)

Performance

Plotting models fitted on large datasets can be slow. There are some things you can try to speed it up:

1. Tune LOWESS-parameters

The red smoothing lines are calculated using the "Locally Weighted Scatterplot Smoothing" algorithm, which can be quite expensive. Try a lower value for lowess_it and a higher value for lowess_delta to gain speed at the cost of accuracy:

lmdiag.plot(lm, lowess_it=1, lowess_delta=0.02)
# Defaults are: lowess_it=2, lowess_delta=0.005

(For details about those parameters, see statsmodels docs.)

2. Change matplotlib backend

Try a different matplotlib backend. Especially static backends like AGG or Cairo should be faster, e.g.:

import matplotlib
matplotlib.use('agg')

Development

Setup environment

python -m venv .venv
source .venv/bin/activate
pip install -e '.[dev]'
pre-commit install

Certification

image

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

lmdiag-0.4.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

lmdiag-0.4.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file lmdiag-0.4.0.tar.gz.

File metadata

  • Download URL: lmdiag-0.4.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for lmdiag-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9d9a9e77c59533d806c9de48cb84482c6bfae5538d4ab4f66e838ded697d831d
MD5 739ca9ec6ed1082315c31462f2d78057
BLAKE2b-256 fcec3902e3f24d7fdbe9de7cb3dd372164d6c8d4f74f201670433c8128ad0ea8

See more details on using hashes here.

File details

Details for the file lmdiag-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: lmdiag-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for lmdiag-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd9c4fed59de21b576b1ebf7a5bf20c1e3c2fd7268175d07d225f5d84ca7e270
MD5 12d69baece834598e4a38be859749505
BLAKE2b-256 9240135d48a3ed2de4c2c7bd207218b1c64638cd4ca051fe0bdb8e23036d98bd

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