Model Residuals
Model residuals are calculated as the differences between observed and predicted values for a statistical model. This method measures error or deviation for each data point by using the formula:
Residuals are a key concept in statistical modeling. They are used to evaluate the goodness of fit, identify patterns, detect outliers, and validate assumptions about the model. Analyzing residuals helps enhance model accuracy and reliability by providing information about areas where the model is underperforming.
Syntax
Here is the general syntax for calculating Model Residuals:
# Fit the model (if not already fitted)
model = sm.OLS(y, X).fit()
# Retrieve the residuals
residuals = model.resid
sm.OLS(y, X)
: Defines theOLS
regression model withy
as the dependent variable andX
as the independent variable..fit()
: Fits the model to the data.model.resid
: Extracts the residuals from the fitted model.
Example
In this example, a linear regression model is fitted using statsmodels, and the residuals are calculated:
import statsmodels.api as smimport numpy as np# Step 1: Create sample dataX = np.random.rand(5, 1) # Independent variable (100 samples)y = 3 * X + np.random.randn(5, 1) # Dependent variable with noise# Step 2: Add constant to X for the intercept termX = sm.add_constant(X)# Step 3: Fit the OLS modelmodel = sm.OLS(y, X).fit()# Step 4: Calculate residualsresiduals = model.resid# Step 5: Display residualsprint("Model Residuals:\n", residuals)
Here is the output for the code:
Model Residuals:[0.07524913 -1.02179262 1.42678355 -1.50131552 1.02107546]
These values indicate how much each prediction deviates from the true value. A smaller residual means the prediction is closer to the actual value, while larger residuals indicate a greater deviation.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours