Skip to content

Latest commit

 

History

History
125 lines (87 loc) · 3.79 KB

README.md

File metadata and controls

125 lines (87 loc) · 3.79 KB

regression.js

Build Status

regression.js is a JavaScript library containing a collection of least-squares fitting methods for finding a trend in a set of data. It currently contains methods for linear, exponential, logarithmic, power and polynomial trends.

Installation

The library can be installed from both bower and npm.

Usage

Most regressions require only two parameters - the regression method (linear, exponential, logarithmic, power or polynomial) and a data source. A third parameter can be used to define the degree of a polynomial when a polynomial regression is required. The regression method name is case-insensitive.

All models return an object with the following properties:

  • equation: an array containing the coefficients of the equation
  • string: A string representation of the equation
  • points: an array containing the predicted data
  • r2: the coefficient of determination(R2)
  • bic: the bayesian information criteria
  • predict: a function of the form f(x) that can be used to invoke a regression model on a value

Regression Types

Linear regression

equation: [gradient, y-intercept] in the form y = mx + c

var data = [[0,1],[32, 67] .... [12, 79]];
var result = regression('linear', data);
var slope = result.equation[0];
var yIntercept = result.equation[1];

Linear regression through the origin

equation: [gradient] in the form y = mx

var data = [[0,1],[32, 67] .... [12, 79]];
var result = regression('linearThroughOrigin', data);

Exponential regression

equation: [a, b] in the form y = ae^bx

Logarithmic regression

equation: [a, b] in the form y = a + b ln x

Power law regression

equation: [a, b] in the form y = ax^b

Polynomial regression

equation: [a0, ... , an] in the form anx^n ... + a1x + a0

var data = [[0,1],[32, 67] .... [12, 79]];
var result = regression('polynomial', data, 4);

Lastvalue

Not exactly a regression. Uses the last value to fill the blanks when forecasting.

Auto

This compares the bic parameters of multiple models to determine the one that fits the data best.

var data = [[0,1],[32, 67] .... [12, 79]];
var models = [{type: 'linear' }, {type: 'polynomial', order: 2}, {type: 'exponential' }];
var result = regression('auto',data, models);

If models is undefined regression.js will check linear, quadratic, cubic, quartic, exponential, and logarithmic models and returns whichever appears to most accurately model the data.

var data = [[0,1],[32, 67] .... [12, 79]];
var result = regression('auto',data);

Filling the blanks and forecasting

var data = [[0,1], [32, null] .... [12, 79]];

In any regression, if you use a null value for data, regression-js will fill it using the trend.

Development

Install packages: npm install

The project is built and controlled with grunt.

To prepare for release, run the default task, which:

  • Lints the source and tests with ESLint
  • Minifies the javascript in to the build/ directory

To run tests, grunt test.