It sounds like Statistics::LSNoHistory might be what you're looking for. It will calculate a Least-Squares linear regression for a given set of input data and return the slope (m) and intercept (k) for a line of the form y = m*x + k. It will also calculate Pearson's r correlation coefficient, which is simply a measure of how well the line fits your data. If by "error" you mean variance, you can calculate that easily after the equation of the line is known (simply sum the squares of the differences of the y-values for each data point and the value predicted by the equation, then divide by the number of data points use the variance_x and variance_y methods).

Update: here is some code that uses your example data:

while( my ( $distrib_type, $href ) = each %{ $distributions } ) { my $regobj = Statistics::LSNoHistory->new( points => [ %{ $href->{distribution} } ] ); print "\nData for $distrib_type:\n"; printf("Slope: %.2f\n", $regobj->slope); printf("Intercept %.2f\n", $regobj->intercept); printf("Correlation Coefficient: %.2f\n", $regobj->pearson_r); printf("Variance (y): %.2f\n", $regobj->variance_y); }

Output:

Data for fast_increase: Slope: 0.72 Intercept 1.05 Correlation Coefficient: 0.89 Variance (y): 22.78 Data for slow_increase: Slope: 0.36 Intercept 0.53 Correlation Coefficient: 0.89 Variance (y): 5.69 Data for random: Slope: 0.12 Intercept 2.77 Correlation Coefficient: 0.28 Variance (y): 6.11


In reply to Re: Calculating your basic $constant, $slope, and $error terms for a time series distribution by bobf
in thread Calculating your basic $constant, $slope, and $error terms for a time series distribution by tphyahoo

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.