Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 20

Detecting Multicollinearity with VIF

Recommended resources

Hello! In our last lesson, we explored how to visualize relationships between variables using correlation heatmaps and pair plots. You saw firsthand how some features can be strongly correlated, providing similar information. This naturally leads to an important question: what are the implications of this redundancy, and how do we deal with it?

Today, we'll address that question directly. We will dive into the concept of multicollinearity, which occurs when two or more predictor variables are highly correlated. You will learn how to diagnose this issue using a standard statistical measure called the Variance Inflation Factor (VIF). Understanding and addressing multicollinearity is a crucial step in preparing data for many machine learning models, as it helps ensure that your model's coefficients are reliable and interpretable.

1. What is Multicollinearity and Why is it a Problem?

Before we start calculating anything, it's essential to build an intuition for what multicollinearity is and why it matters. In short, when features are highly correlated, it becomes difficult for a model to distinguish their individual effects on the target variable.

The A to Z of Multicollinearity | Variance Inflation Factor | Data Preprocessing | Data Science

To get a clear conceptual understanding, please watch the first few minutes of the video "The A to Z of Multicollinearity" from the Six Sigma Pro SMART channel.

Focus on these two parts: What is Multicollinearity?: This section provides a clear definition. Why it's a Problem: Pay close attention to the example with X1 and X2. It provides a great non-mathematical explanation of how multicollinearity leads to unstable and unreliable model coefficients.

As the video explains, if two predictors X1 and X2 contain very similar information, the model gets "confused." It might assign a large positive coefficient to X1 and a large negative one to X2, or vice-versa. Small changes in your data could cause these coefficients to swing wildly. This makes the model difficult to interpret and less trustworthy, even if its overall predictive accuracy isn't harmed.


2. Quantifying Multicollinearity with VIF

While a correlation matrix shows pairwise relationships, it doesn't capture more complex interactions where one feature might be a linear combination of several other features. This is where the Variance Inflation Factor (VIF) comes in.

VIF measures how much the variance of an estimated regression coefficient is inflated because of multicollinearity.

Conceptually, the VIF for a specific feature is calculated by:

  1. Running a linear regression model that uses this feature as the target and all other features as predictors.
  2. Calculating the R-squared () value from this regression. This tells you how well the other features can explain your chosen feature.
  3. Calculating the VIF using the formula: where is the R-squared from regressing feature i on all other features.

If the other features perfectly predict feature i (), the VIF is infinite. If the other features have no relationship with feature i (), the VIF is 1.

Interpreting VIF Values

So, what do the VIF numbers actually mean? Here are the commonly used rules of thumb:

Variance Inflation Factor: How to Detect Multicollinearity

This article from DataCamp provides a concise guide to interpreting VIF values.

Please read the section titled "Interpretation of VIF Values." It gives clear benchmarks for what constitutes low, moderate, and high multicollinearity.

To summarize the key points from the article:

  • VIF = 1: No correlation. The feature is completely independent of the others.
  • 1 < VIF < 5: Moderate correlation. This is often acceptable.
  • VIF > 5: High correlation. This is a cause for concern and may require further investigation.
  • VIF > 10: Very high correlation. Indicates serious multicollinearity, and the feature should likely be removed.

3. Calculating and Using VIF in Python

Now let's move to the practical implementation. We will use the statsmodels library in Python to calculate VIF.

A Crucial Pitfall: Adding a Constant

There's a critical detail when using statsmodels' VIF function: it assumes that the data you provide includes a constant (i.e., an intercept term), but it doesn't add one for you. This is a common source of error that leads to incorrect VIF values. Your background in computer science and experience with APIs will help you appreciate the importance of understanding such implementation details.

Variance Inflation Factor in Python

This Stack Overflow thread is an excellent real-world example of this problem and its solution.

Read the initial question and the top-voted answer. Notice how the user gets vastly different results compared to R. The answer explains that you must explicitly add a constant using add_constant from statsmodels.tools.tools.

