Last active
September 27, 2019 13:37
-
-
Save chrispahm/4804fd4e22cd0fbd293f659530c2271d to your computer and use it in GitHub Desktop.
R lm() poly output to JS function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var coeff = [ | |
[1,0], | |
[2,0], | |
[3,0], | |
[4,0], | |
[0,1], | |
[1,1], | |
[2,1], | |
[3,1], | |
[0,2], | |
[1,2], | |
[2,2], | |
[0,3], | |
[1,3], | |
[0,4], | |
] | |
function toFormula(arr, i) { | |
return 'y[' + (i + 1) + '] * ' + arr.map((val,i) => { | |
let variable = 'size' | |
if (i === 1) variable = 'distance' | |
if (val === 1) return variable | |
if (val > 1) return 'Math.pow(' + variable + ',' + val + ')' | |
}).filter(a => a !== undefined).join(' * ') | |
} | |
;(() => { | |
return `function runRegression(y,size,distance) { | |
return ${'y[0]\n + ' + coeff.map(toFormula).join('\n + ')} | |
}` | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment