Module 13: Regression Model Validation

Supporting Lectures:
EGN3443 Module 13 - Regression Model Validation

1. Residual Analysis

Definition

Residual analysis is a statistical technique used to assess the quality and assumptions of a regression model by examining the differences between observed and predicted values.

Key Components

Types of Residual Analysis

  1. Graphical Methods

  2. Statistical Tests

Example Calculation

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

# Sample data

X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)

y = np.array([2, 4, 5, 4, 5])

# Fit linear regression

model = LinearRegression()

model.fit(X, y)

# Calculate residuals

y_pred = model.predict(X)

residuals = y - y_pred

# Plot residuals

plt.scatter(y_pred, residuals)

plt.title('Residual Plot')

plt.xlabel('Predicted Values')

plt.ylabel('Residuals')

Web References

2. Influence Diagnostics

Definition

Influence diagnostics identify data points that have a significant impact on the regression model's parameters and overall fit.

Key Diagnostic Measures

  1. Leverage

  2. Cook's Distance

Example Calculation

import numpy as np

from statsmodels.stats.outliers_influence import OLSInfluence

import statsmodels.api as sm

# Sample regression model

X = sm.add_constant(X)

model = sm.OLS(y, X).fit()

# Compute influence measures

influence = OLSInfluence(model)

leverage = influence.hat_matrix_diag

cooks_distance = influence.cooks_distance[0]

Web References

3. Transformations

Definition

Transformations are mathematical modifications applied to variables to improve the regression model's fit, linearity, or meet statistical assumptions.

Common Transformations

  1. Log Transformation

  2. Square Root Transformation

  3. Box-Cox Transformation

Example Calculation

import numpy as np

from scipy import stats

# Log transformation

log_transformed = np.log(X)

# Box-Cox transformation

transformed_data, lambda_param = stats.boxcox(X)

Web References

4. Cross-Validation Techniques

Definition

Cross-validation is a resampling method used to evaluate a model's performance and generalizability.

Techniques

  1. K-Fold Cross-Validation

  2. Leave-One-Out Cross-Validation (LOOCV)

  3. Stratified K-Fold

Example Calculation

from sklearn.model_selection import cross_val_score, KFold

from sklearn.linear_model import LinearRegression

# K-Fold Cross-Validation

kf = KFold(n_splits=5)

model = LinearRegression()

scores = cross_val_score(model, X, y, cv=kf)

Web References

5. Prediction Intervals

Definition

Prediction intervals estimate the range within which a future individual observation is likely to fall, considering both model uncertainty and inherent variability.

Calculation

Formula:
Prediction Interval = Predicted Value ± t * SE * √(1 + 1/n)

Example Calculation

import numpy as np

import scipy.stats as stats

def prediction_interval(X, y, X_new, confidence=0.95):

    model = np.polyfit(X, y, 1)

    y_pred = np.polyval(model, X_new)

    

    # Calculation details omitted for brevity

    return y_pred, interval

# Usage

pred, pred_interval = prediction_interval(X, y, X_new)

Web References

Summary

This guide covers essential techniques for validating regression models, ensuring robust and reliable statistical analysis.