Skip to content

BUG or unclear definition, variance inflation factor with implicit constant in model #6249

Closed

Description

@josef-pkt

There might be an inconsisteny in the usage of OLS rsquared if there is an implicit constant in exog.
The definition and computation of rsquared changes if not constant is in the model.

When we drop one of the variables related to implicit constant, then that auxiliary regression does not have a constant, and uses the no-const definition for rsquared.

I'm not sure what the vif for those variables that are part of the implicit constant should be.

Example: one categorical variable with all levels included, no constant, 2 continuous variables.

vif using the current function doesn't match up with how it is computed in #2380

Activity

  1. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    If I use centered tss, then I get the same result as in the matrix version from #2380

    xftxfinv = np.linalg.inv(modf.exog.T.dot(modf.exog) / nobs)
    np.diag(xftxfinv) * modf.exog.var(0)
    

    with auxiliary regression

    r2c = 1 - res_aux.ssr / res_aux.centered_tss
    1 / (1 - r2c)
    

    (based on a messy notebook to look at OLS with singular, dummy variable trap, exog)

  2. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    But it is still not clear to me whether we should demean, scale and/or standardize, especially in the cases with implicit or no constant.

  3. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    (annoying, I had to look up in the source/docs what the names for rss and tss are, very inconsistent naming)

  4. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    motivation, AFAICS, we can easily compute vif from OLSResults with

    np.diag(resf.normalized_cov_params) * nobs * modf.exog.var(0)

    with some assumptions on the presence of a constant

    This only works in nonsingular case, if pinv = inv. Otherwise we get pinv regularized normalized_cov_params.

  5. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    I didn't realize before: It looks like vif is scale invariant, i.e. we don't have problems with variables in original scale in the current function. (vif is based on rsquared which is scale invariant)

    from statsmodels.stats.outliers_influence import variance_inflation_factor as vif
    
    np.array([vif(modf.exog, ii) for ii in range(modf.exog.shape[1])])
    array([1.15527511, 1.13422953, 1.16865924, 1.00303691, 1.00335499])
    
    np.array([vif(modf.exog / modf.exog.std(), ii) for ii in range(modf.exog.shape[1])])
    array([1.15527511, 1.13422953, 1.16865924, 1.00303691, 1.00335499])
    
  6. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    and one more:
    using uncentered tss in the no constant case might be the cause of #2376

    i.e. do we need an explicit constant or not in the current vif function.
    Maybe, if we always use centered rsquared, then we use an implicit constant, not checked yet, Maybe not, we might need to add a demean option.

  7. josef-pkt commented on Nov 13, 2019

    @josef-pkt
    MemberAuthor

    using rsquared for vif doesn't require singular matrix inverse

    mod has constant, all dummies of categorical and 2 continuous variables,
    constant and all dummies causes singular exog

    np.array([vif(mod.exog, ii) for ii in range(mod.exog.shape[1])])
    \statsmodels_py37\statsmodels\statsmodels\regression\linear_model.py:1684: RuntimeWarning: divide by zero encountered in double_scalars
      return 1 - self.ssr/self.centered_tss
    \statsmodels_py37\statsmodels\statsmodels\stats\outliers_influence.py:194: RuntimeWarning: divide by zero encountered in double_scalars
      vif = 1. / (1. - r_squared_i)
    array([0.        ,        inf,        inf,        inf, 1.00303691,
           1.00335499])
    

    vif for constant is 0, should also be inf
    vif for categorical dummies is correctly inf

    I haven't figured out a way yet to use pinv and still get vif=inf for singular components

  8. added
    type-possible-bug
    on Sep 28, 2026
  9. bashtage commented on Oct 28, 2026

    @bashtage
    Member

    Closing with #10623. The new statsmodels.stats.multicollinearity.vif documents the definition (VIF always uses the centered data, i.e. a regression with a constant, checked against car::vif in R), raises for or skips constant columns, and documents the implicit constant case: a full set of dummy variables without a constant column is perfectly collinear after centering and gets an infinite VIF. variance_inflation_factor in main gives the same values for the non-constant columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-possible-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions