.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/manage_data_and_samples/plot_linear_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_auto_data_analysis_manage_data_and_samples_plot_linear_regression.py: Build and validate a linear model ================================= .. GENERATED FROM PYTHON SOURCE LINES 7-32 In this example we are going to build a linear regression model and validate it numerically and graphically. The linear model links a scalar variable :math:`Y` and to an n-dimensional one :math:`\underline{X} = (X_i)_{i \leq n}`, as follows: .. math:: \tilde{Y} = a_0 + \sum_{i=1}^n a_i X_i + \varepsilon where :math:`\varepsilon` is the residual, supposed to follow :math:`\mathcal{N}(0.0, 1.0)`. The linear model may be validated graphically if :math:`\underline{X}` is of dimension 1, by drawing on the same graph the cloud :math:`(X_i, Y_i)`. The linear model can also be validated numerically with several tests: - `LinearModelFisher`: tests the nullity of the regression linear model coefficients (Fisher distribution used), - `LinearModelResidualMean`: tests, under the hypothesis of a Gaussian sample, if the mean of the residual is equal to zero. It is based on the Student test (equality of mean for two Gaussian samples). The hypothesis on the residuals (centered Gaussian distribution) may be validated: - graphically if :math:`\underline{X}` is of dimension 1, by drawing the residual couples (:math:`\varepsilon_i, \varepsilon_{i+1}`), where the residual :math:`\varepsilon_i` is evaluated on the samples :math:`(X, Y)`. - numerically with the `LinearModelResidualMean` test which tests, under the hypothesis of a Gaussian sample, if the mean of the residual is equal to zero. It is based on the Student test (equality of mean for two Gaussian samples). .. GENERATED FROM PYTHON SOURCE LINES 34-40 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 41-42 Generate `X, Y` samples .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: Python N = 1000 Xsample = ot.Triangular(1.0, 5.0, 10.0).getSample(N) Ysample = Xsample * 3.0 + ot.Normal(0.5, 1.0).getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Generate a particular scalar sampleX .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python particularXSample = ot.Triangular(1.0, 5.0, 10.0).getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 51-52 Create the linear model from `Y, X` samples .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python result = ot.LinearModelAlgorithm(Xsample, Ysample).getResult() # Get the coefficients ai print("coefficients of the linear regression model = ", result.getCoefficients()) # Get the confidence intervals of the `ai` coefficients print( "confidence intervals of the coefficients = ", ot.LinearModelAnalysis(result).getCoefficientsConfidenceInterval(0.9), ) .. rst-class:: sphx-glr-script-out .. code-block:: none coefficients of the linear regression model = [0.553713,2.9942] confidence intervals of the coefficients = [0.391587, 0.715838] [2.96562, 3.02278] .. GENERATED FROM PYTHON SOURCE LINES 65-66 Validate the model with a visual test .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python graph = ot.VisualTest.DrawLinearModel(Xsample, Ysample, result) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_linear_regression_001.png :alt: Linear model visual test :srcset: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_linear_regression_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-71 Draw the graph of the residual values .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python graph = ot.VisualTest.DrawLinearModelResidual(Xsample, Ysample, result) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_linear_regression_002.png :alt: residual(i) versus residual(i-1) :srcset: /auto_data_analysis/manage_data_and_samples/images/sphx_glr_plot_linear_regression_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-76 Check the nullity of the regression linear model coefficients .. GENERATED FROM PYTHON SOURCE LINES 76-83 .. code-block:: Python resultLinearModelFisher = ot.LinearModelTest.LinearModelFisher( Xsample, Ysample, result, 0.10 ) print("Test Success ? ", resultLinearModelFisher.getBinaryQualityMeasure()) print("p-value of the LinearModelFisher Test = ", resultLinearModelFisher.getPValue()) print("p-value threshold = ", resultLinearModelFisher.getThreshold()) .. rst-class:: sphx-glr-script-out .. code-block:: none Test Success ? False p-value of the LinearModelFisher Test = 0.0 p-value threshold = 0.1 .. GENERATED FROM PYTHON SOURCE LINES 84-85 Check, under the hypothesis of a Gaussian sample, if the mean of the residuals is equal to zero .. GENERATED FROM PYTHON SOURCE LINES 85-95 .. code-block:: Python resultLinearModelResidualMean = ot.LinearModelTest.LinearModelResidualMean( Xsample, Ysample, result, 0.10 ) print("Test Success ? ", resultLinearModelResidualMean.getBinaryQualityMeasure()) print( "p-value of the LinearModelResidualMean Test = ", resultLinearModelResidualMean.getPValue(), ) print("p-value threshold = ", resultLinearModelResidualMean.getThreshold()) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none Test Success ? True p-value of the LinearModelResidualMean Test = 0.9999999999998205 p-value threshold = 0.1 .. _sphx_glr_download_auto_data_analysis_manage_data_and_samples_plot_linear_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_linear_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_linear_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_linear_regression.zip `