Function linearRegressBeta

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.

double[] linearRegressBeta(U, T...) (
  U Y,
  T XIn
)
if (doubleInput!U);

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.

References

http

//www.mathworks.com/help/toolbox/stats/ridge.html

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.)

Examples

int[] nBeers = [8,6,7,5,3,0,9];
int[] nCoffees = [3,6,2,4,3,6,8];
int[] musicVolume = [3,1,4,1,5,9,2];
int[] programmingSkill = [2,7,1,8,2,8,1];
double[] betas = linearRegressBeta(programmingSkill, repeat(1), nBeers, nCoffees,
    musicVolume, map!"a * a"(musicVolume));

// Now throw in a ridge parameter of 2.5.
double[] ridgeBetas = linearRegressBeta(programmingSkill, repeat(1), nBeers,
    nCoffees, musicVolume, map!"a * a"(musicVolume), 2.5);