.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "packages/statistics/auto_examples/plot_regression.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_packages_statistics_auto_examples_plot_regression.py: Simple Regression ==================== Fit a simple linear regression using 'statsmodels', compute corresponding p-values. .. GENERATED FROM PYTHON SOURCE LINES 8-21 .. code-block:: Python # Original author: Thomas Haslwanter import numpy as np import matplotlib.pyplot as plt import pandas # For statistics. Requires statsmodels 5.0 or more from statsmodels.formula.api import ols # Analysis of Variance (ANOVA) on linear models from statsmodels.stats.anova import anova_lm .. GENERATED FROM PYTHON SOURCE LINES 22-23 Generate and show the data .. GENERATED FROM PYTHON SOURCE LINES 23-34 .. code-block:: Python x = np.linspace(-5, 5, 20) # To get reproducible values, provide a seed value rng = np.random.default_rng(27446968) y = -5 + 3 * x + 4 * np.random.normal(size=x.shape) # Plot the data plt.figure(figsize=(5, 4)) plt.plot(x, y, "o") .. image-sg:: /packages/statistics/auto_examples/images/sphx_glr_plot_regression_001.png :alt: plot regression :srcset: /packages/statistics/auto_examples/images/sphx_glr_plot_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 35-37 Multilinear regression model, calculating fit, P-values, confidence intervals etc. .. GENERATED FROM PYTHON SOURCE LINES 37-54 .. code-block:: Python # Convert the data into a Pandas DataFrame to use the formulas framework # in statsmodels data = pandas.DataFrame({"x": x, "y": y}) # Fit the model model = ols("y ~ x", data).fit() # Print the summary print(model.summary()) # Perform analysis of variance on fitted linear model anova_results = anova_lm(model) print("\nANOVA results") print(anova_results) .. rst-class:: sphx-glr-script-out .. code-block:: none OLS Regression Results ============================================================================== Dep. Variable: y R-squared: 0.845 Model: OLS Adj. R-squared: 0.836 Method: Least Squares F-statistic: 97.76 Date: Mon, 17 Nov 2025 Prob (F-statistic): 1.06e-08 Time: 00:19:37 Log-Likelihood: -53.560 No. Observations: 20 AIC: 111.1 Df Residuals: 18 BIC: 113.1 Df Model: 1 Covariance Type: nonrobust ============================================================================== coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------ Intercept -4.1877 0.830 -5.044 0.000 -5.932 -2.444 x 2.7046 0.274 9.887 0.000 2.130 3.279 ============================================================================== Omnibus: 1.871 Durbin-Watson: 1.930 Prob(Omnibus): 0.392 Jarque-Bera (JB): 0.597 Skew: 0.337 Prob(JB): 0.742 Kurtosis: 3.512 Cond. No. 3.03 ============================================================================== Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. ANOVA results df sum_sq mean_sq F PR(>F) x 1.0 1347.476043 1347.476043 97.760281 1.062847e-08 Residual 18.0 248.102486 13.783471 NaN NaN .. GENERATED FROM PYTHON SOURCE LINES 55-56 Plot the fitted model .. GENERATED FROM PYTHON SOURCE LINES 56-64 .. code-block:: Python # Retrieve the parameter estimates offset, coef = model._results.params plt.plot(x, x * coef + offset) plt.xlabel("x") plt.ylabel("y") plt.show() .. image-sg:: /packages/statistics/auto_examples/images/sphx_glr_plot_regression_002.png :alt: plot regression :srcset: /packages/statistics/auto_examples/images/sphx_glr_plot_regression_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.080 seconds) .. _sphx_glr_download_packages_statistics_auto_examples_plot_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_