Residual Analysis for Diagnostics
A linear regression model shows a strong R², but your business intuition says something is off. How do you uncover the hidden flaws? Residual analysis is the key to understanding if a model's underlying assumptions hold, even with a high R².
The Model's Leftovers: What Are Residuals?
When a linear regression model makes a prediction, it aims to capture the underlying relationship between variables. However, no model is perfect; there will always be some difference between the actual observed values and the values the model predicts.
These differences are called residuals. Think of them as the 'error' or the 'unexplained' portion of the response variable that the model couldn't account for. Analyzing these leftovers provides crucial insights into how well the model truly fits the data and if its fundamental assumptions are met.
Why Residuals Matter: Validating Model Assumptions
Linear regression models rely on several key assumptions about the error terms (which residuals approximate). If these assumptions are violated, the model's coefficients, standard errors, and overall inferences can be unreliable, even if the R² value appears high.
Analyzing residual patterns helps us visually check for violations of assumptions such as linearity (the relationship is truly linear), independence (errors are not correlated), homoscedasticity (errors have constant variance), and normality (errors are normally distributed).
The Go-To Plot: Residuals vs. Fitted Values
The most fundamental diagnostic tool for residuals is a scatter plot of residuals against fitted (predicted) values. This plot helps assess the linearity and homoscedasticity assumptions of the model.
A healthy residual plot shows points randomly scattered around the horizontal line at zero, with no discernible pattern. This indicates that the model has captured the systematic relationship in the data, and the remaining errors are just random noise, fulfilling the model's assumptions.
Pattern Alert: Diagnosing Non-Linearity
If the relationship between the predictor and response variables is not truly linear, but a linear model is fitted, the residuals will often reveal this mismatch. A common indicator of non-linearity is a curved pattern in the residuals vs. fitted plot.
For example, a U-shaped or inverted U-shaped pattern suggests that the linear model is systematically under-predicting or over-predicting at certain ranges of the fitted values. This means the model is missing a non-linear component, and a transformation of variables or a non-linear model might be more appropriate.
Expanding Errors: Identifying Heteroscedasticity
Homoscedasticity is the assumption that the variance of the error terms is constant across all levels of the independent variables. When this assumption is violated, it's called heteroscedasticity.
In a residuals vs. fitted plot, heteroscedasticity often appears as a 'fanning out' or 'cone' shape. This means the spread of residuals increases (or decreases) as the fitted values change. Heteroscedasticity can lead to inefficient parameter estimates and incorrect standard errors, making hypothesis tests unreliable.
Are Errors Normal? The Q-Q Plot
Another crucial assumption for linear regression, particularly for inference (confidence intervals and p-values), is that the error terms are normally distributed. A Quantile-Quantile (Q-Q) plot is used to check this assumption.
A Q-Q plot compares the quantiles of your residuals against the quantiles of a theoretical normal distribution. If the residuals are normally distributed, the points on the plot will generally fall along a 45-degree reference line. Deviations from this line, especially at the tails, suggest non-normal errors, which can affect the validity of statistical tests.
Implementing Residual Diagnostics in Python
X and y where y has a clear linear relationship but with increasing variance (heteroscedasticity). Observe how the residual plot changes.Residuals are the differences between observed and predicted values, representing the unexplained portion of the data.
Residual analysis is a critical diagnostic tool for visually inspecting linear regression assumptions.
A residuals vs. fitted values plot is the primary tool to check for linearity and homoscedasticity.
Random scatter around zero in a residuals plot indicates a well-fitting model.
A curved pattern (e.g., U-shape) in residuals signals a violation of the linearity assumption.
A fanning out or cone shape in residuals indicates heteroscedasticity (non-constant error variance).
A Q-Q plot compares residual quantiles to theoretical normal quantiles, assessing the normality assumption.