Skip to content

ENH/Design: collect tools for neighborhood indices, binning, coarsening #5388

Open

Description

@josef-pkt

This is similar to collecting linalg tools.

We need every once in a while function for defining neighborhood or converting continuous variables into groups or bins.

two different cases

  • neighborhoods of each observation:
    • for one, possibly multivariate variable: find neighbors e.g. local regression
    • for two variables: find neighbors in second dataset, e.g. matching estimators PSM
  • binning or coarsening (has weird name in pandas and R): partition continuous sample space
    partition dataset into finite discrete subsets
    several use cases already for binning, e.g. kde, in my differencing nonparametric PR, gof tests
    • grid: equal spaced
    • coarsening: approximately equal spaced

aside: setup cost and keeping intermediate information around.

AFAIR (might be incorrect):

  • lowess does not store the neighborhood indices, it is directly integrated in the estimation model, This makes it impossible to swap the neighborhood
  • full, non-binned kde also loops through the intervals without keeping neighborhood indices
  • scikit-learn keeps indices or neighborhood data as attribute for reuse during optimization and similar.

current use cases in statsmodels (AFAIR)

  • binning:
    • kde, my kernel regression, differencing PR (includes 2-D binning IIRC)
    • gof-tests, equal spacing or equal weight bins. chisquare test, Hosmer-Lemeshow (not yet) but similar in discrete._diagnostic_count
    • GAM, splines knot selection, defines intervals based on quantiles (of data) or equal spaced
  • neighbor matching
    • imputation
    • propensity score matching PR

related

  • discrete points instead of intervals: e.g. predict when we want to evaluate a function on percentiles of exog.

We need helper functions for the predict_at and margins use case to create new selective datasets, e.g. similar to the lsmeans case.
It is a bit cumbersome to create DataFrames when looking at different percentiles of one or two variables keeping the others constant or averaging over the others, or creating it for each level of the categorical variables. It's similar if we want to plot predictions at predefined grid values.

Activity

  1. josef-pkt commented on Nov 16, 2018

    @josef-pkt
    MemberAuthor

    Aside: R mgcv package is nice because it has the tools functions exposed and in the documentation, especially linalg tools.
    R robust base is the opposite, essentially everything is hiding behind a few high level function or models that combine various different approaches in a few huge functions.

  2. josef-pkt commented on Feb 1, 2019

    @josef-pkt
    MemberAuthor

    semi-random thought
    binning for discretized GOF test might be easier to do with pandas.
    i.e. I think we need unique_rows or pivot table or something similar after coarsening the data.

  3. josef-pkt commented on May 28, 2019

    @josef-pkt
    MemberAuthor

    (just found by chance in related search)

    binning with freq_weights or binning discrete distributions #4359
    I wrote the discrete distribution binning initially for the scipy unit test suite to use with chisquare test.

    Related, combining cells in categorical analysis and contingency tables to get a minimum number of observations in each bin.

    Kerby had added weightstats quantiles that also use freq_weights to compute the quantiles (i.e. corresponding to cut points)

  4. josef-pkt commented on Oct 4, 2020

    @josef-pkt
    MemberAuthor

    tool: (experiments for Hosmer-Lemeshow type tests)

    np.array_split works well for close to equal binning, i.e. split sorted arrays into close to equal subarrays to compute statistics like freq for chi2 test for subarrays.

    for n_ in range(90,101):
        print([arr.shape for arr in np.array_split(np.arange(n_), 10, axis=0)])
    [(9,), (9,), (9,), (9,), (9,), (9,), (9,), (9,), (9,), (9,)]
    [(10,), (9,), (9,), (9,), (9,), (9,), (9,), (9,), (9,), (9,)]
    [(10,), (10,), (9,), (9,), (9,), (9,), (9,), (9,), (9,), (9,)]
    [(10,), (10,), (10,), (9,), (9,), (9,), (9,), (9,), (9,), (9,)]
    [(10,), (10,), (10,), (10,), (9,), (9,), (9,), (9,), (9,), (9,)]
    [(10,), (10,), (10,), (10,), (10,), (9,), (9,), (9,), (9,), (9,)]
    [(10,), (10,), (10,), (10,), (10,), (10,), (9,), (9,), (9,), (9,)]
    [(10,), (10,), (10,), (10,), (10,), (10,), (10,), (9,), (9,), (9,)]
    [(10,), (10,), (10,), (10,), (10,), (10,), (10,), (10,), (9,), (9,)]
    [(10,), (10,), (10,), (10,), (10,), (10,), (10,), (10,), (10,), (9,)]
    [(10,), (10,), (10,), (10,), (10,), (10,), (10,), (10,), (10,), (10,)]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions