2

I try to fit a linear model (and get the R^2) to the following test-data

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!

Raphael Roth
  • 26,751
  • 15
  • 88
  • 145

1 Answers1

1

Your vector of ones needs to come last not first. Hence the r^2 = 1-r_e^2 you expected.

EDIT: While what I said above is correct about switching around your vector of 1s. I'm still getting a horribly incorrect r-squared as well even using that. My slope and intercept are spot on though, much like yours. So... to the source code!

EDIT2: Known issue. Hasn't been fixed. shrug

Robert Beatty
  • 508
  • 5
  • 11