

# Linear regression functions

Use these functions to fit a least-squares line to the (`x`, `y`) pairs of a group and get its slope, intercept, or coefficient of determination. Other functions return the row count, averages, and sums of squares and products that the fit uses, over the rows where neither argument is `NULL`.

## `regr_avgx` function
<a name="athena-duckdb-functions-aggregate-regr-avgx"></a>

Returns the average of `x` over the rows in the group where neither `y` nor `x` is `NULL`. Unlike `avg({{x}})`, it also leaves out the rows where only `y` is `NULL`, so it uses the same rows as the other regression functions, such as `regr_slope`. If no row has both values, the result is `NULL`.

As in the other `regr_` functions, the dependent variable `y` is the first argument and the independent variable `x` is the second. To average `y` instead, use the [`regr_avgy` function](#athena-duckdb-functions-aggregate-regr-avgy).

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-avgx-syntax"></a>

```sql
regr_avgx({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-avgx-arguments"></a>

The `regr_avgx` function takes the following arguments:

**{{y}}**  
The dependent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-avgx-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-avgx-examples"></a>

The following SQL query compares `regr_avgx` with `avg`. The row where `y` is `NULL` is left out of `regr_avgx`, so it averages only `1` and `2`.

```sql
SELECT
    regr_avgx(y, x) AS regr_avgx_result,
    avg(x) AS avg_result
FROM (VALUES (1, 10), (2, 20), (6, NULL)) AS v(x, y)
```

The query returns the following result.


| regr\_avgx\_result | avg\_result | 
| --- | --- | 
| `1.5` | `3.0` | 

The following SQL query returns the average of `x` over four points.

```sql
SELECT regr_avgx(y, x) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `2.5` | 

The following SQL query has no row where both values are present, so the result is `NULL`.

```sql
SELECT regr_avgx(y, x) AS result
FROM (VALUES (1, NULL), (NULL, 2)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

## `regr_avgy` function
<a name="athena-duckdb-functions-aggregate-regr-avgy"></a>

Returns the average of `y` over the rows in the group where neither `y` nor `x` is `NULL`. Unlike `avg({{y}})`, it also leaves out the rows where only `x` is `NULL`, so it uses the same rows as the other regression functions, such as `regr_slope`. If no row has both values, the result is `NULL`.

As in the other `regr_` functions, the dependent variable `y` is the first argument and the independent variable `x` is the second. To average `x` instead, use the [`regr_avgx` function](#athena-duckdb-functions-aggregate-regr-avgx).

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-avgy-syntax"></a>

```sql
regr_avgy({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-avgy-arguments"></a>

The `regr_avgy` function takes the following arguments:

**{{y}}**  
The dependent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-avgy-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-avgy-examples"></a>

The following SQL query compares `regr_avgy` with `avg`. The row where `x` is `NULL` is left out of `regr_avgy`, so it averages only `10` and `20`.

```sql
SELECT
    regr_avgy(y, x) AS regr_avgy_result,
    avg(y) AS avg_result
FROM (VALUES (1, 10), (2, 20), (NULL, 60)) AS v(x, y)
```

The query returns the following result.


| regr\_avgy\_result | avg\_result | 
| --- | --- | 
| `15.0` | `30.0` | 

The following SQL query returns the average of `y` over four points.

```sql
SELECT regr_avgy(y, x) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `5.25` | 

## `regr_count` function
<a name="athena-duckdb-functions-aggregate-regr-count"></a>

Returns the number of rows in the group where neither `y` nor `x` is `NULL`. These are the rows that the other regression functions, such as `regr_slope`, use. If the group has no such rows, the result is `0`, not `NULL`.

As in the other `regr_` functions, the dependent variable `y` is the first argument and the independent variable `x` is the second. For `regr_count`, the order does not change the result.

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-count-syntax"></a>

```sql
regr_count({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-count-arguments"></a>

The `regr_count` function takes the following arguments:

**{{y}}**  
The dependent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-count-return-type"></a>

`UINTEGER`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-count-examples"></a>

The following SQL query compares `regr_count` with `count`. Only two of the four rows have both values.

```sql
SELECT
    regr_count(y, x) AS pairs,
    count(*) AS all_rows,
    count(x) AS x_rows,
    count(y) AS y_rows
FROM (VALUES
    (1, 10),
    (2, NULL),
    (NULL, 30),
    (4, 40)
) AS v(x, y)
```

The query returns the following result.


| pairs | all\_rows | x\_rows | y\_rows | 
| --- | --- | --- | --- | 
| `2` | `4` | `3` | `3` | 

The following SQL query uses `WHERE` to filter out every row, so the result is `0`.

```sql
SELECT regr_count(y, x) AS result
FROM (VALUES (1, 10), (2, 20)) AS v(x, y)
WHERE x > 5
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

The following SQL query casts the result to `BIGINT` before it subtracts a larger number.

```sql
SELECT CAST(regr_count(y, x) AS BIGINT) - 5 AS result
FROM (VALUES (1, 10), (2, 20)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `-3` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-regr-count-considerations"></a>

The result is a `UINTEGER`, an unsigned type, not a `BIGINT` like the result of `count`. Arithmetic that would make it negative, such as `regr_count({{y}}, {{x}}) - 5` when the count is `2`, fails with an overflow error. Cast the result to `BIGINT` first.

## `regr_intercept` function
<a name="athena-duckdb-functions-aggregate-regr-intercept"></a>

Fits the straight line `y = slope * x + intercept` to the (`x`, `y`) pairs in the group by least squares, and returns the intercept. The intercept is the value of `y` where the line crosses `x = 0`. Rows where `y` or `x` is `NULL` are ignored.

If no row has both values, or if every `x` value is the same, including when there is only one row, the result is `NULL`. The dependent variable `y` is the first argument. To get the slope of the same line, use the [`regr_slope` function](#athena-duckdb-functions-aggregate-regr-slope).

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-intercept-syntax"></a>

```sql
regr_intercept({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-intercept-arguments"></a>

The `regr_intercept` function takes the following arguments:

**{{y}}**  
The dependent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-intercept-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-intercept-examples"></a>

The following SQL query uses three points on the line `y = 2x + 1`, so the intercept is `1.0`.

```sql
SELECT regr_intercept(y, x) AS result
FROM (VALUES (1, 3), (2, 5), (3, 7)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `1.0` | 

The following SQL query fits a line to four points and rounds the intercept to four decimal places. Without `round`, the `DOUBLE` result shows a small floating-point error.

```sql
SELECT round(regr_intercept(y, x), 4) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `-0.5` | 

The following SQL query has the same `x` value in every row, so no line can be fitted and the result is `NULL`.

```sql
SELECT regr_intercept(y, x) IS NULL AS is_null
FROM (VALUES (2, 1), (2, 3), (2, 5)) AS v(x, y)
```

The query returns the following result.


| is\_null | 
| --- | 
| `true` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-regr-intercept-differences-from-engine-version-3"></a>

In Athena engine version 3, `regr_intercept` with integer arguments returns a `REAL` value, which holds about 7 significant digits. In DuckDB, it returns a `DOUBLE`, so results can differ after about the seventh digit.

## `regr_r2` function
<a name="athena-duckdb-functions-aggregate-regr-r2"></a>

Returns the coefficient of determination of the least-squares line fitted to the (`x`, `y`) pairs in the group. The result is a value from `0` to `1` that shows how much of the variation in `y` the line explains. A result of `1` means that every point lies on the line. Rows where `y` or `x` is `NULL` are ignored.

If no row has both values, or if every `x` value is the same, including when there is only one row, the result is `NULL`. If the `x` values vary but every `y` value is the same, the result is `1`. The dependent variable `y` is the first argument.

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-r2-syntax"></a>

```sql
regr_r2({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-r2-arguments"></a>

The `regr_r2` function takes the following arguments:

**{{y}}**  
The dependent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-r2-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-r2-examples"></a>

The following SQL query returns the coefficient of determination for four points, rounded to four decimal places.

```sql
SELECT round(regr_r2(y, x), 4) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `0.9706` | 

The following SQL query uses three points that lie on one line, so the result is `1.0`.

```sql
SELECT regr_r2(y, x) AS result
FROM (VALUES (1, 3), (2, 5), (3, 7)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `1.0` | 

The following SQL query has the same `x` value in every row, so the result is `NULL`.

```sql
SELECT regr_r2(y, x) AS result
FROM (VALUES (2, 1), (2, 3), (2, 5)) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

## `regr_slope` function
<a name="athena-duckdb-functions-aggregate-regr-slope"></a>

Fits the straight line `y = slope * x + intercept` to the (`x`, `y`) pairs in the group by least squares, and returns the slope. The slope is how much `y` changes, on average, when `x` increases by `1`. Rows where `y` or `x` is `NULL` are ignored. If no row has both values, the result is `NULL`.

If every `x` value is the same, including when there is only one row, the result is `NaN`, not `NULL`. The dependent variable `y` is the first argument, and swapping the arguments returns a different slope. To get the intercept of the same line, use the [`regr_intercept` function](#athena-duckdb-functions-aggregate-regr-intercept).

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-slope-syntax"></a>

```sql
regr_slope({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-slope-arguments"></a>

The `regr_slope` function takes the following arguments:

**{{y}}**  
The dependent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

**{{x}}**  
The independent variable, as a `DOUBLE` value. Other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-slope-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-slope-examples"></a>

The following SQL query returns the slope of the line fitted to four points.

```sql
SELECT regr_slope(y, x) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS v(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `2.3` | 

The following SQL query returns the slope and the intercept of the line `y = 2x + 1`, which passes through all three points.

```sql
SELECT
    regr_slope(y, x) AS slope,
    regr_intercept(y, x) AS intercept
FROM (VALUES (1, 3), (2, 5), (3, 7)) AS v(x, y)
```

The query returns the following result.


| slope | intercept | 
| --- | --- | 
| `2.0` | `1.0` | 

The following SQL query shows that the argument order matters. The slope of `y` on `x` is `2.0`, and the slope of `x` on `y` is `0.5`.

```sql
SELECT
    regr_slope(y, x) AS slope_of_y_on_x,
    regr_slope(x, y) AS slope_of_x_on_y
FROM (VALUES (1, 3), (2, 5), (3, 7)) AS v(x, y)
```

The query returns the following result.


| slope\_of\_y\_on\_x | slope\_of\_x\_on\_y | 
| --- | --- | 
| `2.0` | `0.5` | 

The following SQL query has the same `x` value in every row, so the slope is `NaN`. `nullif` turns the `NaN` into `NULL`.

```sql
SELECT
    isnan(regr_slope(y, x)) AS slope_is_nan,
    nullif(
        regr_slope(y, x), CAST('NaN' AS DOUBLE)
    ) AS slope_or_null
FROM (VALUES (2, 1), (2, 3), (2, 5)) AS v(x, y)
```

The query returns the following result.


| slope\_is\_nan | slope\_or\_null | 
| --- | --- | 
| `true` | `NULL` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-regr-slope-considerations"></a>

To get `NULL` instead of `NaN` when every `x` value is the same, use `nullif(regr_slope({{y}}, {{x}}), CAST('NaN' AS DOUBLE))`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-aggregate-regr-slope-differences-from-engine-version-3"></a>

In Athena engine version 3, `regr_slope` with integer arguments returns a `REAL` value, which holds about 7 significant digits. In DuckDB, it returns a `DOUBLE`, so results can differ after about the seventh digit.

In Athena engine version 3, `regr_slope` returns `NULL` when every `x` value is the same. In DuckDB, it returns `NaN`. To get the Athena engine version 3 result, use `nullif(regr_slope({{y}}, {{x}}), CAST('NaN' AS DOUBLE))`.

## `regr_sxx` function
<a name="athena-duckdb-functions-aggregate-regr-sxx"></a>

Returns the sum of the squared differences between each `x` value and the mean of `x`. The function uses only the rows where neither `y` nor `x` is `NULL`. A row whose `y` is `NULL` is left out, even though the result does not otherwise depend on `y`. The result equals `regr_count(y, x) * var_pop(x)`, with `var_pop` computed over those same rows.

If no row has both values, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-sxx-syntax"></a>

```sql
regr_sxx({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-sxx-arguments"></a>

The `regr_sxx` function takes the following arguments:

**{{y}}**  
The dependent variable, a `DOUBLE` value. Only whether it is `NULL` affects the result.

**{{x}}**  
The independent variable, a `DOUBLE` value.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-sxx-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-sxx-examples"></a>

The following SQL query returns the sum of the squared deviations of `x` for four pairs of values.

```sql
SELECT regr_sxx(y, x) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS t(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `5.0` | 

The following SQL query has a `NULL` in `y` in the last row, so the function uses only the first three rows. `regr_count` shows the number of rows used.

```sql
SELECT regr_sxx(y, x) AS sxx, regr_count(y, x) AS pairs
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, NULL)
) AS t(x, y)
```

The query returns the following result.


| sxx | pairs | 
| --- | --- | 
| `2.0` | `3` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-regr-sxx-considerations"></a>

The dependent variable `y` comes first, as in the other `regr_` functions. If you swap the arguments, you get the value of `regr_syy` instead.

## `regr_sxy` function
<a name="athena-duckdb-functions-aggregate-regr-sxy"></a>

Returns the sum of `(x - avg(x)) * (y - avg(y))` over the rows where neither `y` nor `x` is `NULL`, with both means computed over those same rows. The result equals `regr_count(y, x) * covar_pop(y, x)`.

A positive result means that `y` tends to rise with `x`, and a negative result means that it tends to fall. If no row has both values, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-sxy-syntax"></a>

```sql
regr_sxy({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-sxy-arguments"></a>

The `regr_sxy` function takes the following arguments:

**{{y}}**  
The dependent variable, a `DOUBLE` value.

**{{x}}**  
The independent variable, a `DOUBLE` value.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-sxy-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-sxy-examples"></a>

The following SQL query returns the sum of the products of the deviations for four pairs of values.

```sql
SELECT regr_sxy(y, x) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS t(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `11.5` | 

The following SQL query shows that the result equals the number of pairs multiplied by the population covariance.

```sql
SELECT
    regr_sxy(y, x) AS sxy,
    regr_count(y, x) * covar_pop(y, x) AS same_value
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS t(x, y)
```

The query returns the following result.


| sxy | same\_value | 
| --- | --- | 
| `11.5` | `11.5` | 

## `regr_syy` function
<a name="athena-duckdb-functions-aggregate-regr-syy"></a>

Returns the sum of the squared differences between each `y` value and the mean of `y`. The function uses only the rows where neither `y` nor `x` is `NULL`. A row whose `x` is `NULL` is left out, even though the result does not otherwise depend on `x`. The result equals `regr_count(y, x) * var_pop(y)`, with `var_pop` computed over those same rows.

If no row has both values, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-aggregate-regr-syy-syntax"></a>

```sql
regr_syy({{y}}, {{x}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-regr-syy-arguments"></a>

The `regr_syy` function takes the following arguments:

**{{y}}**  
The dependent variable, a `DOUBLE` value.

**{{x}}**  
The independent variable, a `DOUBLE` value. Only whether it is `NULL` affects the result.

### Return type
<a name="athena-duckdb-functions-aggregate-regr-syy-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-aggregate-regr-syy-examples"></a>

The following SQL query returns the sum of the squared deviations of `y` for four pairs of values.

```sql
SELECT regr_syy(y, x) AS result
FROM (VALUES
    (1.0, 2.0),
    (2.0, 3.5),
    (3.0, 7.0),
    (4.0, 8.5)
) AS t(x, y)
```

The query returns the following result.


| result | 
| --- | 
| `27.25` | 

The following SQL query has a `NULL` in `x` in the third row, so the function uses only the `y` values `2.0`, `4.0`, and `9.0`.

```sql
SELECT regr_syy(y, x) AS syy, regr_count(y, x) AS pairs
FROM (VALUES
    (1.0, 2.0),
    (2.0, 4.0),
    (NULL, 7.0),
    (4.0, 9.0)
) AS t(x, y)
```

The query returns the following result.


| syy | pairs | 
| --- | --- | 
| `26.0` | `3` | 