## `stats::linReg` -- linear regression (least squares fit)

#### Introduction

`stats::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]>)`

#### Parameters

`x1, 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, ..`.

#### Returns

a 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

`stats::reg`, `stats::sample`

#### Details

- A linear relation y[i] = a+b*x[i]+e[i] between the data pairs (x[i], y[i]) is assumed. The least squares estimators a and b are chosen such that the quadratic error sum(e[i]^2, i) is minimized.
- 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`.

#### 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:

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

                                     [0, 3/2]

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

                                     [-10, 10]

#### 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]), 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     --

#### Changes

- The input data can now be provided by a `stats::sample`.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000