Skip to content

Instantly share code, notes, and snippets.

@itrav
Created May 3, 2018 04:50
Show Gist options
  • Save itrav/3957403dcd42d29b3b167718d73c35b3 to your computer and use it in GitHub Desktop.
Save itrav/3957403dcd42d29b3b167718d73c35b3 to your computer and use it in GitHub Desktop.
let raw = `M,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15
M,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7
...
F,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9`;
let parsed = raw.split("\n").map(line => line.split(",").map((v, i) => i > 0 ? parseFloat(v): v == "M" ? 1 : -1));
let X = parsed.map(row => row.slice(0, row.length - 1));
let y = parsed.map(row => row[row.length - 1]);
regcoeff(X, y);
// => [3.3576422722029235, 0.18268246827720258, -1.5345509209630563, 12.47947253897155,
10.900190976889746, 10.530568134447548, -21.242605678685777, -11.118169569897418, 7.265138117998504]
regcoeff(X, y, 30);
// => [5.901924286379025, 0.1572216805234667, 1.708346241965316, 1.8784686275536408,
1.3662233281204532, 3.2006159123286366, -4.159964236588952, 0.03182702737051635, 4.40646195094493]
let regest1 = regestimator(X, y);
X.splice(0,3).map(regest1);
// => [9.497270394050621, 6.94080693306762, 8.018581297481597]
mean(X.map((row, i) => Math.pow(y[i] - regest1(row), 2)));
// => 4.898168242402778
mean(X_.map((row, i) => Math.pow(y_[i] - regest1(row), 2)));
// => 4.849607393269926
let regest2 = regestimator(X, y, 1, 30);
X.splice(0,3).map(regest2);
// => [12.960317691184055, 10.903546039864027, 9.565260647464731]
mean(X.map((row, i) => Math.pow(y[i] - regest2(row), 2)))
// => 5.00008919384234
mean(X_.map((row, i) => Math.pow(y_[i] - regest2(row), 2)))
// => 4.85483982326952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment