//linearReg.js
//David N. Blauch  Version 1.0  Copyright 2000
//
//x and y are arrays
//function returns an array, element 0 is the slope and element 1 is the intercept
function linearReg(x,y) 	//perform the linear regression analysis
{
	var val = new Array(2);
	var nbrPts = x.length;

	if (nbrPts<=1) {alert("At least two points are required to determine a line."); return;}
	var sX = 0;
	var sY = 0;
	var sXX = 0;
	var sXY = 0;
	for (i=0; i<nbrPts; i++) {
		sX += x[i];	sY += y[i];	sXY += x[i]*y[i];	sXX += x[i]*x[i];
	}
	slope = (nbrPts*sXY-sX*sY)/(nbrPts*sXX-sX*sX);
	intercept = (sY-slope*sX)/nbrPts;
	val[0] = slope;
	val[1] = intercept;
	return val;
}

