Strange R^2 when fitting a linear model

44 views
Skip to first unread message

Raphael Roth

unread,
Oct 20, 2016, 1:16:17 PM10/20/16
to Scala Breeze
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!



David Hall

unread,
Oct 20, 2016, 1:50:33 PM10/20/16
to scala-...@googlegroups.com
clearly a bug. please file a bug report? (I wonder if it it's just 1 - r^2?)

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/edf60e6d-2309-412c-aef1-b8e572758634%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages