Linear regression (least squares fit)
MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
stats::linReg([x_{1}, x_{2}, …]
,[y_{1}, y_{2}, …]
, <[w_{1}, w_{2}, …]
>, <CovarianceMatrix>) stats::linReg([[x_{1}, y_{1}, <w_{1}>], [x_{2}, y_{2}, <w_{2}>], …]
, <CovarianceMatrix>) stats::linReg(s
, <cx, cy, <cw>
>, <CovarianceMatrix>) stats::linReg(s
, <[cx, cy, <cw>]
>, <CovarianceMatrix>)
stats::linReg([x_{1}, x_{2},
…], [y_{1}, y_{2},
…], [w_{1}, w_{2},
…])
computes the least squares estimators a, b of
a linear relation y_{i} = a + b x_{i} between
the data pairs (x_{i}, y_{i}) by
minimizing
.
A linear relation y_{i} = a + b x_{i} + e_{i} between the data pairs (x_{i}, y_{i}) is assumed.
The column indices cx
, cy
are
optional if the data are given by a stats::sample
object
containing only two nonstring columns. Cf. Example 2.
Multivariate linear regression and nonlinear 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.
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 (y_{i}) = (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 , .
We create a sample consisting of one string column and two nonstring 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 nonstring columns, so the column indices do not have to be specified:
stats::linReg(%)
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)
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)
We create data (x_{i}, y_{i}) with a randomized relation y_{i} = a + b x_{i}:
DIGITS := 5: r := stats::normalRandom(0, 5): X := [i $ i = 0..100]: Y := [12 + 17*x + r() $ x in X]:
By construction, the variances σ(y_{i})^{2} for the data y_{i} 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:

Statistical data: arithmetical expressions 

Statistical data: arithmetical expressions 

Weights: arithmetical expressions. If no weights are provided, w_{1} = w_{2} = … = 1 is used. 

A sample of domain type 

Integers representing column indices of the sample 

Changes the return value from
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
With . The covariance matrix of the least squares estimators is given by , , . 
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.
P.R. Bevington and D.K. Robinson, "Data Reduction and Error Analysis for The Physical Sciences", McGrawHill, New York, 1992.