Skip to content

Instantly share code, notes, and snippets.

@chrispahm
Last active September 27, 2019 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrispahm/4804fd4e22cd0fbd293f659530c2271d to your computer and use it in GitHub Desktop.
Save chrispahm/4804fd4e22cd0fbd293f659530c2271d to your computer and use it in GitHub Desktop.
R lm() poly output to JS function
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