programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# How to find a regression estimate on JavaScript?

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
In JavaScript, how can I do regression estimates from data? I mean, I have data of the form
1960: 200000
1970: 400000
1980: 600000
1990: 1000000
2000: 1800000
2010: 3300000
My goal is to predict what is the value of 2020 if the growth remains the same. The problem was given as how a population of particular artificial country will grow so I am not sure if the exponential model describes the growth well enough.

Marshal
Posts: 74376
334
• Number of slices to send:
Optional 'thank-you' note:
Do you mean, how do you write a linear regression formula, or how do you verify that the historical population figures for a country do show exponential growth?
Can you work out the logarithms of those numbers and do regression on them? If you were doubling those numbers every ten years, you would have reached 6,400,000 by now.

You mentioned JS, which we already have a forum for, but I think I should add you to our general computing forum because such algorithms are language‑neutral.

Jaakko Seppälä
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
I mean that it would be nice to see what kind of regression models the data a good way, and what are the coefficients of such a model. If this is too hard, it is sufficient to find real numbers a and b such that e^(a+bx) fits to the data the best way is sense of ordinary least squares approximation. I try to learn how to implement regression and matrix algebra to JavaScript.

Campbell Ritchie
Marshal
Posts: 74376
334
• Number of slices to send:
Optional 'thank-you' note:
That sounds like something where you can look up an algorithm. Does JS support exponentiation in its mathematical libraries?

Bartender
Posts: 4689
183
• Number of slices to send:
Optional 'thank-you' note:
The three important things are Matrix multiplication, transpose a Matrix and invert a Matrix. Look for Gaussian elimination for the inversion. I always used Excel too, to see graphically what kind of model I had to use (direct lineat, log-log, quadratic, et cetera.

Another way is to look up the formulas that involve terms line x, x^2, x*y, y^2 et cetera, no linear algebra required.

I have this method in my Matrix class: (it's in java)

Campbell Ritchie
Marshal
Posts: 74376
334
• Number of slices to send:
Optional 'thank-you' note:
Do you need matrix multiplication if you can draw/impute a graph of time vs. log(population) and do a least squares fit to it?

Piet Souris
Bartender
Posts: 4689
183
• Number of slices to send:
Optional 'thank-you' note:
About the multiply: as you can see in the result Matrix, there is quite some multiplying involved.

But if you make a graph of the data, then how do you do that regression?

By the way: the example I gave in my method lacks a little. A cow for the first one who corrects it.

Campbell Ritchie
Marshal
Posts: 74376
334
• Number of slices to send:
Optional 'thank-you' note:
There is a method called least squares which gives the closest possible straight line fit to the data. If they are supposed to be exponential, you can do a regression to their logarithm.

Piet Souris
Bartender
Posts: 4689
183
• Number of slices to send:
Optional 'thank-you' note:
My result matrix is exactly that method.

Jaakko Seppälä
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
I think the following will do the job:

Campbell Ritchie
Marshal
Posts: 74376
334
• Number of slices to send:
Optional 'thank-you' note:
Don't use Math.pow when you could write the numbers as literals. I think 1960,&'xa0;2&&xa0;* Math.pow(10, 6) is incorrect.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton