How to Calculate Standardized Residuals in Python

Spread the love

Standardized residuals are a crucial part of the statistical toolkit for assessing the quality of regression models. In this article, we’ll provide a comprehensive guide on how to calculate standardized residuals in Python, while exploring why they are important, what insights they can provide, and how they can help improve your regression models.

The Importance of Standardized Residuals

Before delving into the calculations, let’s understand why standardized residuals are important.

In regression analysis, residuals (the differences between observed and predicted values) offer valuable insight into the performance of a model. However, raw residuals can sometimes be challenging to interpret due to scale differences. This is where standardized residuals come in.

Standardized residuals are a form of residual that have been normalized to have a mean of 0 and a standard deviation of 1, much like a standard normal distribution. This standardization makes it easier to identify outliers and assess the model’s accuracy. Essentially, standardized residuals help us gauge whether the difference between the observed and predicted values is significant or just a product of random error.

Calculating Standardized Residuals in Python

Here is a step-by-step process of calculating standardized residuals for a simple linear regression model in Python:

Step 1: Import Required Libraries

First, import the necessary Python libraries:

import numpy as np
import pandas as pd
import statsmodels.api as sm

Step 2: Create or Load Dataset

Next, create or load your dataset. For this guide, we’ll create a simple synthetic dataset using numpy:

# Predictor variable
X = np.random.rand(100, 1)

# Response variable
Y = 1 + 2*X + np.random.randn(100, 1)

In this example, we’ve created a simple linear relationship between X (predictor) and Y (response), with some random noise added to Y.

Step 3: Fit a Linear Regression Model

Now, we’ll use the Ordinary Least Squares (OLS) regression model from the statsmodels library to fit our data:

# Add a constant to the predictor variable
X = sm.add_constant(X)

# Fit the OLS model
model = sm.OLS(Y, X).fit()

Step 4: Calculate Raw Residuals

The raw residuals can be obtained directly from the fitted model object. These are the differences between the observed and predicted values of the response variable:

# Calculate raw residuals
raw_residuals = model.resid

Step 5: Calculate Standardized Residuals

Finally, we can calculate the standardized residuals. We’ll divide the raw residuals by the standard deviation of the residuals:

# Calculate standardized residuals
standardized_residuals = raw_residuals / np.std(raw_residuals)

Interpreting Standardized Residuals

Once you’ve calculated the standardized residuals, you can use them to diagnose your regression model. A key benefit of standardized residuals is their straightforward interpretation:

  1. Residuals close to 0: These points are well explained by the model.
  2. Residuals with absolute values > 2: These are potential outliers. In a standard normal distribution, about 95% of values fall within two standard deviations from the mean.
  3. Residuals with absolute values > 3: These are likely outliers. In a standard normal distribution, about 99.7% of values fall within three standard deviations from the mean.

By identifying these values, you can check your data for potential errors, reevaluate your model, or take these outliers into account in further analysis.


Standardized residuals provide a valuable tool for interpreting and diagnosing regression models. By calculating standardized residuals in Python, we can identify outliers and other issues that might impact the quality of our model.

This guide provides a step-by-step process for calculating standardized residuals in Python, with a focus on a simple linear regression model. However, the same principles apply to more complex models. With the understanding of how to calculate and interpret standardized residuals, you can more effectively evaluate and refine your regression models.

Leave a Reply