Categories:

Aggregate Functions (Linear Regression) , Window Functions

REGR_R2

Returns the coefficient of determination for non-null pairs in a group. It is computed for non-null pairs using the following formula:

NULL                 if VAR_POP(x) = 0, else
1                    if VAR_POP(y) = 0 and VAR_POP(x) <> 0, else
POWER(CORR(y,x), 2)

Where x is the independent variable and y is the dependent variable.

Syntax

REGR_R2(y, x)

Usage Notes

  • DISTINCT is not supported for this function.

Examples

create or replace table aggr(k int, v decimal(10,2), v2 decimal(10, 2));
insert into aggr values(1, 10, null);
insert into aggr values(2, 10, 11), (2, 20, 22), (2, 25,null), (2, 30, 35);
select k, regr_r2(v, v2) from aggr group by k;

---+----------------+
 k | regr_r2(v, v2) |
---+----------------+
 1 | [NULL]         |
 2 | 0.9976905312   |
---+----------------+