You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by iyounus <gi...@git.apache.org> on 2015/12/18 22:31:48 UTC

[GitHub] spark pull request: [SPARK-12331][ML] R^2 for regression through t...

Github user iyounus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10384#discussion_r48072262
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/evaluation/RegressionMetricsSuite.scala ---
    @@ -22,91 +22,111 @@ import org.apache.spark.mllib.util.MLlibTestSparkContext
     import org.apache.spark.mllib.util.TestingUtils._
     
     class RegressionMetricsSuite extends SparkFunSuite with MLlibTestSparkContext {
    +  val obs = List[Double](77, 85, 62, 55, 63, 88, 57, 81, 51)
    +  val eps = 1E-5
     
       test("regression metrics for unbiased (includes intercept term) predictor") {
         /* Verify results in R:
    -       preds = c(2.25, -0.25, 1.75, 7.75)
    -       obs = c(3.0, -0.5, 2.0, 7.0)
    +        y = c(77, 85, 62, 55, 63, 88, 57, 81, 51)
    +        x = c(16, 22, 14, 10, 13, 19, 12, 18, 11)
    +        df <- as.data.frame(cbind(x, y))
    +        model <- lm(y ~  x, data=df)
    +        preds <- signif(predict(model), digits = 4)
     
    -       SStot = sum((obs - mean(obs))^2)
    -       SSreg = sum((preds - mean(obs))^2)
    -       SSerr = sum((obs - preds)^2)
    +        cat("predictions: ", preds, "\n")
    +        cat("explainedVariance =", mean((preds - mean(y))^2), "\n")
    +        cat("meanAbsoluteError =", mean(abs(preds - y)), "\n")
    +        cat("meanSquaredError  =", mean((preds - y)^2), "\n")
    +        cat("rmse =", sqrt(mean((preds - y)^2)), "\n")
    +        cat("r2 =", summary(model)$r.squared, "\n")
     
    -       explainedVariance = SSreg / length(obs)
    -       explainedVariance
    -       > [1] 8.796875
    -       meanAbsoluteError = mean(abs(preds - obs))
    -       meanAbsoluteError
    -       > [1] 0.5
    -       meanSquaredError = mean((preds - obs)^2)
    -       meanSquaredError
    -       > [1] 0.3125
    -       rmse = sqrt(meanSquaredError)
    -       rmse
    -       > [1] 0.559017
    -       r2 = 1 - SSerr / SStot
    -       r2
    -       > [1] 0.9571734
    +      Output of R code:
    +        predictions:  72.08 91.88 65.48 52.28 62.18 81.98 58.88 78.68 55.58
    +        explainedVariance = 157.3
    +        meanAbsoluteError = 3.735556
    +        meanSquaredError  = 17.53951
    +        rmse = 4.18802
    +        r2 = 0.8996822
          */
    -    val predictionAndObservations = sc.parallelize(
    -      Seq((2.25, 3.0), (-0.25, -0.5), (1.75, 2.0), (7.75, 7.0)), 2)
    +    val preds = List(72.08, 91.88, 65.48, 52.28, 62.18, 81.98, 58.88, 78.68, 55.58)
    +    val pairs: Seq[(Double, Double)] = preds.zip(obs)
    +    val predictionAndObservations = sc.parallelize(pairs, 2)
         val metrics = new RegressionMetrics(predictionAndObservations)
    -    assert(metrics.explainedVariance ~== 8.79687 absTol 1E-5,
    +    assert(metrics.explainedVariance ~== 157.3 absTol eps,
           "explained variance regression score mismatch")
    -    assert(metrics.meanAbsoluteError ~== 0.5 absTol 1E-5, "mean absolute error mismatch")
    -    assert(metrics.meanSquaredError ~== 0.3125 absTol 1E-5, "mean squared error mismatch")
    -    assert(metrics.rootMeanSquaredError ~== 0.55901 absTol 1E-5,
    +    assert(metrics.meanAbsoluteError ~== 3.735556 absTol eps, "mean absolute error mismatch")
    +    assert(metrics.meanSquaredError ~== 17.53951 absTol eps, "mean squared error mismatch")
    +    assert(metrics.rootMeanSquaredError ~== 4.18802 absTol eps,
           "root mean squared error mismatch")
    -    assert(metrics.r2 ~== 0.95717 absTol 1E-5, "r2 score mismatch")
    +    assert(metrics.r2 ~== 0.8996822 absTol eps, "r2 score mismatch")
       }
     
       test("regression metrics for biased (no intercept term) predictor") {
         /* Verify results in R:
    -       preds = c(2.5, 0.0, 2.0, 8.0)
    -       obs = c(3.0, -0.5, 2.0, 7.0)
    +        y = c(77, 85, 62, 55, 63, 88, 57, 81, 51)
    +        x = c(16, 22, 14, 10, 13, 19, 12, 18, 11)
    +        df <- as.data.frame(cbind(x, y))
    +        model <- lm(y ~ 0 + x, data=df)
    +        preds <- signif(predict(model), digits = 4)
     
    -       SStot = sum((obs - mean(obs))^2)
    -       SSreg = sum((preds - mean(obs))^2)
    -       SSerr = sum((obs - preds)^2)
    +        cat("predictions: ", preds, "\n")
    +        cat("explainedVariance =", mean((preds - mean(y))^2), "\n")
    +        cat("meanAbsoluteError =", mean(abs(preds - y)), "\n")
    +        cat("meanSquaredError  =", mean((preds - y)^2), "\n")
    +        cat("rmse =", sqrt(mean((preds - y)^2)), "\n")
    +        cat("r2 =", summary(model)$r.squared, "\n")
     
    -       explainedVariance = SSreg / length(obs)
    -       explainedVariance
    -       > [1] 8.859375
    -       meanAbsoluteError = mean(abs(preds - obs))
    -       meanAbsoluteError
    -       > [1] 0.5
    -       meanSquaredError = mean((preds - obs)^2)
    -       meanSquaredError
    -       > [1] 0.375
    -       rmse = sqrt(meanSquaredError)
    -       rmse
    -       > [1] 0.6123724
    -       r2 = 1 - SSerr / SStot
    -       r2
    -       > [1] 0.9486081
    +      Output of R code:
    --- End diff --
    
    I can follow that conversion. I just thought it was easier to save the code in a file and run it from R prompt.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org