You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG or unclear definition, variance inflation factor with implicit constant in model #6249
There might be an inconsisteny in the usage of OLSrsquared 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
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])
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.
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
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.
Activity
josef-pkt commentedon Nov 13, 2019
If I use centered tss, then I get the same result as in the matrix version from #2380
with auxiliary regression
(based on a messy notebook to look at OLS with singular, dummy variable trap, exog)
josef-pkt commentedon Nov 13, 2019
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.
josef-pkt commentedon Nov 13, 2019
(annoying, I had to look up in the source/docs what the names for rss and tss are, very inconsistent naming)
josef-pkt commentedon Nov 13, 2019
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.
josef-pkt commentedon Nov 13, 2019
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)
josef-pkt commentedon Nov 13, 2019
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.josef-pkt commentedon Nov 13, 2019
using rsquared for vif doesn't require singular matrix inverse
modhas constant, all dummies of categorical and 2 continuous variables,constant and all dummies causes singular exog
vif for constant is 0, should also be
infvif for categorical dummies is correctly
infI haven't figured out a way yet to use
pinvand still getvif=inffor singular componentsbashtage commentedon Oct 28, 2026
Closing with #10623. The new
statsmodels.stats.multicollinearity.vifdocuments the definition (VIF always uses the centered data, i.e. a regression with a constant, checked againstcar::vifin 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_factorinmaingives the same values for the non-constant columns.