SPARK2 Survival Analysis Survival regression

Source: Internet
Author: User
Tags spark mllib

In Spark.ml, an accelerated failure time (AFT) model is implemented, which is a parametric survival regression model for checking data. It describes the model of the time-to-live logarithm, so it is often referred to as a logarithmic linear model of survival analysis. Unlike proportional risk models designed for the same purpose, aft models are more likely to parallelize because each instance contributes independently to the target function.

When a aftsurvivalregressionmodel is matched on a dataset with a constant non-0 column without a intercept, Spark mllib outputs 0 coefficients for a constant non-0 column. This behavior differs from R survival:: Survreg.

Importing packages

Import Org.apache.spark.sql.SparkSessionimport Org.apache.spark.sql.Datasetimport Org.apache.spark.sql.Rowimport Org.apache.spark.sql.DataFrameimport Org.apache.spark.sql.Columnimport Org.apache.spark.sql.DataFrameReaderimport Org.apache.spark.rdd.RDDimport Org.apache.spark.sql.catalyst.encoders.ExpressionEncoderimport Org.apache.spark.sql.Encoderimport Org.apache.spark.sql.DataFrameStatFunctionsimport Org.apache.spark.sql.functions._import Org.apache.spark.ml.linalg.Vectorsimport Org.apache.spark.ml.regression.AFTSurvivalRegressionimport Org.apache.spark.ml.feature.VectorAssembler

Modeling

    Val spark = Sparksession.builder (). AppName ("Spark survival regression"). config ("spark.some.config.option", "    Some-value "). Getorcreate ()//For implicit conversions as converting RDDs to dataframes import spark.implicits._       Val datalist:list[(double, double, double, double)] = List ((2, 51, 1, 1), (2, 58, 1, 1), (2, 55, 2, 1),  (2, 28, 22, 1), (1, 21, 30, 0), (1, 19, 28, 1), (2, 25, 32, 1), (2, 48, 11, 1), (2, 47, 14,  1), (2, 25, 36, 0), (2, 31, 31, 0), (1, 24, 33, 0), (1, 25, 33, 0), (2, 30, 37, 0), (2, 33,  35, 0), (1, 36, 25, 1), (1, 30, 31, 0), (1, 41, 22, 1), (2, 43, 26, 1), (2, 45, 24, 1), (2, 0), (1, +, 0), (1, +, 0), (1,,, 1), (2,,, 1), (1,, 0)) v Al data = DATALIST.TODF ("Sex", "Age", "label", "censor"). The ("label") val Colarray = Array ("Sex", "age") Val- Embler = new Vectorassembler (). SetInputcols (Colarray). Setoutputcol ("Features") Val vecdf:dataframe = assembler.transform (data) val aft = new Aftsurvi  Valregression () Val model = Aft.fit (VECDF)//Print the coefficients, intercept and scale parameter for aft survival Regression println (S "coefficients: ${model.coefficients} Intercept:" + S "${model.intercept} scale: ${model.scale } ") Val Array (coeff1, coeff2) = Model.coefficients.toArray val intercept:double = model.intercept val Scale:dou ble = model.scale val aftdf = Model.transform (VECDF)//Risk rate H (t) aftdf.selectexpr ("Sex", "Age", "label", "CENs Or "," features "," Round (prediction,2) as prediction ", S" Round (exp (sex* $coeff 1+age* $coeff $intercept), 2) as H (t) "). (" label "). Show (False)

SPARK2 Survival Analysis Survival regression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.