stats::linReg

Linear regression (least squares fit)

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

stats::linReg([x1, x2, …], [y1, y2, …], <[w1, w2, …]>, <CovarianceMatrix>)
stats::linReg([[x1, y1, <w1>], [x2, y2, <w2>], …], <CovarianceMatrix>)
stats::linReg(s, <cx, cy, <cw>>, <CovarianceMatrix>)
stats::linReg(s, <[cx, cy, <cw>]>, <CovarianceMatrix>)

Description

stats::linReg([x1, x2, …], [y1, y2, …], [w1, w2, …]) computes the least squares estimators a, b of a linear relation yi = a + bxi between the data pairs (xi, yi) by minimizing

.

A linear relation yi = a + bxi + ei between the data pairs (xi, yi) is assumed.

The column indices cx, cy are optional if the data are given by a stats::sample object containing only two non-string columns. Cf. Example 2.

Multivariate linear regression and non-linear regression is provided by stats::reg.

External statistical data stored in an ASCII file can be imported into a MuPAD® session via import::readdata. In particular, see Example 1 of the corresponding help page.

Examples

Example 1

We calculate the least square estimators of four pairs of values given in two lists. Note that there is a linear relation y = 1 + 2 x between the entries of the lists. The minimized quadratic deviation is 0 indicating a perfect fit:

stats::linReg([0, 1, 2, 3], [1, 3, 5, 7])

Alternatively, data may be specified by a list of pairs:

stats::linReg([[1, 1.0], [2, 1.2], [3, 1.3], [4, 1.5]])

We assume that the variable y in the previous example is Poissonian, i.e. that the measurements (yi) = (1.0, 1.2, 1.3, 1.5) have errors given by the standard deviation . We provide corresponding weights and estimate confidence intervals for the least squares estimators by using the option CovarianceMatrix:

stats::linReg([[1, 1.0, 1/1.0], [2, 1.2, 1/1.2], 
               [3, 1.3, 1/1.3], [4, 1.5, 1/1.5]], CovarianceMatrix)

The square roots of the diagonal elements of the covariance matrix provide standard deviations for the estimated parameters:

sqrt(%[3][1,1]), sqrt(%[3][2,2])

Thus, we obtain the estimates , .

Example 2

We create a sample consisting of one string column and two non-string columns:

stats::sample([["1", 0, 0], ["2", 10, 15], ["3", 20, 30]])
"1"   0   0
"2"  10  15
"3"  20  30

The least square estimators are calculated using the data columns 2 and 3. In this example there are only two non-string columns, so the column indices do not have to be specified:

stats::linReg(%)

Example 3

We create a sample consisting of three data columns:

stats::sample([[1, 0, 0], [2, 10, 15], [3, 20, 30]])
1   0   0
2  10  15
3  20  30

We compute the least square estimators for the data pairs given by the first and the second column:

stats::linReg(%, 1, 2)

Example 4

We create a sample of three columns containing symbolic data:

stats::sample([[x, y, 0], [2, 4, 15], [3, 20, 30]])
x   y   0
2   4  15
3  20  30

We compute the symbolic least square estimators for the data pairs given by the first and the second column. Here we specify these columns by a list of column indices:

map(stats::linReg(%, [1, 2], CovarianceMatrix), normal)

Example 5

We create data (xi, yi) with a randomized relation yi = a + bxi:

DIGITS := 5:
r := stats::normalRandom(0, 5):
X := [i $ i = 0..100]:
Y := [12 + 17*x + r() $ x in X]:

By construction, the variances σ(yi)2 for the data yi in the list Y is 5. We use the weights for all data:

W := [1/5 $ i = 0..100]:
[ab, chisquared, C]:= stats::linReg(X, Y, W, CovarianceMatrix)

The standard deviations of the estimators a, b are the square roots of the diagonal elements of C:

sqrt(float(C[1,1])), sqrt(float(C[2,2]))

Thus, the estimate for a is , the estimate for b is .

delete r, X, Y, W, ab, chisquared, C:

Parameters

x1, x2, …

Statistical data: arithmetical expressions

y1, y2, …

Statistical data: arithmetical expressions

w1, w2, …

Weights: arithmetical expressions. If no weights are provided, w1 = w2 = … = 1 is used.

s

A sample of domain type stats::sample.

cx, cy, cw

Integers representing column indices of the sample s. Column cx provides the data x1, x2, …, column cy provides the data y1, y2, …. Column cw, if present, provides the weights w1, w2, …. If no index for the weights is provided, w1 = w2 = … = 1 is used.

Options

CovarianceMatrix

Changes the return value from [[a, b], chisquared] to [[a, b], chisquared, C], where C is the covariance matrix

Of the estimators a, b.

With this option, information on confidence intervals for the least squares estimators are provided. In particular, the return value includes the covariance matrix

Of type Dom::Matrix(). Assuming that the data (yi) are randomly perturbed with stochastic variations , the quadratic error to be minimized is

With

.

The covariance matrix of the least squares estimators is given by

,

,

.

Return Values

Without the option CovarianceMatrix, a list [[a, b], chisquared] is returned. The arithmetical expressions a and b are estimators of the the offset and the slope of the linear relation. The arithmetical expression chisquared is the quadratic deviation

,

where a, b are the optimized estimators.

With the option CovarianceMatrix, a list [[a, b], chisquared, C] is returned. The matrix C is the covariance matrix of the optimized estimators a and b.

FAIL is returned if the estimators a and b do not exist.

References

P.R. Bevington and D.K. Robinson, "Data Reduction and Error Analysis for The Physical Sciences", McGraw-Hill, New York, 1992.

See Also

MuPAD Functions

MuPAD Graphical Primitives

Was this topic helpful?