-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-13010] [ML] [SparkR] Implement a simple wrapper of AFTSurvivalRegression in SparkR #11447
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
81cd88c
96fe3c2
4ccb172
e760527
dbc1077
900c85f
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 |
|---|---|---|
|
|
@@ -61,6 +61,34 @@ setMethod("glm", signature(formula = "formula", family = "ANY", data = "DataFram | |
| return(new("PipelineModel", model = model)) | ||
| }) | ||
|
|
||
| #' Fit an accelerated failure time (AFT) survival regression model. | ||
| #' | ||
| #' Fit an accelerated failure time (AFT) survival regression model, similarly to R's survreg(). | ||
| #' | ||
| #' @param formula A symbolic description of the model to be fitted. Currently only a few formula | ||
| #' operators are supported, including '~', ':', '+', and '-'. | ||
| #' @param data DataFrame for training. | ||
| #' @return a fitted MLlib model | ||
|
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. provide a |
||
| #' @rdname survreg | ||
| #' @export | ||
| #' @examples | ||
| #'\dontrun{ | ||
| #' sc <- sparkR.init() | ||
| #' sqlContext <- sparkRSQL.init(sc) | ||
| #' library(survival) | ||
| #' data(ovarian) | ||
| #' df <- createDataFrame(sqlContext, ovarian) | ||
| #' model <- survreg(Surv(futime, fustat) ~ ecog_ps + rx, df) | ||
| #' summary(model) | ||
| #'} | ||
| setMethod("survreg", signature(formula = "formula", data = "DataFrame"), | ||
|
||
| function(formula, data) { | ||
| formula <- paste(deparse(formula), collapse = "") | ||
| model <- callJStatic("org.apache.spark.ml.api.r.SparkRWrappers", | ||
| "fitAFTSurvivalRegression", formula, data@sdf) | ||
| return(new("PipelineModel", model = model)) | ||
| }) | ||
|
|
||
| #' Make predictions from a model | ||
| #' | ||
| #' Makes predictions from a model produced by glm(), similarly to R's predict(). | ||
|
|
@@ -135,6 +163,11 @@ setMethod("summary", signature(object = "PipelineModel"), | |
| colnames(coefficients) <- unlist(features) | ||
| rownames(coefficients) <- 1:k | ||
| return(list(coefficients = coefficients, size = size, cluster = dataFrame(cluster))) | ||
| } else if (modelName == "AFTSurvivalRegressionModel") { | ||
| coefficients <- as.matrix(unlist(coefficients)) | ||
| colnames(coefficients) <- c("Value") | ||
| rownames(coefficients) <- unlist(features) | ||
| return(list(coefficients = coefficients)) | ||
| } else { | ||
| stop(paste("Unsupported model", modelName, sep = " ")) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,3 +141,24 @@ test_that("kmeans", { | |
| cluster <- summary.model$cluster | ||
| expect_equal(sort(collect(distinct(select(cluster, "prediction")))$prediction), c(0, 1)) | ||
| }) | ||
|
|
||
| test_that("SparkR::survreg vs survival::survreg", { | ||
| library(survival) | ||
|
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. Q: Would the test fail if we don't have
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. No, because we have added required library at
Member
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. actually,
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. I agree, we should not require |
||
| data(ovarian) | ||
| df <- suppressWarnings(createDataFrame(sqlContext, ovarian)) | ||
|
|
||
| model <- SparkR::survreg(Surv(futime, fustat) ~ ecog_ps + rx, df) | ||
| stats <- summary(model) | ||
| coefs <- as.vector(stats$coefficients[, 1][1:3]) | ||
| scale <- exp(stats$coefficients[, 1][4]) | ||
|
|
||
| rModel <- survival::survreg(Surv(futime, fustat) ~ ecog.ps + rx, ovarian) | ||
| rCoefs <- as.vector(coef(rModel)) | ||
| rScale <- rModel$scale | ||
|
|
||
| expect_true(all(abs(rCoefs - coefs) < 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.
|
||
| expect_true(abs(rScale - scale) < 1e-4) | ||
| expect_true(all( | ||
| rownames(stats$coefficients) == | ||
| c("(Intercept)", "ecog_ps", "rx", "Log(scale)"))) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,12 +17,13 @@ | |
|
|
||
| package org.apache.spark.ml.api.r | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.ml.{Pipeline, PipelineModel} | ||
| import org.apache.spark.ml.attribute._ | ||
| import org.apache.spark.ml.classification.{LogisticRegression, LogisticRegressionModel} | ||
| import org.apache.spark.ml.clustering.{KMeans, KMeansModel} | ||
| import org.apache.spark.ml.feature.{RFormula, VectorAssembler} | ||
| import org.apache.spark.ml.regression.{LinearRegression, LinearRegressionModel} | ||
| import org.apache.spark.ml.regression._ | ||
| import org.apache.spark.sql.DataFrame | ||
|
|
||
| private[r] object SparkRWrappers { | ||
|
|
@@ -52,6 +53,43 @@ private[r] object SparkRWrappers { | |
| pipeline.fit(df) | ||
| } | ||
|
|
||
| def fitAFTSurvivalRegression( | ||
| value: String, | ||
|
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. change |
||
| df: DataFrame): PipelineModel = { | ||
|
|
||
| def formulaRewrite(value: String): (String, String) = { | ||
|
||
| var rewrited: String = null | ||
| var censorCol: String = null | ||
|
|
||
| val regex = "^Surv\\s*\\(([^,]+),([^,]+)\\)\\s*\\~\\s*(.+)".r | ||
|
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. Use triple quotes to avoid escaping |
||
| try { | ||
| val regex(label, censor, features) = value | ||
| // TODO: Support dot operator. | ||
| if (features.contains(".")) { | ||
| throw new UnsupportedOperationException( | ||
| "Terms of survreg formula can not support dot operator.") | ||
|
||
| } | ||
| rewrited = label.trim + "~" + features | ||
| censorCol = censor.trim | ||
| } catch { | ||
| case e: MatchError => | ||
| throw new SparkException(s"Could not parse formula: $value") | ||
| } | ||
|
|
||
| (rewrited, censorCol) | ||
| } | ||
|
|
||
| val (rewritedValue, censorCol) = formulaRewrite(value) | ||
|
|
||
| val formula = new RFormula().setFormula(rewritedValue) | ||
| val estimator = new AFTSurvivalRegression() | ||
| .setCensorCol(censorCol) | ||
| .setFitIntercept(formula.hasIntercept) | ||
|
|
||
| val pipeline = new Pipeline().setStages(Array(formula, estimator)) | ||
| pipeline.fit(df) | ||
| } | ||
|
|
||
| def fitKMeans( | ||
| df: DataFrame, | ||
| initMode: String, | ||
|
|
@@ -91,6 +129,12 @@ private[r] object SparkRWrappers { | |
| } | ||
| case m: KMeansModel => | ||
| m.clusterCenters.flatMap(_.toArray) | ||
| case m: AFTSurvivalRegressionModel => | ||
| if (m.getFitIntercept) { | ||
| Array(m.intercept) ++ m.coefficients.toArray ++ Array(math.log(m.scale)) | ||
| } else { | ||
| m.coefficients.toArray ++ Array(math.log(m.scale)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -151,6 +195,14 @@ private[r] object SparkRWrappers { | |
| val attrs = AttributeGroup.fromStructField( | ||
| m.summary.predictions.schema(m.summary.featuresCol)) | ||
| attrs.attributes.get.map(_.name.get) | ||
| case m: AFTSurvivalRegressionModel => | ||
| val attrs = AttributeGroup.fromStructField( | ||
| m.summary.predictions.schema(m.getFeaturesCol)) | ||
| if (m.getFitIntercept) { | ||
| Array("(Intercept)") ++ attrs.attributes.get.map(_.name.get) ++ Array("Log(scale)") | ||
| } else { | ||
| attrs.attributes.get.map(_.name.get) ++ Array("Log(scale)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -162,6 +214,8 @@ private[r] object SparkRWrappers { | |
| "LogisticRegressionModel" | ||
| case m: KMeansModel => | ||
| "KMeansModel" | ||
| case m: AFTSurvivalRegressionModel => | ||
| "AFTSurvivalRegressionModel" | ||
| } | ||
| } | ||
| } | ||
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.
Document that
.is not supported.