# Build and validate a linear model¶

In this example we are going to build a linear regression model and validate it numerically and graphically.

The linear model between links a scalar variable and to an n-dimensional one , as follows: where is the residual, supposed to follow the Normal(0.0, 1.0) distribution.

The linear model may be validated graphically if is of dimension 1, by drawing on the same graph the cloud .

The linear model also be validate 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 is of dimension 1, by drawing the residual couples ( ), where the residual is evaluated on the samples .

• 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).

from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)


Generate X,Y samples

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)


Generate a particular scalar sampleX

particularXSample = ot.Triangular(1.0, 5.0, 10.0).getSample(N)


Create the linear model from Y,X samples

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))


Out:

coefficients of the linear regression model =  [0.620986,2.98488]
confidence intervals of the coefficients =  [0.464408, 0.777565]
[2.95727, 3.0125]


Validate the model with a visual test

graph = ot.VisualTest.DrawLinearModel(Xsample, Ysample, result)
view = viewer.View(graph) Draw the graph of the residual values

graph = ot.VisualTest.DrawLinearModelResidual(Xsample, Ysample, result)
view = viewer.View(graph) Check the nullity of the regression linear model coefficients

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())


Out:

Test Success ?  False
p-value of the LinearModelFisher Test =  0.0
p-value threshold =  0.1


Check, under the hypothesis of a gaussian sample, if the mean of the residual is equal to zero

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()


Out:

Test Success ?  True
p-value of the LinearModelResidualMean Test =  0.9999999999998087
p-value threshold =  0.1


Total running time of the script: ( 0 minutes 0.162 seconds)

Gallery generated by Sphinx-Gallery
{"ft_lang_label":"__label__en","ft_lang_prob":0.75554407,"math_prob":0.99468756,"size":3607,"snap":"2021-31-2021-39","text_gpt3_token_len":884,"char_repetition_ratio":0.16985846,"word_repetition_ratio":0.22125813,"special_character_ratio":0.2306626,"punctuation_ratio":0.18995291,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99920577,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],"im_url_duplicate_count":[null,null,null,2,null,2,null,7,null,null,null,2,null,null,null,2,null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-19T08:22:26Z\",\"WARC-Record-ID\":\"<urn:uuid:944f1c6f-8e82-4514-b224-cd4f1b71af8b>\",\"Content-Length\":\"21703\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e9d931a9-97f7-4a2f-901a-7a90555e7d30>\",\"WARC-Concurrent-To\":\"<urn:uuid:4bac81e9-e8aa-44ec-81a6-64070131a88f>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://openturns.github.io/openturns/1.16/auto_data_analysis/manage_data_and_samples/plot_linear_regression.html\",\"WARC-Payload-Digest\":\"sha1:7HWGRYDEW3NWT6HXZOCL5AVCIDU6JLJF\",\"WARC-Block-Digest\":\"sha1:KBPFBR5DJOFWWBB4DHZ2OX2Y73TLSQKD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056752.16_warc_CC-MAIN-20210919065755-20210919095755-00063.warc.gz\"}"}