linear-regression-model

1.4.2 • Public • Published

linear-regression-model

An npm package to make it easier to deal with a handful of values, and try to model them in one of the most used mathematical models, with an R-like accuracy algorithm

npm logo

Setup

Requirements:

  • Node.js installed
    The ideal version, to run the package, is LTS(16.13 by the time it is being published),
    however, older versions shouldn't have any issues, as the package does not use any
    other npm packages, or fancy, new methods, not supported by older versions

  • Installing the module

npm install linear-regression-model
  • Importing the module

First, there is the Linear Regression model between two different datasets, in relation to each other
Which will look like something similar to this:

import {LinearModel} from 'linear-regression-model'
// or
const {LinearModel} = require("linear-regression-model");
// if it is preferred not to use destructuring, or constants use
// var LinearModel = require("linear-regression-model-model").LinearModel;

Or if the use case for this is pending more to the behavior of one single dataset
overtime, this will be more fitting:

import {LinearModelOverTime} from 'linear-regression-model'
// or
const {LinearModelOverTime} = require("linear-regression-model");
// if it is preferred not to use destructuring, or constants use
// var LinearModel = require("linear-regression-model").LinearModelOverTime;

And to use the Correlation class, it will be some like this:

 import {Correlation} from 'linear-regression-model'
 // or
 const {Correlation} = require("linear-regression-model");
 // if it is preferred not to use destructuring, or constants use
 // var Correlation = require("linear-regression-model").Correlation;

Although if your use case is the overtime behaviour, i would advise the use
of the method in the LinearModelOverTime class
But if you are using the LinearModel class, it is really up to you

Instantiate the classes

const lm = new LinearModel([1, 2, 3, 2, 3, 4], [3, 4, 5, 4, 5, 6]);
// mandatory to pass two, same sized, all number, arrays, being the
// orientation (x, y), being x the independent variable and y the
// dependent: y changes according to how x changes, basically

or

const lm = new LinearModelOverTime([1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6]);
// mandatory to pass one, all number, array, being the orientation y, the
//dependent variable: y changes according to how x changes, the later is
// generated automatically for a better representation of the behavior overtime

or

 const corr = new Correlation([1, 2, 3, 2, 3, 4], [3, 4, 5, 4, 5, 6]);
// mandatory to pass two, same sized, all number, arrays, being the
// orientation (x, y), being x the independent variable and y the
// dependent: y changes according to how x changes, basically

Let it be clear, it is necessary to pass all number arrays, with more than 1 value,
in order for the algorithm to work properly, it will try to convert all the elements
to numbers, but if that is not possible, the code will crash.

Methods

Both the classes have similar methods, the biggest difference between the classes being
if the X axis dataset is informed or generated, therefore, the method will be showed using the LinearModel class, the use for the LinearModelOverTime class is the exact same though

LinearModel and LinearModelOverTime

  • getDataset
 lm.getDataset()
 // returns the dataset Informed in the Y axis
  • getXAxisValues
 lm.getXAxisValues()
 // returns the dataset in the X axis, informed previously or not
  • (static) radsToDegs
 LinearModel.radsToDegs(rad)
 // converts an angle in radians to degrees
 // returns the angle in degrees
  • (static) getMean
 LinearModel.getMean(dataset)
 // Utility method to calculate the mean of a dataset
  • (static) getMode
 LinearModel.getMode(dataset)
 // Utility method to calculate the mode of a dataset
  • (static) getMedian
 LinearModel.getMedian(dataset)
 // Utility method to calculate the median of a dataset
  • getDatasetLength
 lm.getDatasetLength()
 // returns the dataset length
  • getSumOfDatasetValues
 lm.getSumOfDatasetValues()
 // returns the sum of all the elements in the dataset on y azis
  • getSumOfXValues
 lm.getSumOfXValues()
 // returns the sum of all the elements in the dataset on x azis
  • getSlope
 lm.getSlope()
 // returns the slope of the equation: the "m" on the y = mx + n function
 // which is the tangent of the inclination angle
  • getAngleInRadians
 lm.getAngleInRadians()
 // returns the inclination angle in radians, like 3/4 meaning  3/4 rad
  • getAngleInDegrees
 lm.getAngleInDegrees()
 // returns the inclination angle in degrees, like 43°,
 // for example 3/4 rad ~~ 43°
  • getDatasetBehavior
 lm.getDatasetBehavior()
 // returns the overall macro behaviour of the dataset
  • getDatasetBehavioralIntensity
 lm.getDatasetBehavioralIntensity()
 // returns the intensity of the behaviour previously measured
  • getLinearCoefficient
 lm.getDatasetBehavioralIntensity()
 // returns the intensity of the behaviour previously measured
  • getLinearCoefficient
 lm.getLinearCoefficient()
 // returns the linear coefficient of the equation: the "n"
 //on the y = mx + n function
  • getCoefficients
 lm.getCoefficients()
 // returns both the linear coefficient and the slope
 //of the equation: the "m" and the "n" on y = mx + n
  • getLinearEquation
 lm.getLinearEquation()
 // returns both the function as a string to be displayed
 // and an actual js function to make predictions, for example
 // function(x): returns Y, using the same method as the dataset
  • getR2
 lm.getR2()
 // returns the coefficient of determination(R²) to find
 // the accuracy of the linear regression just calculated

Correlation

  • getCorrelation
 lm.getCorrelation()
 // returns the correlation between the datasets
  • getCorrelationInterpretation
 lm.getCorrelationInterpretation()
 // returns the interpretation of the correlation index
 // between the datasets

Correlation (class)

  • (static) getMean
 Correlation.getMean(dataset)
 // returns the mean of the dataset
  • (static) getDifferenceFromMeanAndElements
 Correlation.getDifferenceFromMeanAndElements(dataset)
 // returns the difference between the mean and the elements
 // of the dataset, so can be used to calculate variance
 // or standard deviation, for example
  • getCorrelation
 corr.getCorrelation()
 // returns the correlation between the datasets
  • getCorrelationWay
 corr.getCorrelationWay()
 // returns the way/sign the two datasets are correlated
 // to each other
  • getCorrelationIntensity
 corr.getCorrelationIntensity()
 // returns the intensity of the correlation between the datasets
  • getCorrelationInterpretation
 corr.getCorrelationInterpretation()
 // returns the interpretation of the correlation index
 // between the datasets

Contributing

Well, since this is a really simple package, contributing is always welcome, just as much as creating issues experienced with the package

In order to better organize this contributions, it would be ideal that all PRs follow the template:

PR Template

WHAT:
A brief description of the improvements

WHY:
A explanation on why those changes were needed, necessary, or at least, why is was on the best interest of the package users

CHANGES:
List of changes made, can be the name of the commits made, or a simple changes list

Commits

Ideally the commits should make use of the convention of Conventional Commits
Something i recommend is the usage of either the Commitizen terminal extension or the Commit Message Editor VSCode Extension

Readme

Keywords

none

Package Sidebar

Install

npm i linear-regression-model

Weekly Downloads

0

Version

1.4.2

License

ISC

Unpacked Size

40.6 kB

Total Files

4

Last publish

Collaborators

  • nikolas-virionis