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', 'niceTables.pl',
    "PGcourse.pl"
);

Preamble

Statistics functions mean and standard deviation are used so we load PGstatisticsmacros.pl. We use the DataTable method from the niceTables.pl macro.

# produce an approximate slope and intercept
$m = random(0.1, 0.75, 0.05);
$b = random(0.5, 5,    0.25);

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

@rows = ([ '\(x\)', '\(y\)' ]);
push(@rows, [ $x[$_], $y[$_] ]) for (0 .. $#x);

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

Setup

First, generate random numbers and then use the methods sample_correlation and linear_regression from the macro PGstatisticsmacros.pl.

BEGIN_PGML
Consider the following data:

[@ DataTable(\@rows,
  padding => [0.25, 0.25], 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.