clementreiffers-linear-regression
v2.0.3
Published
calculation of a linear regression
Downloads
26
Maintainers
Readme
linear-regression
Simple linear regression made in JavaScript.
How to install
npm i clementreiffers-linear-regression
or yarn add clementreiffers-linear-regression
if you
use yarn instead of npm.
How to use
Linear Regression
The lightest function
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 } }
The loudest function
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 }
}
score
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
cost function
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
How it is calculated
this package use the Covariance and Variance to calculate the linear regression, see here : https://en.wikipedia.org/wiki/Linear_regression
Contacts
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/