Iterative VIF Calculation and Feature Removal

The correct way to use VIF for feature selection is an iterative process:

  1. Calculate the VIF for all features.
  2. Find the feature with the highest VIF.
  3. If this VIF is above your threshold (e.g., 5 or 10), remove that single feature.
  4. Repeat the process until all remaining features have VIFs below the threshold.

It is incorrect to calculate VIFs once and drop all features above the threshold simultaneously. When you remove one feature, the VIFs of all other features will change, so they must be recalculated.

The following video provides a hands-on demonstration of this exact iterative process.

Hands-on Multicollinearity Treatment | Variance Inflation Factor | Data Preprocessing in Python

This video, "Hands-on Multicollinearity Treatment," shows the entire workflow in a Colab notebook.

Please watch these two key sections: Introducing VIF: This shows the initial VIF calculation and how to interpret the alarmingly high initial values. Iterative Removal: This is the most important part. It demonstrates the correct, iterative approach using a while loop to drop the feature with the highest VIF until all remaining features are below the threshold.

Here is a complete code example that puts everything together. We'll use the California housing dataset from scikit-learn.

import pandas as pd
import numpy as np
from sklearn.datasets import fetch_california_housing
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.tools.tools import add_constant




# Load the dataset
housing = fetch_california_housing()
X = pd.DataFrame(housing.data, columns=housing.feature_names)




# --- The Iterative VIF Removal Process ---
vif_threshold = 10.0
features_to_keep = X.copy()



# We use a while loop to iteratively remove features
while True:



    # Add a constant for the VIF calculation
    X_with_const = add_constant(features_to_keep)
    



    # Calculate VIF for each feature
    vif_data = pd.Series([variance_inflation_factor(X_with_const.values, i) 
                          for i in range(X_with_const.shape[1])], 
                         index=X_with_const.columns).drop('const')
    



    # Get the feature with the highest VIF
    max_vif_feature = vif_data.idxmax()
    max_vif_value = vif_data.max()
    
    print(f"Iteration with {len(features_to_keep.columns)} features. Max VIF is for '{max_vif_feature}' with value {max_vif_value:.2f}")




    # If the highest VIF is below the threshold, we are done
    if max_vif_value < vif_threshold:
        break
    else:



        # Otherwise, drop the feature with the highest VIF and repeat
        print(f"Dropping '{max_vif_feature}'...")
        features_to_keep = features_to_keep.drop(columns=[max_vif_feature])

print("\nFinal set of features after VIF selection:")
print(features_to_keep.columns.tolist())

In some cases, you might encounter perfect multicollinearity, where one feature is a perfect linear combination of others. This results in an infinite VIF.

This image shows VIF calculation results where 'year' and 'age' have an infinite VIF. This is a classic example of perfect multicollinearity, as 'age' is likely calculated directly from 'year' (e.g., `age = 2024 - year`), making them perfectly redundant.

Conclusion

In this lesson, you have moved from visually identifying correlations to diagnostically measuring and acting on multicollinearity. This is a fundamental skill in the feature preprocessing stage of the machine learning workflow.

Key Takeaways:

  • Multicollinearity occurs when predictor variables are highly correlated, which can make model coefficients unstable and hard to interpret.
  • Variance Inflation Factor (VIF) quantifies how much a feature's variance is inflated by its relationship with other features.
  • VIF Interpretation: A VIF greater than 5 is a warning sign, and a VIF greater than 10 indicates serious multicollinearity.
  • Practical Implementation: Always use add_constant when calculating VIF with statsmodels. Feature removal must be an iterative process, removing the single highest-VIF feature and recalculating in each step.

You are now equipped to handle one of the common data issues that can trip up modeling. In our next module, we will formally begin our journey into machine learning by exploring the core theoretical concepts that underpin how models learn and generalize, starting with the crucial bias-variance tradeoff and the role of loss functions.