check_independence

check_independence

check_independence.py Module for testing independence of residuals in linear regression models.

This module provides tools for detecting autocorrelation in residuals from a fitted linear regression model using the Durbin-Watson statistic. Independence of residuals is a key assumption for valid inference in linear modeling.

Functions

  • check_independence(df, target) Fits a linear regression model and tests for independence of residuals using the Durbin-Watson statistic to detect autocorrelation.

Functions

Name Description
check_independence Checks the independence of residuals using the Durbin-Watson statistic.

check_independence

check_independence.check_independence(df, target)

Checks the independence of residuals using the Durbin-Watson statistic.

This function fits a linear regression model on the provided dataframe, calculates the residuals, and then computes the Durbin-Watson score to determine if autocorrelation is present in the residuals. Independence is a key assumption for valid inference in linear modeling.

Parameters

Name Type Description Default
df pandas.DataFrame Input DataFrame with feature columns and the target column. Only numeric features will be used as predictors. required
target str Name of the target column. The column must be numeric and the name must match the column name in the dataframe. required

Returns

Type Description
dict A dictionary containing: - ‘dw_statistic’ (float): The calculated Durbin-Watson value (0 to 4). - ‘is_independent’ (bool): True if the statistic is near 2 (typically 1.5 to 2.5), suggesting no significant autocorrelation. - ‘message’ (str): A brief interpretation of the result.

Examples

>>> import pandas as pd
>>> df = pd.DataFrame({
...     "x1": [1, 2, 3, 4, 5],
...     "x2": [2, 4, 5, 7, 8],
...     "y": [10, 20, 25, 35, 40]
... })
>>> check_independence(df, target="y")
{'dw_statistic': np.float64(0.0727), 'is_independent': False, 'message': 'Positive autocorrelation detected. Residuals may not be independent.'}