Skip to main content

binspect

Binned scatterplots for linear specification diagnostics.

binspect estimates binned conditional means and compares them with a linear fit to the underlying observations. The bin means are the fitted values from the saturated model OLS(y ~ C(bin)). Their weighted deviations from the line provide a descriptive linear specification diagnostic.

binspect

import binspect

bs = binspect.binscatter(df, y="sales", x="age", bins=20)

bs.table  # per-bin means, SDs, standard errors, intervals
bs.summary_frame()  # one-row model and diagnostic table
bs.to_dict()  # JSON-compatible structured results
print(bs.summary())
bs.plot(theme="paper")
bs.audit(theme="paper")  # plot plus marginal distributions and residuals

Adjust both variables for numeric or categorical controls with FWL residualization:

adjusted = binspect.binscatter(
    df,
    y="sales",
    x="age",
    controls=["region", "tenure"],
    bins=20,
)
adjusted.fit.slope  # age coefficient from OLS(sales ~ age + region + tenure)
adjusted.plot()  # axes are explicitly labelled as adjusted

Residualized variables retain their original means, keeping the plot on a familiar scale. Categorical controls are indicator-encoded and a constant is included automatically. With weights=, the projection uses the same reliability weights.

For comparisons across groups, pooled bin edges are used by default so facets refer to the same intervals of x:

comparison = binspect.compare(
    df,
    y="sales",
    x="age",
    group="region",
    bins=20,
)

comparison.table  # one row per group and bin
comparison.summary_frame()  # one row per group
comparison.plot(sharex=True, sharey=True)

Pass common_bins=False to select bins separately within each group. The pooled estimate remains available as comparison.pooled.

Related packages

binsreg (Cattaneo, Crump, Farrell, and Feng) provides formal binscatter inference. binspect delegates optimal bin selection to it when requested. Use binsreg when uniform confidence bands or formal shape-restriction tests are required.

What it draws

The default plot presents the estimates, uncertainty, linear fit, lack of fit, and distribution of the exogenous variable as separate layers.

Layer What it shows Default
bins Bin means — the saturated-model fitted values on
ci Confidence bar per bin mean on
fit OLS line through the underlying data on
deviation Shading between bin means and the line — the lack of fit on
rug x-density, so quantile bins can't hide their own imbalance on
sd_line Slope σy/σx — the OLS line is this flattened by r off
smooth Local-linear smoother through the bin means off
raw Underlying observations at low alpha off

Three themes are included: notebook (default), paper (thin, serif, grayscale-safe), and deck (larger marks and type). Themes are colorblind-safe and scoped; importing binspect does not modify global rcParams.

Use bs.audit() for a composed diagnostic figure with the unchanged binscatter in the central panel, marginal histograms, and OLS residuals against fitted values. Either companion view can be omitted with marginals=False or residuals=False. These panels describe the stored estimate; they do not add a formal specification test.

One thing to know about η²

The bin-indicator model does not nest the linear model. Consequently, η² can be below the linear R² when bins are coarse, and their difference is not a valid curvature measure. binspect reports normalized lack of fit,

SS_lof = Σⱼ nⱼ (ȳⱼ − ŷ(x̄ⱼ))²      gap = SS_lof / SS_total

which is nonnegative by construction and corresponds to the deviations shown in the plot. This quantity is descriptive and is not a formal test of linearity.

Status

Initial alpha release (0.1.0). The API may continue to evolve during the 0.x series. The distribution name is binspect-regression; the import remains binspect.

Not yet implemented: cluster-robust standard errors, uniform confidence bands, and quantile regression. Standard errors are currently sd/√n within bin and assume independent observations.

Install

git clone https://github.com/joshuamyers22/binspect.git && cd binspect
pip install -e ".[dev]"
pytest

Install the published package with pip install binspect-regression and continue to write import binspect.

For contributing, release checks, and development conventions, see CONTRIBUTING.md. Please report vulnerabilities privately as described in SECURITY.md.

Maintainer release instructions are in RELEASING.md.

License

MIT.

Citation

The methodology this package leans on is Cattaneo, M. D., Crump, R. K., Farrell, M. H., & Feng, Y. (2024). "On Binscatter." American Economic Review, 114(5), 1488–1514. If you use binned scatterplots for inference, cite that paper and consider using binsreg directly.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binspect_regression-0.1.0.tar.gz (106.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

binspect_regression-0.1.0-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

Details for the file binspect_regression-0.1.0.tar.gz.

File metadata

  • Download URL: binspect_regression-0.1.0.tar.gz
  • Upload date:
  • Size: 106.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for binspect_regression-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a02da5e70e82480d71d0fb24e3ebef8655092e3a86debec7edac7016d36b3b87
MD5 d6ac856687bc3bb33a31ae1a272a8809
BLAKE2b-256 8e5e78aaab43212a4703255c0a18b7c6c9036fd53b6e924ac63bec0702fb6232

See more details on using hashes here.

Provenance

The following attestation bundles were made for binspect_regression-0.1.0.tar.gz:

Publisher: release.yml on joshuamyers22/binspect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binspect_regression-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for binspect_regression-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 449f76330bb92ecc4ef3b110165d0e1175fcaaead6442f821a9242e2abe1a316
MD5 5aa5813516405873b3945b68a5e32e03
BLAKE2b-256 c0e831ef56d7af569e490ec75ee15fd7b86f33c8ccf5c8be4eee6c7b83cefa7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for binspect_regression-0.1.0-py3-none-any.whl:

Publisher: release.yml on joshuamyers22/binspect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.2

2 files

0.1.1

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page