The values you want to compute are called residuals, and incidentally, Statistics::LineFit does offer a method to compute them, e.g. my @resid = $lfit->residuals(); (The general idea is that you have some model (the trend line equation, in this case), which explains/predicts some of the variation found in the data. The deviations from the model are called residuals.)
OTOH, in order to get a better understanding of what's going on, there's nothing wrong with writing the code yourself, in particular as it's rather straightforward. Similarly, if you were to compute residuals for other points (i.e. points which were not used to compute the parameters of the trend equation), you'd have to code it yourself anyway (Statistics::LineFit's residuals method doesn't do the latter).
In your case, positive residuals are above, and negative residuals below the trend line. What's "on" the line is a matter of definition. Generally, in mathematics, a line is infinitisimally thin, so practically nothing ever really lies "on" it (and a direct comparison of floats would be subject to round-off errors)... but you can specify a certain +/- range around it, and check whether a certain value lies within that range.
Here's what the check could look like, essentially (I'll leave it to you to integrate that into your routine):
my $delta = 0.01; # choose some sensible value (defines range) for my $i (0 .. $#array_of_x_values) { my ($x, $y) = ($array_of_x_values[$i], $array_of_y_values[$i]) +; my $y_trend = $intercept + ($x * $slope); my $resid = $y - $y_trend; if ($resid > -$delta and $resid < $delta) { # points are "on" (i.e. within some small range around) tr +end line # ... } else { if ($resid > 0) { # points are above the trend line # ... } else { # points are below the trend line # ... } } }
In reply to Re: returning distance from trendline
by almut
in thread returning distance from trendline
by Win
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |