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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.