observablehq/plot

Return slope from linear regression mark?

hbcondo opened this issue · 1 comments

Is it possible to return the slope and other calculated values from a linear regression mark? Overall, it would be great to get these values so we can determine the trend direction. Dealing with hundreds of plots, being able to filter on which plots have high increases / decreases in trend direction is the main goal.

const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);

Fil commented

This is out of scope. Even though I sometimes also want this, it's better to leave this to specialized libraries (like simple-statistics). The simple explanation is that the goal of a mark is only to draw the trend line, but that's not the main reason. A more serious answer is that the stats are done in pixel space, so the slope and most of the derived values depend on the scales, and are not immediately useful without translating them back to data space—which is feasible for linear scales but more complex with log/sqrt scales. (I think R2 is independent of the scaling, and could be returned separately, but then we'd have to design a whole API around this—currently marks don't expose anything but the visuals they insert in the chart.)