statsmodels.stats.multicollinearity.vif#
- statsmodels.stats.multicollinearity.vif(data, *, is_moment=False, has_constant='raise')[source]#
Variance inflation factors of all variables
The variance inflation factor, VIF, of a variable is \(1 / (1 - R^2)\), where \(R^2\) is the coefficient of determination of the regression of the variable on all other variables and a constant. It measures by how much the variance of the estimate of the coefficient of the variable is inflated by the correlation with the other variables.
- Parameters:
- dataarray_like
The explanatory variables with observations in rows and variables in columns. The data should not include a constant, see has_constant. If is_moment is True, then this is a square covariance or correlation matrix of the variables.
- is_momentbool,
optional If True, then data is a symmetric, positive semi-definite moment matrix of the variables instead of the observations. Only the correlations are used, so the matrix can be a covariance or a correlation matrix. The default is False.
- has_constant{“raise”, “skip”},
optional How to treat constant columns, which have zero variance. If “raise”, the default, then a ValueError is raised. If “skip”, then the constant columns are ignored and the corresponding elements of the result are NaN. The other variables are not affected because the regressions always include a constant.
- Returns:
- Raises:
ValueErrorIf data contains missing or infinite values, if data has constant columns and has_constant is “raise”, or if is_moment is True and data is not a valid moment matrix.
See also
statsmodels.stats.outliers_influence.variance_inflation_factorVIF of a single variable, estimated by OLS.
vif_ridgeVIF of the parameter estimates of ridge regression.
collinear_indexColumns that are perfectly collinear with preceding columns.
Notes
The VIF of all variables are the diagonal elements of the inverse of the correlation matrix of the variables. This needs one matrix decomposition instead of one regression for each variable, which is much faster than calling
variance_inflation_factorfor each column. Up to numerical precision, the results are the same as those of that function with its defaultstandardize=True. The latter limits the VIF to \(10^{15}\) and warns if the design matrix is ill-conditioned, while this function returnsinffor variables that are collinear with other variables.Constants
The VIF requires a constant in the regressions. The variables are demeaned, which is equivalent to including a constant, and the constant column of a design matrix, for example
res.model.exog, has to be removed or skipped. A set of dummy variables that sums to one is an implicit constant. If the design matrix does not include another constant, then it has to be dropped, because the dummy variables are perfectly collinear after demeaning and their VIF is infinite.If is_moment is True and the moment matrix is not centered, for example the cross-product matrix
x.T @ x, then the VIF are those of regressions without a constant.Singular and nearly singular data
The computation uses the eigenvalues of the correlation matrix. The VIF is infinite for variables that are numerically perfectly collinear with other variables, i.e. if the correlation matrix is singular up to floating point precision. A variable that is not part of such a linear relationship has a finite VIF. VIFs above about \(10^{12}\) should be read as indicating that the variable is collinear because they are affected by floating point noise. Floating point precision of the data matters: a linear combination of single precision (float32) variables is collinear only up to the precision of the single precision data, and is not detected as perfectly collinear.
References
[1]Belsley, D. A., E. Kuh, and R. E. Welsch. 1980. Regression Diagnostics: Identifying Influential Data and Sources of Collinearity. Wiley.
Examples
The VIF of the explanatory variables of the Longley data, which are highly collinear.
>>> import statsmodels.api as sm >>> from statsmodels.stats.api import vif >>> exog = sm.datasets.longley.load_pandas().exog >>> vif(exog).round(1) GNPDEFL 135.5 GNP 1788.5 UNEMP 33.6 ARMED 3.6 POP 399.2 YEAR 759.0 dtype: float64
The design matrix of a model that is estimated with a formula has a constant, which is skipped.
>>> import statsmodels.formula.api as smf >>> mod = smf.ols("TOTEMP ~ GNPDEFL + GNP + UNEMP + ARMED + POP + YEAR", ... data=sm.datasets.longley.load_pandas().data) >>> vif(mod.data.orig_exog, has_constant="skip").round(1) Intercept NaN GNPDEFL 135.5 GNP 1788.5 UNEMP 33.6 ARMED 3.6 POP 399.2 YEAR 759.0 dtype: float64