{ "cells": [ { "cell_type": "markdown", "id": "0f30665a", "metadata": {}, "source": [ "# Regression Plots\n", "\n", "Regression EDA assumes the model **y = f(x) + e** with a *continuous*\n", "predictor `x`. These plots diagnose the fit quality and stability of a\n", "linear regression as a function of position in `x`.\n", "\n", "Pass `x` as a continuous numeric array to `EDAData`.\n", "\n", "Reference: [NIST Handbook Chapter 4](https://www.itl.nist.gov/div898/handbook/pmd/pmd.htm)" ] }, { "cell_type": "code", "execution_count": null, "id": "411ac3ba", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from drippy import EDAData\n", "from drippy import (\n", " scatter_plot,\n", " six_plot,\n", " linear_correlation_plot,\n", " linear_intercept_plot,\n", " linear_slope_plot,\n", " linear_residual_sd_plot,\n", ")\n", "\n", "rng = np.random.default_rng(42)\n", "\n", "# 50-point calibration curve\n", "x = np.linspace(0, 10, 50)\n", "y = 2.5 * x + 3.0 + rng.normal(scale=1.5, size=50)\n", "data = EDAData(y=y, x=x)" ] }, { "cell_type": "markdown", "id": "c13951da", "metadata": {}, "source": [ "## Scatter Plot (NIST 1.3.3.26)\n", "\n", "The starting point for any regression EDA: raw `y` vs `x` to confirm\n", "linearity and spot outliers before fitting." ] }, { "cell_type": "code", "execution_count": null, "id": "cd5801ae", "metadata": {}, "outputs": [], "source": [ "fig, ax = scatter_plot(data)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "4b1434c2", "metadata": {}, "source": [ "## 6-Plot (NIST 1.3.3.33)\n", "\n", "Comprehensive regression diagnostic in a 2×3 grid:\n", "\n", "1. Scatter with fitted line\n", "2. Residuals vs x\n", "3. Lag plot of residuals\n", "4. Histogram of residuals\n", "5. Normal probability plot of residuals\n", "6. Run sequence of residuals\n", "\n", "If residuals in panels 2–6 look like white noise, the linear model is adequate." ] }, { "cell_type": "code", "execution_count": null, "id": "78f4c0dd", "metadata": {}, "outputs": [], "source": [ "fig, axes = six_plot(data)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "80f0f2fa", "metadata": {}, "source": [ "## Linear Correlation Plot (NIST 1.3.3.16)\n", "\n", "Plots the Pearson correlation coefficient from a rolling window of\n", "observations. Stable, high correlation confirms that the linear\n", "model holds throughout the data range; dips indicate local non-linearity." ] }, { "cell_type": "code", "execution_count": null, "id": "4a21ef0b", "metadata": {}, "outputs": [], "source": [ "fig, ax = linear_correlation_plot(data)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "42327e5b", "metadata": {}, "outputs": [], "source": [ "fig, ax = linear_correlation_plot(data, window=20)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "4188b7ca", "metadata": {}, "source": [ "## Linear Intercept Plot (NIST 1.3.3.19)\n", "\n", "Rolling OLS intercept. Drift in the intercept over the data sequence\n", "may indicate calibration shift or a non-stationary baseline." ] }, { "cell_type": "code", "execution_count": null, "id": "631a9952", "metadata": {}, "outputs": [], "source": [ "fig, ax = linear_intercept_plot(data)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "49419512", "metadata": {}, "source": [ "## Linear Slope Plot (NIST 1.3.3.20)\n", "\n", "Rolling OLS slope (sensitivity). A flat line confirms constant\n", "sensitivity; trends reveal systematic changes in the response." ] }, { "cell_type": "code", "execution_count": null, "id": "807eb8d6", "metadata": {}, "outputs": [], "source": [ "fig, ax = linear_slope_plot(data)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "809f8e63", "metadata": {}, "source": [ "## Linear Residual SD Plot (NIST 1.3.3.22)\n", "\n", "Rolling residual standard deviation. Increasing SD indicates\n", "heteroscedasticity — variance that grows with `x`." ] }, { "cell_type": "code", "execution_count": null, "id": "550589bb", "metadata": {}, "outputs": [], "source": [ "fig, ax = linear_residual_sd_plot(data)\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 5 }