Last active
September 1, 2020 14:05
-
-
Save AdamSpannbauer/c99c366b0c7d5b6c4920a46c32d738e5 to your computer and use it in GitHub Desktop.
VIF utility function for checking multicollinearity with stats models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
import statsmodels.api as sm | |
from statsmodels.stats.outliers_influence import variance_inflation_factor | |
def print_vif(x): | |
"""Utility for checking multicollinearity assumption | |
:param x: input features to check using VIF. This is assumed to be a pandas.DataFrame | |
:return: nothing is returned the VIFs are printed as a pandas series | |
""" | |
# Silence numpy FutureWarning about .ptp | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") | |
x = sm.add_constant(x) | |
vifs = [] | |
for i in range(x.shape[1]): | |
vif = variance_inflation_factor(x.values, i) | |
vifs.append(vif) | |
print('VIF results\n-------------------------------') | |
print(pd.Series(vifs, index=x.columns)) | |
print('-------------------------------\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment