stats::linReg -- linear
regression (least squares fit)
Introductionstats::linReg(data) returns the least
squares estimators [a, b] of a linear relation y = a +
b*x between data pairs.
Call(s)stats::linReg([x1, x2, ..], [y1, y2, ..])
stats::linReg([[x1, y1], [x2, y2], ..])
stats::linReg(s <, cx, cy>)
stats::linReg(s <, [cx, cy]>)
Parametersx1, x2, .. |
- | statistical data: arithmetical expressions. |
y1, y2, .. |
- | statistical data: arithmetical expressions. |
s |
- | a sample of domain type stats::sample. |
cx, cy |
- | integers representing column indices of the sample
s. Column cx provides the data x1, x2,
.., column cy provides the data y1, y2,
... |
Returnsa list [a, b] of arithmetical expressions representing
the offset and the slope of the linear relation. FAIL is
returned, if these estimators do not exist.
Related
Functions
Detailscx, cy are optional,
if the data are given by a stats::sample object containing only
two non-string columns. Cf. example 2.stats::reg.
Example
1We 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:
>> stats::linReg([0, 1, 2, 3], [1, 3, 5, 7])
[1, 2]
Alternatively, the data may be specified by a list of pairs:
>> stats::linReg([[0, 0], [1, 3.3], [2, 4.8], [3, 6.9]])
[0.42, 2.22]
Example
2We 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(%)
[0, 3/2]
Example
3We 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)
[-10, 10]
Example
4We 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]), normal)
-- 2 --
| 13 y - 68 x - 5 x y + 24 x - 28 2 x y - 5 y - 24 x + 84 |
| --------------------------------, ----------------------- |
| 2 2 |
-- 2 x - 10 x + 14 2 x - 10 x + 14 --
stats::sample.