Skip to content

Instantly share code, notes, and snippets.

@Alakhator
Created March 4, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alakhator/0dcc2f7a9ec3f2b71b85c0881ba8d507 to your computer and use it in GitHub Desktop.
Save Alakhator/0dcc2f7a9ec3f2b71b85c0881ba8d507 to your computer and use it in GitHub Desktop.
# Function to calculate VIF
def calculate_vif(data):
vif_df = pd.DataFrame(columns = ['Var', 'Vif'])
x_var_names = data.columns
for i in range(0, x_var_names.shape[0]):
y = data[x_var_names[i]]
x = data[x_var_names.drop([x_var_names[i]])]
r_squared = sm.OLS(y,x).fit().rsquared
vif = round(1/(1-r_squared),2)
vif_df.loc[i] = [x_var_names[i], vif]
return vif_df.sort_values(by = 'Vif', axis = 0, ascending=False, inplace=False)
X=df.drop(['Salary'],axis=1)
calculate_vif(X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment