R-Squared

All functions · nseries package

R2

func (s Series) R2(n int) Series

R2 computes a least-squares regression on the last <n>{=html} values at each point in the series. Where there are insufficient values, the result is zero. The underlying calculation uses numerically stable, centred coordinates so R2 remains accurate for windows with a large common offset (for example, prices near 1e9). Finite constant and singleton windows yield zero. A window containing NaN or infinity yields NaN; finite windows recover.

The output is on a 0 to 1 scale, not a percentage. Its warm-up reads 0, not NaN, on bars 0 to n-2, and 0 reads as no linear fit.