# 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 $(X_i, Y_i).

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

```
In [1]:
```

```
from __future__ import print_function
import openturns as ot
```

```
In [2]:
```

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

```
In [3]:
```

```
# Generate a particular scalar sampleX
particularXSample = ot.Triangular(1.0, 5.0, 10.0).getSample(N)
```

```
In [4]:
```

```
# Create the linear model from Y,X samples
linearRegressionModel = ot.LinearModelFactory().build(Xsample, Ysample, 0.10)
# Get the coefficients ai
print("coefficients of the linear regression model = ", linearRegressionModel.getRegression())
# Get the confidence intervals of the ai coefficients
print("confidence intervals of the coefficients = ", linearRegressionModel.getConfidenceIntervals())
```

```
coefficients of the linear regression model = [0.592409,2.98159]
confidence intervals of the coefficients = [0.714594, 0.470224]
[3.00321, 2.95996]
```

```
In [5]:
```

```
# Validate the model with a visual test
ot.VisualTest.DrawLinearModel(Xsample, Ysample, linearRegressionModel)
```

```
Out[5]:
```

```
In [6]:
```

```
# Draw the graph of the residual values
ot.VisualTest.DrawLinearModelResidual(Xsample, Ysample, linearRegressionModel)
```

```
Out[6]:
```

```
In [7]:
```

```
# Check the nullity of the regression linear model coefficients
resultLinearModelFisher = ot.LinearModelTest.LinearModelFisher(Xsample, Ysample,
linearRegressionModel, 0.10)
print("Test Success ? ", resultLinearModelFisher.getBinaryQualityMeasure())
print("p-value of the LinearModelFisher Test = ", resultLinearModelFisher.getPValue())
print("p-value threshold = ", resultLinearModelFisher.getThreshold())
```

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

```
In [8]:
```

```
# Check, under the hypothesis of a gaussian sample, if the mean of the residual is equal to zero
resultLinearModelResidualMean = ot.LinearModelTest.LinearModelResidualMean(Xsample, Ysample,
linearRegressionModel, 0.10)
print("Test Success ? ", resultLinearModelResidualMean.getBinaryQualityMeasure())
print("p-value of the LinearModelResidualMean Test = ", resultLinearModelResidualMean.getPValue())
print("p-value threshold = ", resultLinearModelResidualMean.getThreshold())
```

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