Skip to content

SUMM/REF: create exog for predict at for margins and predictions #7071

Open

Description

@josef-pkt

(I didn't find an issue directly for automatically creating exog)

We need automatically created exog for prediction as tools to illustrate the effect of explanatory variables.
I'm trying to do this currently for OrderedModel in #7035

  • discrete_margins code has at code, but it is mostly for aggregation over sample observations (no prediction on grid, AFAICS)

  • sandbox.predict_functional has Kerby's code for prediction functions including confint

  • GAM has plot_partial to plot only the effect of the spline or smoother

  • I wrote a simple helper function in ENH: Ordinal model rebased2 #7035 but only in notebook, endog is 2-d multi-column, mainly in support of plotting

  • single column focus variable - oneway
    simplest case, all we need is a grid and fixed values of others, e.g. at mean. We can directly use model exog.

  • single column focus variables - two or multi-way
    would be nice for interactions, or splitting continuous prediction across a categorical variable (e.g. effect of age by gender)
    needs double grid

  • single column focus variables, but aggregated of other variables
    This is what margins use, but only for average prediction at a point (or two for effect of dummy/count, ATE)
    For functional prediction, i.e. ATE at many different points of a continuous predictor, would need either large memory or loop over grid points

  • multi-column terms and predictors

    • available in GAM for specific term not included in formula, we control the smooth part
    • user provided transformation, we don't have enough info and the user can currently not provide that info
    • using formulas, and going through it for prediction
      This is used in sandbox.predict_functional but I never went through the details to see how robust this is.
      Main problem is that we need to automatically create something like the user's original exog.
    • categorical: we can get the relevant info from design_info.
      I used it in t_test_pairwise to convert from different encodings, but it is currently not used for prediction. (e.g. in GLM. discrete)

asides:

  • location might be statsmodels.base similar to get_predictions
  • Results class hierarchy: This does not apply to TSA, at least not directly. We need something separate for non-tsa models. I'm not sure how it would work for proper panel data models, MixedLM and GEE (we only predict fixed effects? averaging over unobserved heterogeneity?).
  • extensions to multivariate/multinomial endog: not really directly relevant because this is about creating exog_predict and processing the prediction can be treated separately.

Activity

  1. josef-pkt commented on Sep 30, 2020

    @josef-pkt
    MemberAuthor

    My current version in a notebook for MNLogit and OrderedModel

    grid=np.arange(...) works for discrete variables

    def get_exog_predict(exog, focus_var, at="mean", grid=None):
        if at != "mean":
            raise ValueError("only mean currently available")
        if isinstance(exog, pd.DataFrame):
            if grid is None:
                grid = 11
            if np.ndim(grid) == 0:
                min = exog[focus_var].min(0)
                max = exog[focus_var].max(0)
                grid = np.linspace(min, max, grid)
            mean_other = pd.DataFrame(exog.mean()).drop(focus_var).T
            grid_focus = pd.DataFrame({focus_var:grid})
            exog_new = grid_focus.join(mean_other, how="outer")
            exog_new.fillna(method="ffill", inplace=True)
            exog_new = exog_new[exog.columns]
        else:
            exog = np.asarray(exog)
            if grid is None:
                grid = 11
            if np.ndim(grid) == 0:
                min = exog[:, focus_var].min(0)
                max = exog[:, focus_var].max(0)
                grid = np.linspace(min, max, grid)
            nobs = len(grid)
            exog_new = np.repeat(exog.mean(0)[None, :], nobs, axis=0)
            exog_new[:, focus_var] = grid
            
        return exog_new
    

    e.g. predicted probabilities for 3 choices, The focus exog is int from 1 to 7

    exog_new = get_exog_predict(exog, "selfLR", at="mean", grid=np.arange(1, 8)).iloc[:, :-1]
    pred_lr = res_omb.predict(exog_new)
    plt.plot(np.arange(1, 8), pred_lr);
    

    image

  2. josef-pkt commented on Mar 19, 2023

    @josef-pkt
    MemberAuthor

    see also similar, newer issue #8439

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