Repository navigation
Description
In the source code,
the line below doesnt take the constant term (interception) into account.
r_squared_i = OLS(x_i, x_noti).fit().rsquared
For example, data is:
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
For regression,
fit <- lm(mpg~disp+hp+wt+drat, data=mtcars)
vif( fit ) should give
disp hp wt drat
8.209402 2.894373 5.096601 2.279547
But vif( df[:, ['disp', 'hp', 'wt','drat'].values(), 0 ) will give 37.1154 instead of 8.209
Activity
josef-pkt commentedon Apr 28, 2015
I wrote
variance_inflation_factorunder the assumption that we use the design matrix of or for a regression that already includes a constant.At least the docstring needs to be improved.
I never looked at the case when there is no constant in the regression. I'm not sure the interpretation of the demeaned vif still holds in that case.
I didn't think VIF would become so popular. I have a new version for use with just the data that demeans but I haven't included it yet in statsmodels.
Thanks for the feedback.
josef-pkt commentedon Apr 28, 2015
The shortest version for standard vif I can come up with might make statsmodels obsolete if we advertise it :)
print(np.diag(np.linalg.inv(np.corrcoef(x, rowvar=0))))using
invassumes no perfect collinearity.using pinv would regularize.
the above is the same as the explicit version (rsquared of auxiliary OLS)
(I have a few more variations on this with different numerical stability properties and explicit or implicit constant.)
related issue ???: partial correlation
I don't have yet: sequential partial correlation using qr (or cholesky)
josef-pkt commentedon Apr 29, 2015
because of numerical precision noise, inv with singular x might not fail
example: last column is sum of two previous columns
using pinv is very misleading for interpretation as vif (they don't indicate singularity because of regularization)
josef-pkt commentedon Apr 29, 2015
see also issue #2157
tasks in vif:
invand don't care about anything elseneed also unit test with categorical dummies where smallest eigenvalue could be exactly zero.
josef-pkt commentedon Apr 29, 2015
Theory: I guess we can calculate vif for pinv (PCA/PCR) regularized regression in the same way as for Ridge regression.
I've never seen it mentioned anywhere.
And it doesn't seem to make a difference
josef-pkt commentedon Apr 29, 2015
see #1669 for original issue on enhancing variance_inflation_factor for Ridge regression.
stevenlis commentedon Feb 24, 2019
Since VIF is a common way to test for multicollinearity, are you planning to add this as a
RegressionResultsmethod?josef-pkt commentedon Dec 10, 2019
there are some references about constant in vif and similar (I didn't look for them yet, just saw them mentioned in other articles.)
Belsley, David A. 1984. “Demeaning Conditioning Diagnostics through Centering.” The American Statistician 38 (2): 73–77. https://doi.org/10.2307/2683236.
Demeaning improves condition number of demeaned exog, but does not change the underlying multicollinearity problem in the model/data. Vif is only mentioned in a footnote
(IIRC, vif is unchanged, unit test vif with demeaning is same as vif with explicit constant.)
1 remaining item
josef-pkt commentedon Dec 10, 2019
That's about the same as what the current function
variance_inflation_factordoes. It also assumes thatinput_datacontains all variables including a constant or is demeaned.josef-pkt commentedon Dec 12, 2019
another article about the role of the constant in vif, based on abstract (I didn't read article)
Jensen, D. R., and D. E. Ramirez. 2013. “Revision: Variance Inflation in Regression.” Research article. Advances in Decision Sciences. 2013. https://doi.org/10.1155/2013/671204.
josef-pkt commentedon Dec 13, 2019
problem with constant is still ongoing
Velilla, Santiago. 2018. “A Note on Collinearity Diagnostics and Centering.” The American Statistician 72 (2): 140–46. https://doi.org/10.1080/00031305.2016.1264312.
Christensen, Ronald. 2018. “Comment on ‘A Note on Collinearity Diagnostics and Centering’ by Velilla (2018).” The American Statistician 72 (1): 114–17. https://doi.org/10.1080/00031305.2017.1392896.
Gómez, Román Salmerón, Catalina García García, and Jose García Pérez. 2019. “Comment on ‘A Note on Collinearity Diagnostics and Centering’ by Velilla (2018).” The American Statistician 0 (0): 1–4. https://doi.org/10.1080/00031305.2019.1635527.
we can get a measure for multicollinearity with a constant but Gomez et al argue that it's not a "vif" but a "Steward k", (or something like that)
(defining
variance inflation factoras the increase of the variance of the parameter estimate relative to some fictitious uncorrelated model/data)(I didn't read all those debates, just superficial skimming)
josef-pkt commentedon Dec 13, 2019
using uncentered Rsquared in auxiliary regression to define VIFnc which is the same as Steward's k
Gómez, Román Salmerón, and Catalina García García y José García Pérez. 2019. “Centered and Non-Centered Variance Inflation Factor.” ArXiv:1905.12293 [Econ, Stat], May. http://arxiv.org/abs/1905.12293.
I think I have also seen another article for uncentered vif, but don't remember which
josef-pkt commentedon Dec 13, 2019
general comment as in the first answer
Do we want to evaluate multicollinearity and numerical problems
short answer: we want both, i.e. more options
similar issue already for condition number
Garcia et al argue vif should be unchanged to scaling and mean shifts (and only applied to slope parameters)
However, a specific
exogmight be based on a "bad" parameterization, e.g. dummy variable trap, empty cells for rank deficiency or bad scaling. (power polynomial on calender years)Belsley 1984 on centering argues that centered condition number might not reflect the problems with actual exog i.e. be much smaller than cond. nr of full exog.
josef-pkt commentedon Dec 17, 2019
I think now that the entire discussion in the literature in the previous comment is largely bikeshedding (is the vif green or blue)
Stata has centered and uncentered vif referencing a book by Belsley that is out of print and I don't have access to. Stata default is centered.
going back to Belsley, Kuh, Welsch which recommend scaling to unit length (in squares)
"This scaling is similar to that used to transform the cross products matrix XrX into a
correlation matrix, except that the “mean zero” property is not needed, and, indeed, would be
inappropriate in the event that X contains a constant column."
footnote 14 p. 120
this is for condition number, condition indices and variance decomposition to singular vectors.
AFAIU, this is equivalent to using non-centered Rsquared in the usual definition of vif = 1 / (1 - r2)
The current version in outlier_influence uses automatic centered/uncentered based on the presence of a constant in the auxiliary regression. (which only causes a problem in the implicit constant case)
I think after OLS we want uncentered vif, similar to our use of condition number on the untransformed exog, i.e. numerical properties of the actual exog.
For supporting code, standalone diagnostic functions, we want to offer all options of raw, unit scaling and standardizing the data for those measures.
(aside: the warning sentence for large condition number in OLS summary should mention bad scaling as possible problem)
josef-pkt commentedon Jan 1, 2020
parking here
a japanese blog post with good explanation including vif and variable selection
(based on google translation)
https://future-chem.com/esol-reg-aic/
uses simple vif computation for pandas (data without constant)
MaverickMeerkat commentedon Apr 18, 2023
I'm not sure why 8 years after (!!!) an innocent user who got unreasonably high VIF has to stumble upon this thread for answers. 1000 thumbs down.
josef-pkt commentedon Apr 18, 2023
Well the debate about whether to include or not the constant in multicollinearity test in the literature between two groups of authors also lasted several years - without conclution.
There is a one-liner in the 3rd comment above, if you don't like to check your design matrix directly as is.
My 2015 pull request for multicollinearity checks stalled because I could not decide how to integrate different versions for the different cases of either scaling or constant/demeaning options.
MaverickMeerkat commentedon Apr 18, 2023
Not sure what the debate is, but the OLS code used in this VIF gives back a "non-centered" R^2, which is incredibly high. All the variables in my data come out to be extremely colinear... which strikes me as wrong.
josef-pkt commentedon Apr 18, 2023
I guess if you get uncentered R-squared, then you did not include the constant in your x (
exog). You need the constant in x if you include it also in the regression.The code uses the
r_squaredattributes which changes definition in OLS/WLS if there is no constant in the model.The problem is that I never updated the docstring to be more explicit about which vif it computes
https://www.statsmodels.org/dev/generated/statsmodels.stats.outliers_influence.variance_inflation_factor.html
aishwary0812 commentedon Dec 1, 2023
Hi @josef-pkt,
As you mentioned in the 3rd comment above, for calculating VIF one can use the below shortcut:-
print(np.diag(np.linalg.inv(np.corrcoef(x, rowvar=0))))
wherein using inv assumes no perfect collinearity.
On the contrary using the existing statsmodels variance_inflation_factor function has a provision of outputting variables having VIF='inf' values as well but it is too slower than the above short trick(O(n^2).
Is there any technique(without using the above statsmodels variance_inflation_factor function as it's too time consuming) where I can
Thanks
josef-pkt commentedon Dec 1, 2023
The easiest might be to use a QR or (better) pivoting QR to find the sequentially collinear columns.
AFAIR, all variables where the value of the diagonal of the R matrix are close to zero are collinear or near collinear.
In this case order matters because the decomposition is essentially a regression on all preceding columns.
(I don't remember right now where or in which PR I had parked a helper function for this.)
Note, dropping collinear variables, so that only non-collinear variables are left, is a bit arbitrary in which of the collinear columns are dropped. QR drops in sequence, however pivoting QR and similar methods permute the variable order so that the "most collinear" variables are at the end. Which variables are the "most collinear" depends on small changes in the data, consequently the selection which variables are dropped is not stable across similar datasets.
bashtage 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.