Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Term Entry] Python statsmodels: model residuals #5903

Merged
merged 27 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3d161b7
New file has been added.
SaviDahegaonkar Aug 6, 2024
5a211ba
Update user-input.md
cigar-galaxy82 Sep 8, 2024
38a6c7b
Merge branch 'main' into main
cigar-galaxy82 Sep 8, 2024
5923e4e
Update user-input.md
cigar-galaxy82 Sep 8, 2024
3e57a05
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Oct 16, 2024
7fd94ae
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Oct 17, 2024
ba4692a
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Oct 17, 2024
ea09cd7
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Nov 19, 2024
8ef7979
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 2, 2024
4c5cfc5
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 3, 2024
80df0b6
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 7, 2024
fbcbbfb
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 13, 2024
5787bae
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 18, 2024
492d478
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 19, 2024
63c93dc
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 27, 2024
608769a
File has been added.
SaviDahegaonkar Dec 28, 2024
9e4f0be
Update content/python/concepts/statsmodels/terms/model-residuals/mode…
SaviDahegaonkar Jan 13, 2025
9cec821
Update content/python/concepts/statsmodels/terms/model-residuals/mode…
SaviDahegaonkar Jan 13, 2025
2c9ed84
Update content/python/concepts/statsmodels/terms/model-residuals/mode…
SaviDahegaonkar Jan 13, 2025
f1206a7
Made the changes.
SaviDahegaonkar Jan 13, 2025
4e60b6d
Changes implemented in the file.
SaviDahegaonkar Jan 13, 2025
75e57d9
Changes have been done.
SaviDahegaonkar Jan 14, 2025
d84adfd
Updated model-residuals.md and added example image
SaviDahegaonkar Jan 14, 2025
21784fb
Updated model-residuals.md with improvements
SaviDahegaonkar Jan 15, 2025
8a2c77e
Removed the extra spaces.
SaviDahegaonkar Jan 15, 2025
af52531
Changes done.
SaviDahegaonkar Jan 15, 2025
43721f4
Merge branch 'main' into modelresi
Radhika-okhade Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
Title: 'Model Residuals'
Description: 'Analyzes residuals for Python statistical models, measure model performance, detect patterns, and diagnose problems using concise syntax and examples.'
Subjects:
- 'AI'
- 'Data Science'
- 'Machine Learning'
Tags:
- 'Data'
- 'Linear Regression'
- 'Logistic Regression'
- 'Models'
- 'Statsmodels'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**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:

![Model Residuals formula](https://raw.githubusercontent.com/Codecademy/docs/main/media/model-residuals-example.png)

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:

```pseudo
# Fit the model (if not already fitted)
model = sm.OLS(y, X).fit()
# Retrieve the residuals
residuals = model.resid
```

- `sm.OLS(y, X)`: Defines the `OLS` regression model with `y` as the dependent variable and `X` 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:

```py
import statsmodels.api as sm
import numpy as np

# Step 1: Create sample data
X = 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 term
X = sm.add_constant(X)

# Step 3: Fit the OLS model
model = sm.OLS(y, X).fit()

# Step 4: Calculate residuals
residuals = model.resid

# Step 5: Display residuals
print("Model Residuals:\n", residuals)
```

Here is the output for the code:

```shell
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.
Binary file added media/model-residuals-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading