Skip to content

Commit

Permalink
Fix RegressionMetrics computation of explainedVariance and r2
Browse files Browse the repository at this point in the history
  • Loading branch information
Feynman Liang committed Jul 13, 2015
1 parent c472eb1 commit 4c4e56f
Showing 1 changed file with 11 additions and 10 deletions.
Expand Up @@ -53,14 +53,16 @@ class RegressionMetrics(predictionAndObservations: RDD[(Double, Double)]) extend
)
summary
}
private lazy val SSerr = math.pow(summary.normL2(1), 2)
private lazy val SStot = summary.variance(0)
private lazy val SSreg = SStot - SSerr

/**
* Returns the explained variance regression score.
* explainedVariance = 1 - variance(y - \hat{y}) / variance(y)
* Reference: [[http://en.wikipedia.org/wiki/Explained_variation]]
* Returns the variance explained by regression.
* @see [[https://en.wikipedia.org/wiki/Fraction_of_variance_unexplained]]
*/
def explainedVariance: Double = {
1 - summary.variance(1) / summary.variance(0)
SSreg / summary.count
}

/**
Expand All @@ -76,23 +78,22 @@ class RegressionMetrics(predictionAndObservations: RDD[(Double, Double)]) extend
* expected value of the squared error loss or quadratic loss.
*/
def meanSquaredError: Double = {
val rmse = summary.normL2(1) / math.sqrt(summary.count)
rmse * rmse
SSerr / summary.count
}

/**
* Returns the root mean squared error, which is defined as the square root of
* the mean squared error.
*/
def rootMeanSquaredError: Double = {
summary.normL2(1) / math.sqrt(summary.count)
math.sqrt(this.meanSquaredError)
}

/**
* Returns R^2^, the coefficient of determination.
* Reference: [[http://en.wikipedia.org/wiki/Coefficient_of_determination]]
* Returns R^2^, the unadjusted coefficient of determination.
* @see [[http://en.wikipedia.org/wiki/Coefficient_of_determination]]
*/
def r2: Double = {
1 - math.pow(summary.normL2(1), 2) / (summary.variance(0) * (summary.count - 1))
SSreg / SStot
}
}

0 comments on commit 4c4e56f

Please sign in to comment.