ThatGuy343
ThatGuy343

Reputation: 2424

Java Linear Regression not working

Let me start off by saying I am terrible with math. I do not claim to be an expert (or even an advanced) mathematician by far.

What I'm trying to do is take this output:

55,55
55,55
340,340
333,333
41,41
71,141
143,133
121,122
12,16

And from those values (x,y) produce the following:

Slope (b): 0.96112312980002
Regression line equation: y=12.280535000464+0.96112312980002x
Correlation coefficient (r): 0.98120789611439

I used this website to calculate the above values can be found here. Any help in this matter would be greatly appreciated. I attempted using a snippet for this on stackoverflow found here, however the results are dismal. Can someone point me to a way of calculating these things via a library or even a class of its own?

The value i am most interested in is the correlation coefficient.

Upvotes: 1

Views: 213

Answers (2)

Paul Boddington
Paul Boddington

Reputation: 37655

The Apache Commons Math class SimpleRegression does it all.

You use addData() to input the x and y values. You use getSlope() and getIntercept() to get the equation of the regression line. You use getR() to get the correlation coefficient.

It couldn't be easier!

Upvotes: 1

Related Questions