# Regression.pm - weighted linear regression package

### From WeBWorK

- NAME
- DESCRIPTION
- USAGE
- ALGORITHM
- AUTHOR
- Subroutines
- DEBUGGING = SAMPLE USAGE CODE
- BUGS/PROBLEMS
- INSTALLATION and DOCUMENTATION
- LICENSE

# NAME

Regression.pm - weighted linear regression package (line+plane fitting)

# DESCRIPTION

Regression.pm is a multivariate linear regression package. That is, it estimates the c coefficients for a line-fit of the type

y= b(0)*x(0) + b(1)*x1 + b(2)*x2 + ... + b(k)*xk

given a data set of N observations, each with k independent x variables and one y variable. Naturally, N must be greater than k---and preferably considerably greater. Any reasonable undergraduate statistics book will explain what a regression is. Most of the time, the user will provide a constant ('1') as x(0) for each observation in order to allow the regression package to fit an intercept.

# USAGE

If the sample data for (x1, x2, y) includes ($x1[$i], $x2[$i], $y[$i]) for 0<=$i<=5, type

$reg = Regression->new( 3, "y", [ "const", "x1", "x2" ] );

for($i=0; $i<6; $i++){ $reg->include( $y[$i], [ 1.0, $x1[$i], $x2[$i] ] ); }

@coeff= $reg->`theta()`

;

$b0 = $coeff[0][0]; $b1 = $coeff[0][1]; $b2 = $coeff[0][2];

# ALGORITHM

## Original Algorithm (ALGOL-60):

W. M. Gentleman, University of Waterloo, "Basic Description For Large, Sparse Or Weighted Linear Least Squares Problems (Algorithm AS 75)," Applied Statistics (1974) Vol 23; No. 3

## INTERNALS

R=Rbar is an upperright triangular matrix, kept in normalized form with implicit 1's on the diagonal. D is a diagonal scaling matrix. These correspond to "standard Regression usage" as

X' X = R' D R

A backsubsitution routine (in thetacov) allows to invert the R matrix (the inverse is upper-right triangular, too!). Call this matrix H, that is H=R^(-1).

(X' X)^(-1) = [(R' D^(1/2)') (D^(1/2) R)]^(-1) = [ R^-1 D^(-1/2) ] [ R^-1 D^(-1/2) ]'

## Remarks

This algorithm is the statistical "standard." Insertion of a new observation can be done one obs at any time (WITH A WEIGHT!), and still only takes a low quadratic time. The storage space requirement is of quadratic order (in the indep variables). A practically infinite number of observations can easily be processed!

# AUTHOR

Naturally, Gentleman invented this algorithm. Adaptation by ivo welch. Alan Miller (alan@dmsmelb.mel.dms.CSIRO.AU) pointed out nicer ways to compute the R^2.

# Subroutines

## new

receives the number of variables on each observations (i.e., an integer) and returns the blessed data structure. Also takes an optional name for this regression to remember, as well as a reference to a k*1 array of names for the X coefficients.

## dump

is used for debugging.

prints the estimated coefficients, and R^2 and N.

## include

receives one new observation. Call is

$blessedregr->include( $yvariable, [ $x1, $x2, $x3 ... $xk ], 1.0 );

where 1.0 is an (optional) weight. Note that inclusion with a weight of -1 can be used to delete an observation.

The function returns the number of observations so far included.

## theta

estimates and returns the vector of coefficients.

## rsq, adjrsq, sigmasq, ybar, sst, k, n

These functions provide common auxiliary information. rsq, adjrsq, sigmasq, sst, and ybar have not been checked but are likely correct. The results are stored for later usage, although this is somewhat unnecessary because the computation is so simple anyway.

# DEBUGGING = SAMPLE USAGE CODE

The sample code included with this package demonstrates regression usage. To execute it, just set the constant DEBUGGING at the script head to 1, and do

perl Regression.pm

The printout should be

**************************************************************** Regression 'sample regression' **************************************************************** Theta[0='const']= 0.2950 Theta[1='someX']= 0.6723 Theta[2='someY']= 1.0688 R^2= 0.808, N= 4 ****************************************************************

# BUGS/PROBLEMS

**Missing**-
This package lacks routines to compute the standard errors of the coefficients. This requires access to a matrix inversion package, and I do not have one at my disposal. If you want to add one, please let me know.

**Perl Problem**-
perl is unaware of IEEE number representations. This makes it a pain to test whether an observation contains any missing variables (coded as 'NaN' in Regression.pm).

**Others**-
I am a novice perl programmer, so this is probably ugly code. However, it does seem to work, and I could not find anything equivalent on cpan.

# INSTALLATION and DOCUMENTATION

Installation consists of moving the file 'Regression.pm' into a subdirectory Statistics of your modules path (e.g., /usr/lib/perl5/site_perl/5.6.0/).

The documentation was produced from the module:

pod2html -noindex -title "perl weighted least squares regression package" Regression.pm > Regression.html

The documentation was slightly modified by Maria Voloshina, University of Rochester.

# LICENSE

This module is released for free public use under a GPL license.

(C) Ivo Welch, 2001.