2020-06-18 08:24:31 +00:00
|
|
|
---
|
2022-08-28 14:53:34 +00:00
|
|
|
slug: /en/sql-reference/aggregate-functions/reference/simplelinearregression
|
2024-06-24 11:52:30 +00:00
|
|
|
sidebar_position: 183
|
2020-06-18 08:24:31 +00:00
|
|
|
---
|
|
|
|
|
2022-06-02 10:55:18 +00:00
|
|
|
# simpleLinearRegression
|
2020-06-18 08:24:31 +00:00
|
|
|
|
|
|
|
Performs simple (unidimensional) linear regression.
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
simpleLinearRegression(x, y)
|
|
|
|
```
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2024-02-21 16:29:47 +00:00
|
|
|
- `x` — Column with explanatory variable values.
|
|
|
|
- `y` — Column with dependent variable values.
|
2020-06-18 08:24:31 +00:00
|
|
|
|
|
|
|
Returned values:
|
|
|
|
|
docs: use `k` as constant name instead of `a` in `simpleLinearRegression`
Function uses `k` internally.
E.g.
```sql
SELECT arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [0, 1, 2, 3]) FORMAT JsonEachRow
```
outputs `{"arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [0, 1, 2, 3])":{"k":1,"b":0}}`
2024-10-21 13:34:15 +00:00
|
|
|
Constants `(k, b)` of the resulting line `y = k*x + b`.
|
2020-06-18 08:24:31 +00:00
|
|
|
|
|
|
|
**Examples**
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [0, 1, 2, 3])
|
|
|
|
```
|
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [0, 1, 2, 3])─┐
|
|
|
|
│ (1,0) │
|
|
|
|
└───────────────────────────────────────────────────────────────────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [3, 4, 5, 6])
|
|
|
|
```
|
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─arrayReduce('simpleLinearRegression', [0, 1, 2, 3], [3, 4, 5, 6])─┐
|
|
|
|
│ (1,3) │
|
|
|
|
└───────────────────────────────────────────────────────────────────┘
|
|
|
|
```
|