statsmodels.stats.multicollinearity.collinear_index#

statsmodels.stats.multicollinearity.collinear_index(data, tol=None)[source]#

Find the columns that are perfectly collinear with preceding columns

The columns are processed in sequence. A column is collinear if it is in the space spanned by the preceding columns that are not collinear.

Parameters:
dataarray_like

The design matrix with observations in rows and variables in columns. The matrix is not demeaned and the columns are not standardized. If a constant is part of the design, then it should be in the first column, for example by using add_constant.

tolfloat, optional

A column is collinear if the norm of its residuals in the regression on the preceding columns is less than or equal to tol times the norm of the column. The default is max(nobs, k_vars) times the machine epsilon, which is the default of numpy.linalg.matrix_rank.

Returns:
idx_collinearndarray or Index

Position of the columns that are collinear with preceding columns. This is an Index with the column labels if data is a pandas DataFrame.

idx_keepndarray or Index

Position of the columns that are not collinear with preceding columns. This is an Index with the column labels if data is a pandas DataFrame.

See also

vif

VIF for all columns, which is infinite for collinear columns.

Notes

Removing the collinear columns, i.e. selecting idx_keep, gives a design matrix of full column rank. Which of the columns that are part of a collinear relationship is flagged depends on the order of the columns, only the last column of each relationship is flagged.

The residuals are computed with Gram-Schmidt orthogonalization of the columns with reorthogonalization. A column that is flagged does not contribute to the orthogonal basis for the columns that follow it.

Examples

A constant and a full set of dummy variables are collinear.

>>> import numpy as np
>>> from statsmodels.stats.api import collinear_index
>>> dummies = np.tile(np.eye(3), (4, 1))
>>> x = np.column_stack((np.ones(12), dummies))
>>> idx_collinear, idx_keep = collinear_index(x)
>>> idx_collinear
array([3])
>>> idx_keep
array([0, 1, 2])