MultiCollinearity with Variance Inflation Factor

In [43]:
import numpy as np
import pandas as pd
import statsmodels.api as sm
from scipy import stats
In [44]:
df = pd.read_csv("diabetes2.csv")
In [45]:
df.head()
Out[45]:
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedigreeFunction Age Outcome
0 6 148 72 35 0 33.6 0.627 50 1
1 1 85 66 29 0 26.6 0.351 31 0
2 8 183 64 0 0 23.3 0.672 32 1
3 1 89 66 23 94 28.1 0.167 21 0
4 0 137 40 35 168 43.1 2.288 33 1
In [46]:
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
In [47]:
X.head()
Out[47]:
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI DiabetesPedigreeFunction Age
0 6 148 72 35 0 33.6 0.627 50
1 1 85 66 29 0 26.6 0.351 31
2 8 183 64 0 0 23.3 0.672 32
3 1 89 66 23 94 28.1 0.167 21
4 0 137 40 35 168 43.1 2.288 33
In [48]:
y.head()
Out[48]:
0    1
1    0
2    1
3    0
4    1
Name: Outcome, dtype: int64
In [49]:
names = X.columns
In [50]:
names
Out[50]:
Index(['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
       'BMI', 'DiabetesPedigreeFunction', 'Age'],
      dtype='object')
In [59]:
for i in range(0,len(names)):
    y1 = X.loc[:,X.columns == names[i]]
    x  = X.loc[:,X.columns != names[i]]
    model = sm.OLS(y1,x)
    output = model.fit()
    r_squared = output.rsquared
    vif = round(1/(1-r_squared),2)
    if vif>=5:
        print("We can exclude this feature as the Variance Inflation Factor for column {} is {}".format(names[i],vif))
        print("The R-Squared value for column {} is {} with all the other columns as features".format(names[i],r_squared))
        
    else:
        print("We can include this feature as the Variance Inflation Factor for column {} is {}".format(names[i],vif))
        print("The R-Squared value for column {} is {} with all the other columns as features".format(names[i],r_squared))
We can include this feature as the Variance Inflation Factor for column Pregnancies is 3.28
The R-Squared value for column Pregnancies is 0.6947262230396156 with all the other columns as features
We can exclude this feature as the Variance Inflation Factor for column Glucose is 16.73
The R-Squared value for column Glucose is 0.9402095460899875 with all the other columns as features
We can exclude this feature as the Variance Inflation Factor for column BloodPressure is 14.62
The R-Squared value for column BloodPressure is 0.9315982643175567 with all the other columns as features
We can include this feature as the Variance Inflation Factor for column SkinThickness is 4.01
The R-Squared value for column SkinThickness is 0.7505423502883466 with all the other columns as features
We can include this feature as the Variance Inflation Factor for column Insulin is 2.06
The R-Squared value for column Insulin is 0.51543093330839 with all the other columns as features
We can exclude this feature as the Variance Inflation Factor for column BMI is 18.41
The R-Squared value for column BMI is 0.9456784022240197 with all the other columns as features
We can include this feature as the Variance Inflation Factor for column DiabetesPedigreeFunction is 3.2
The R-Squared value for column DiabetesPedigreeFunction is 0.6870722362261501 with all the other columns as features
We can exclude this feature as the Variance Inflation Factor for column Age is 13.49
The R-Squared value for column Age is 0.9258874161727161 with all the other columns as features
In [ ]: