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
Returns the average of x over the rows in the group where neither y nor x is NULL. Unlike avg(, it also leaves out the rows where only x)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.
Syntax
regr_avgx(y, x)
Arguments
The regr_avgx function takes the following arguments:
y-
The dependent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE. x-
The independent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
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.
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 |
|---|---|
|
|
The following SQL query returns the average of x over four points.
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 |
|---|
|
The following SQL query has no row where both values are present, so the result is NULL.
SELECT regr_avgx(y, x) AS result FROM (VALUES (1, NULL), (NULL, 2)) AS v(x, y)
The query returns the following result.
| result |
|---|
|
regr_avgy function
Returns the average of y over the rows in the group where neither y nor x is NULL. Unlike avg(, it also leaves out the rows where only y)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.
Syntax
regr_avgy(y, x)
Arguments
The regr_avgy function takes the following arguments:
y-
The dependent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE. x-
The independent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
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.
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 |
|---|---|
|
|
The following SQL query returns the average of y over four points.
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 |
|---|
|
regr_count function
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
regr_count(y, x)
Arguments
The regr_count function takes the following arguments:
y-
The dependent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE. x-
The independent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
UINTEGER
Examples
The following SQL query compares regr_count with count. Only two of the four rows have both values.
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 |
|---|---|---|---|
|
|
|
|
The following SQL query uses WHERE to filter out every row, so the result is 0.
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 |
|---|
|
The following SQL query casts the result to BIGINT before it subtracts a larger number.
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 |
|---|
|
Considerations
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( when the count is y, x) - 52, fails with an overflow error. Cast the result to BIGINT first.
regr_intercept function
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.
Syntax
regr_intercept(y, x)
Arguments
The regr_intercept function takes the following arguments:
y-
The dependent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE. x-
The independent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query uses three points on the line y = 2x + 1, so the intercept is 1.0.
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 |
|---|
|
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.
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 |
|---|
|
The following SQL query has the same x value in every row, so no line can be fitted and the result is NULL.
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 |
|---|
|
Differences from Athena engine version 3
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
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
regr_r2(y, x)
Arguments
The regr_r2 function takes the following arguments:
y-
The dependent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE. x-
The independent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the coefficient of determination for four points, rounded to four decimal places.
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 |
|---|
|
The following SQL query uses three points that lie on one line, so the result is 1.0.
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 |
|---|
|
The following SQL query has the same x value in every row, so the result is NULL.
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 |
|---|
|
regr_slope function
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.
Syntax
regr_slope(y, x)
Arguments
The regr_slope function takes the following arguments:
y-
The dependent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE. x-
The independent variable, as a
DOUBLEvalue. Other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the slope of the line fitted to four points.
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 |
|---|
|
The following SQL query returns the slope and the intercept of the line y = 2x + 1, which passes through all three points.
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 |
|---|---|
|
|
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.
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 |
|---|---|
|
|
The following SQL query has the same x value in every row, so the slope is NaN. nullif turns the NaN into NULL.
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 |
|---|---|
|
|
Considerations
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
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
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
regr_sxx(y, x)
Arguments
The regr_sxx function takes the following arguments:
y-
The dependent variable, a
DOUBLEvalue. Only whether it isNULLaffects the result. x-
The independent variable, a
DOUBLEvalue.
Return type
DOUBLE
Examples
The following SQL query returns the sum of the squared deviations of x for four pairs of values.
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 |
|---|
|
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.
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 |
|---|---|
|
|
Considerations
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
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
regr_sxy(y, x)
Arguments
The regr_sxy function takes the following arguments:
y-
The dependent variable, a
DOUBLEvalue. x-
The independent variable, a
DOUBLEvalue.
Return type
DOUBLE
Examples
The following SQL query returns the sum of the products of the deviations for four pairs of values.
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 |
|---|
|
The following SQL query shows that the result equals the number of pairs multiplied by the population covariance.
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 |
|---|---|
|
|
regr_syy function
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
regr_syy(y, x)
Arguments
The regr_syy function takes the following arguments:
y-
The dependent variable, a
DOUBLEvalue. x-
The independent variable, a
DOUBLEvalue. Only whether it isNULLaffects the result.
Return type
DOUBLE
Examples
The following SQL query returns the sum of the squared deviations of y for four pairs of values.
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 |
|---|
|
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.
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 |
|---|---|
|
|