I try to fit a linear model (and get the R^2) to the following test-data (first column containing independent variable, second column dependent variable)
0.0 0.0
1.0 1.0
2.0 2.0
3.0 3.1
I wrote the following code using scalanlp/breeze 0.12 :
import breeze.linalg.{DenseMatrix, DenseVector}
import breeze.stats.regression.leastSquares
val indep = DenseMatrix((1.0, 0.0), (1.0, 1.0), (1.0, 2.0), (1.0, 3.0))
val dep = DenseVector(0.0, 1.0, 2.0, 3.1)
val result = leastSquares(indep, dep)
println("intercept=" + result.coefficients.data(0))
println("slope=" + result.coefficients.data(1))
println("r^2=" + result.rSquared)
the output is:
intercept=-0.020000000000000018
slope=1.03
r^2=0.0014623322596666252
Intercept and slope are reasonable, but I don't understand R-squared, it should be close to 1!