Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear Regression in Javascript [closed]

I want to do Least Squares Fitting in Javascript in a web browser.

Currently users enter data point information using HTML text inputs and then I grab that data with jQuery and graph it with Flot.

After the user had entered in their data points I would like to present them with a "line of best fit". I imagine I would calculate the linear, polynomial, exponential and logarithmic equations and then choose the one with the highest R^2 value.

I can't seem to find any libraries that will help me to do this though. I stumbled upon jStat, but it is completely missing documentation (as far as I can find) and after digging through the the source code it doesn't seem to have any linear regression functionality built in--I'm basing this purely on function names however.

Does anyone know any Javascript libraries that offer simple regression analysis?


The hope would be that I could use the library like so...

If I had some set of scatter points in an array var points = [[3,4],[15,45],...[23,78]], I would be able to hand that to some function like lin_reg(points) and it would return something like [7.12,3] if the linear equation was y = 7.12 x + 3.

like image 241
Chris W. Avatar asked Jun 01 '11 01:06

Chris W.


People also ask

Can you do Linear Regression in JavaScript?

Regression. js is a JavaScript module containing a collection of linear least-squares fitting methods for simple data analysis. It can be used for data approximation, finding data regularities and so on. It is available as the 'regression' package on npm and on the cdnjs CDN.

What does Linear Regression () do?

Linear regression analysis is used to predict the value of a variable based on the value of another variable. The variable you want to predict is called the dependent variable. The variable you are using to predict the other variable's value is called the independent variable.

When should we not use regression?

There are two things that explain why Linear Regression is not suitable for classification. The first one is that Linear Regression deals with continuous values whereas classification problems mandate discrete values. The second problem is regarding the shift in threshold value when new data points are added.

Can regression lines run horizontally?

If changes in the independent variable (x) values have no impact to the values of the dependent variable (y), then there is no regression. In a regression model this can be seen as the line having no slope, i.e. a horizontal line with the slope coefficient equal to zero.


1 Answers

What kind of linear regression? For something simple like least squares, I'd just program it myself:

http://mathworld.wolfram.com/LeastSquaresFitting.html

The math is not too hard to follow there, give it a shot for an hour or so and let me know if it's too hard, I can try it.

EDIT:

Found someone that did it:

http://dracoblue.net/dev/linear-least-squares-in-javascript/159/

like image 100
Milimetric Avatar answered Oct 05 '22 06:10

Milimetric