Linear Regression

Find the mean and standard deviation of a list of numbers.

Complete Code

Download file: LinearRegression.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

loadMacros("PGstandard.pl", "PGML.pl", 'PGstatisticsmacros.pl', "PGcourse.pl");

Preamble

The PGstatisticsmacros.pl macro provides the sample_correlation and linear_regression methods.

# Generate a random slope and intercept.
$m = random(0.1, 0.75, 0.05);
$b = random(0.5, 5,    0.25);

$x = [];
$y = [];

# Create some random data
for (0 .. 9) {
    $x->[$_] = random(2.5, 7.5, 0.5);
    $y->[$_] = $m * $x->[$_] + $b;
}

@rows = map { [ $x->[$_], $y->[$_] ] } 0 .. $#$x;

$corr = sample_correlation($x, $y);
($m, $b) = linear_regression($x, $y);

Setup

Generate random numbers, and then use the sample_correlation and linear_regression methods from PGstatisticsmacros.pl.

BEGIN_PGML
Consider the following data:

[#
    [.[`x`].] [.[`y`].]*{ headerrow => 1 }
    [. .]{ rows => \@rows }
#]{ horizontalrules => 1, align => '|c|c|' }

Find the correlation coefficient and the linear regression line:

a) correlation coefficient: [__]{$corr}

b) linear regression line [`\hat{y} =`] [__]{Formula("$m x + $b")}

END_PGML

Statement

This is the problem statement in PGML.
BEGIN_PGML_SOLUTION
Provide a solution here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.