-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-13925] [ML] [SparkR] Expose R-like summary statistics in SparkR::glm for more family and link functions #12393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,55 @@ test_that("glm and predict", { | |
| expect_equal(length(predict(lm(y ~ x))), 15) | ||
| }) | ||
|
|
||
| test_that("glm summary", { | ||
| # gaussian family | ||
| training <- suppressWarnings(createDataFrame(sqlContext, iris)) | ||
| stats <- summary(glm(Sepal_Width ~ Sepal_Length + Species, data = training)) | ||
|
|
||
| rStats <- summary(glm(Sepal.Width ~ Sepal.Length + Species, data = iris)) | ||
|
|
||
| coefs <- unlist(stats$coefficients) | ||
| rCoefs <- unlist(rStats$coefficients) | ||
| expect_true(all(abs(rCoefs - coefs) < 1e-4)) | ||
| expect_true(all( | ||
| rownames(stats$coefficients) == | ||
| c("(Intercept)", "Sepal_Length", "Species_versicolor", "Species_virginica"))) | ||
| expect_equal(stats$dispersion, rStats$dispersion) | ||
| expect_equal(stats$null.deviance, rStats$null.deviance) | ||
| expect_equal(stats$deviance, rStats$deviance) | ||
| expect_equal(stats$df.null, rStats$df.null) | ||
| expect_equal(stats$df.residual, rStats$df.residual) | ||
| expect_equal(stats$aic, rStats$aic) | ||
|
|
||
| # binomial family | ||
| df <- suppressWarnings(createDataFrame(sqlContext, iris)) | ||
| training <- df[df$Species %in% c("versicolor", "virginica"), ] | ||
| stats <- summary(glm(Species ~ Sepal_Length + Sepal_Width, data = training, | ||
| family = binomial(link = "logit"))) | ||
|
|
||
| rTraining <- iris[iris$Species %in% c("versicolor", "virginica"), ] | ||
| rStats <- summary(glm(Species ~ Sepal.Length + Sepal.Width, data = rTraining, | ||
| family = binomial(link = "logit"))) | ||
|
|
||
| coefs <- unlist(stats$coefficients) | ||
| rCoefs <- unlist(rStats$coefficients) | ||
| expect_true(all(abs(rCoefs - coefs) < 1e-4)) | ||
| expect_true(all( | ||
| rownames(stats$coefficients) == | ||
| c("(Intercept)", "Sepal_Length", "Sepal_Width"))) | ||
| expect_equal(stats$dispersion, rStats$dispersion) | ||
| expect_equal(stats$null.deviance, rStats$null.deviance) | ||
| expect_equal(stats$deviance, rStats$deviance) | ||
| expect_equal(stats$df.null, rStats$df.null) | ||
| expect_equal(stats$df.residual, rStats$df.residual) | ||
| expect_equal(stats$aic, rStats$aic) | ||
|
|
||
| # Test summary works on base GLM models | ||
| baseModel <- stats::glm(Sepal.Width ~ Sepal.Length + Species, data = iris) | ||
| baseSummary <- summary(baseModel) | ||
| expect_true(abs(baseSummary$deviance - 12.19313) < 1e-4) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we just test R native |
||
| }) | ||
|
|
||
| test_that("kmeans", { | ||
| newIris <- iris | ||
| newIris$Species <- NULL | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,19 +30,59 @@ private[r] class GeneralizedLinearRegressionWrapper private ( | |
| private val glm: GeneralizedLinearRegressionModel = | ||
| pipeline.stages(1).asInstanceOf[GeneralizedLinearRegressionModel] | ||
|
|
||
| lazy val rFeatures: Array[String] = if (glm.getFitIntercept) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Array("(Intercept)") ++ features | ||
| } else { | ||
| features | ||
| } | ||
|
|
||
| lazy val rCoefficients: Array[Double] = if (glm.getFitIntercept) { | ||
| Array(glm.intercept) ++ glm.coefficients.toArray | ||
| Array(glm.intercept) ++ glm.coefficients.toArray ++ | ||
| rCoefficientStandardErrors ++ rTValues ++ rPValues | ||
| } else { | ||
| glm.coefficients.toArray | ||
| glm.coefficients.toArray ++ rCoefficientStandardErrors ++ rTValues ++ rPValues | ||
| } | ||
|
|
||
| lazy val rFeatures: Array[String] = if (glm.getFitIntercept) { | ||
| Array("(Intercept)") ++ features | ||
| private lazy val rCoefficientStandardErrors = if (glm.getFitIntercept) { | ||
| Array(glm.summary.coefficientStandardErrors.last) ++ | ||
| glm.summary.coefficientStandardErrors.dropRight(1) | ||
| } else { | ||
| features | ||
| glm.summary.coefficientStandardErrors | ||
| } | ||
|
|
||
| private lazy val rTValues = if (glm.getFitIntercept) { | ||
| Array(glm.summary.tValues.last) ++ glm.summary.tValues.dropRight(1) | ||
| } else { | ||
| glm.summary.tValues | ||
| } | ||
|
|
||
| def transform(dataset: DataFrame): DataFrame = { | ||
| private lazy val rPValues = if (glm.getFitIntercept) { | ||
| Array(glm.summary.pValues.last) ++ glm.summary.pValues.dropRight(1) | ||
| } else { | ||
| glm.summary.pValues | ||
| } | ||
|
|
||
| lazy val rDispersion: Double = glm.summary.dispersion | ||
|
|
||
| lazy val rNullDeviance: Double = glm.summary.nullDeviance | ||
|
|
||
| lazy val rDeviance: Double = glm.summary.deviance | ||
|
|
||
| lazy val rResidualDegreeOfFreedomNull: Long = glm.summary.residualDegreeOfFreedomNull | ||
|
|
||
| lazy val rResidualDegreeOfFreedom: Long = glm.summary.residualDegreeOfFreedom | ||
|
|
||
| lazy val rAic: Double = glm.summary.aic | ||
|
|
||
| lazy val rNumIterations: Int = glm.summary.numIterations | ||
|
|
||
| lazy val rDevianceResiduals: DataFrame = glm.summary.residuals() | ||
|
|
||
| lazy val rFamily: String = glm.getFamily | ||
|
|
||
| def residuals(residualsType: String): DataFrame = glm.summary.residuals(residualsType) | ||
|
|
||
| def transform(dataset: Dataset[_]): DataFrame = { | ||
| pipeline.transform(dataset).drop(glm.getFeaturesCol) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this S3 function for formatted output of summary(GeneralizedLinearRegressionModel).