REGR_R2() OVER ...

All functions > WINDOW FUNCTION > REGR_R2() OVER ...

Returns the coefficient of determination (R²) over the window frame.

Syntax

REGR_R2(expr_y, expr_x) [FILTER (WHERE condition) | WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])

Notes

  • Equivalent to POWER(CORR(y, x), 2) over the window
  • Returns a value between 0 (no fit) and 1 (perfect fit)

See also

Examples

FeatureQL
SELECT
    -- R² is 1 for a perfect linear fit over the frame
    f1 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(30, 10, 20, 5) AS v,
        ARRAY(3.0e0, 5.0e0, 7.0e0, 9.0e0) AS y,
        ARRAY(1.0e0, 2.0e0, 3.0e0, 4.0e0) AS x
    ).TRANSFORM(SELECT REGR_R2(y, x) OVER (ORDER BY id ASC).ROUND(2)).UNWRAP(),
    -- R² counting only rows where v > 15 (10 and 5 excluded from the frame)
    f2 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(30, 10, 20, 5) AS v,
        ARRAY(3.0e0, 5.0e0, 7.0e0, 9.0e0) AS y,
        ARRAY(1.0e0, 2.0e0, 3.0e0, 4.0e0) AS x
    ).TRANSFORM(
        SELECT REGR_R2(y, x) FILTER (WHERE v > 15) OVER (ORDER BY id ASC).ROUND(2)
    ).UNWRAP()
;
Result
f1 ARRAYf2 ARRAY
[null, 1.00, 1.00, 1.00][null, null, 1.00, 1.00]