Perform a linear regression and return just the beta values. The advantages to just returning the beta values are that it's faster and that each range needs to be iterated over only once, and thus can be just an input range. The beta values are returned such that the smallest index corresponds to the leftmost element of X. X can be either a tuple or a range of input ranges. Y must be an input range.

If, after all X variables are passed in, a numeric type is passed as the last parameter, this is treated as a ridge parameter and ridge regression is performed. Ridge regression is a form of regression that penalizes the L2 norm of the beta vector and therefore results in more parsimonious models. However, it makes statistical inference such as that supported by linearRegress() difficult to impossible. Therefore, linearRegress() doesn't support ridges.

If no ridge parameter is passed, or equivalently if the ridge parameter is zero, then ordinary least squares regression is performed.

Notes: The X ranges are traversed in lockstep, but the traversal is stopped at the end of the shortest one. Therefore, using infinite ranges is safe. For example, using repeat(1) to get an intercept term works.


Venables, W. N. & Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth Edition. Springer, New York. ISBN 0-387-95457-0 (This is the citation for the MASS R package.)



1 int[] nBeers = [8,6,7,5,3,0,9];
2 int[] nCoffees = [3,6,2,4,3,6,8];
3 int[] musicVolume = [3,1,4,1,5,9,2];
4 int[] programmingSkill = [2,7,1,8,2,8,1];
5 double[] betas = linearRegressBeta(programmingSkill, repeat(1), nBeers, nCoffees,
6     musicVolume, map!"a * a"(musicVolume));
8 // Now throw in a ridge parameter of 2.5.
9 double[] ridgeBetas = linearRegressBeta(programmingSkill, repeat(1), nBeers,
10     nCoffees, musicVolume, map!"a * a"(musicVolume), 2.5);