Simple linear regression made in JavaScript.
npm i clementreiffers-linear-regression
or yarn add clementreiffers-linear-regression
if you
use yarn instead of npm.
the lightest function is very usefull if you're interested in getting only the essential parameters
import { linearRegression, predict } from "clementreiffers-linear-regression";
// import { computeLightLinearRegression } from "clementreiffers-linear-regression";
const x = [1, 2, 3, 4];
const y = [1, 2, 3, 4];
const lr = linearRegression(x, y, true); // if you want values into an Object
// executed only if true in linearRegression Function, it gives the same result as above
// computeLightLinearRegression(x, y);
const pred1 = predict([1, 2], lr);
const pred2 = predict(6, lr);
console.log(lr); // to show the object which represents the linear regression
by trying this example above, you will have :
{ parameters: { a: 1, b: 0 } }
it will compute all necessary calculations and put it into the same json.
import { linearRegression, predict } from "clementreiffers-linear-regression";
// import { computeLoudLinearRegression } from "clementreiffers-linear-regression";
const x = [1, 2, 3, 4];
const y = [1, 2, 3, 4];
const lr = linearRegression(x, y); // if you want values into an Object
// executed by default, it gives the same result as above
// computeLoudLinearRegression(x, y);
const pred1 = predict([1, 2], lr);
const pred2 = predict(6, lr);
console.log(lr); // to show the object which represents the linear regression
by trying this example above, you will have :
{
parameters: { a: 1, b: 0 },
trainData: { x: [ 1, 2, 3, 4 ], y: [ 1, 2, 3, 4 ] },
trainCurvePredict: [ 1, 2, 3, 4 ],
statistics: { r2: 0.9999999999999996, cost: 0, pearson: 0.9999999999999998 }
}
the score represents the capacity to do a linear regression with the data given.
import { score } from "clementreiffers-linear-regression";
const x = [1, 2, 3, 4];
const y = [1, 2, 3, 4];
console.log(score(x, y));
by executing this code you will have :
0.9999999999999996
import { linearRegression, costFunction } from "clementreiffers-linear-regression";
const x = [1, 2, 3, 4];
const y = [1, 2, 3, 4];
const lr = linearRegression(x, y);
const pred = predict(lr, x);
const cost = costFunction(y, pred);
console.log(cost);
by executing this function you will have :
0
this package use the Covariance and Variance to calculate the linear regression, see here : https://en.wikipedia.org/wiki/Linear_regression
any idea to improve this package ?
- email me to : [email protected]
- do a git issue on
- contact me on linkedin : https://www.linkedin.com/in/cl%C3%A9ment-reiffers-bb8983185/