.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/regression/plot_01_ols_diagnostics.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_statistics_regression_plot_01_ols_diagnostics.py: OLS regression with residual diagnostics ================================================ Fits a noisy linear relationship and inspects the fit: coefficient standard errors/p-values, R^2, and a residuals-vs-fitted-values plot. .. GENERATED FROM PYTHON SOURCE LINES 10-15 .. code-block:: Python import numpy as np from mathematicskit.statistics import linear_regression from mathematicskit.statistics.visualizers.plots import plot_regression_fit, plot_residuals .. GENERATED FROM PYTHON SOURCE LINES 16-18 Simulate noisy linear data and fit ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 18-28 .. code-block:: Python rng = np.random.default_rng(0) x = np.linspace(0.0, 10.0, 60) y = 3.0 * x + 5.0 + rng.normal(0.0, 2.0, x.shape) result = linear_regression(x, y) print(f"intercept = {result.coefficients[0]:.3f} (se={result.standard_errors[0]:.3f}, p={result.p_values[0]:.4f})") print(f"slope = {result.coefficients[1]:.3f} (se={result.standard_errors[1]:.3f}, p={result.p_values[1]:.4f})") print(f"R^2 = {result.r_squared:.4f}, adjusted R^2 = {result.adjusted_r_squared:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none intercept = 4.608 (se=0.458, p=0.0000) slope = 3.109 (se=0.079, p=0.0000) R^2 = 0.9639, adjusted R^2 = 0.9633 .. GENERATED FROM PYTHON SOURCE LINES 29-31 Plot the fit and residual diagnostics ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: Python plot_regression_fit(x, y, result) plot_residuals(result) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /api/gallery/statistics/regression/images/sphx_glr_plot_01_ols_diagnostics_001.png :alt: Linear regression fit :srcset: /api/gallery/statistics/regression/images/sphx_glr_plot_01_ols_diagnostics_001.png :class: sphx-glr-multi-img * .. image-sg:: /api/gallery/statistics/regression/images/sphx_glr_plot_01_ols_diagnostics_002.png :alt: Residuals vs. fitted values :srcset: /api/gallery/statistics/regression/images/sphx_glr_plot_01_ols_diagnostics_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.045 seconds) .. _sphx_glr_download_api_gallery_statistics_regression_plot_01_ols_diagnostics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_ols_diagnostics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_ols_diagnostics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_ols_diagnostics.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_