User Documentation
 All Files Functions Variables Groups
Cox-Proportional Hazards Regression

Proportional-Hazard models enable the comparison of various survival models. These survival models are functions describing the probability of a one-item event (prototypically, this event is death) with respect to time. The interval of time before death occurs is the survival time. Let T be a random variable representing the survival time, with a cumulative probability function P(t). Informally, P(t) is the probability that death has happened before time t.

Training Function

Following is the syntax for the coxph_train() training function:

coxph_train( source_table, 
             output_table, 
             dependent_variable, 
             independent_variable, 
             right_censoring_status, 
             strata, 
             optimizer_params
           )

Arguments

source_table
TEXT. The name of the table containing input data.
output_table

TEXT. The name of the table where the output model is saved. The output is saved in the table named by the output_table argument. It has the following columns:

coef FLOAT8[]. Vector of the coefficients.
loglikelihood FLOAT8. Log-likelihood value of the MLE estimate.
std_err FLOAT8[]. Vector of the standard error of the coefficients.
stats FLOAT8[]. Vector of the statistics of the coefficients.
p_values FLOAT8[]. Vector of the p-values of the coefficients.
hessian FLOAT8[]. The vectorized Hessian matrix computed using the final solution.
num_iterations INTEGER. The number of iterations performed by the optimizer.

Additionally, a summary output table is generated that contains a summary of the parameters used for building the Cox model. It is stored in a table named output_table_summary. It has the following columns:

source_table The source table name.
dep_var The dependent variable name.
ind_var The independent variable name.
right_censoring_status The right censoring status
strata The stratification columns

dependent_variable
TEXT. A string containing the name of a column that contains an array of numeric values, or a string expression in the format 'ARRAY[1, x1, x2, x3]', where x1, x2 and x3 are column names. Dependent variables refer to the time of death. There is no need to pre-sort the data.
independent_variable
TEXT. The name of the independent variable.
right_censoring_status (optional)
TEXT, default: TRUE for all observations. A string containing an expression that evaluates to the right-censoring status for the observation—TRUE if the observation is not censored and FALSE if the observation is censored. The string could contain the name of the column containing the right-censoring status, a fixed Boolean expression (i.e., 'true', 'false', '0', '1') that applies to all observations, or a Boolean expression such as 'column_name < 10' that can be evaluated for each observation.
strata (optional)
VARCHAR, default: NULL, which does not do any stratifications. A string of comma-separated column names that are the strata ID variables used to do stratification.
optimizer_params (optional)
VARCHAR, default: NULL, which uses the default values of optimizer parameters: max_iter=20, optimizer='newton', tolerance=1e-4. It should be a string that contains pairs of 'key=value' separated by commas.

Proportional Hazards Assumption Test Function

The cox_zph() function tests the proportional hazards assumption (PHA) of a Cox regression.

Proportional-hazard models enable the comparison of various survival models. These PH models, however, assume that the hazard for a given individual is a fixed proportion of the hazard for any other individual, and the ratio of the hazards is constant across time. MADlib does not currently have support for performing any transformation of the time to compute the correlation.

The cox_zph() function is used to test this assumption by computing the correlation of the residual of the coxph_train model with time.

Following is the syntax for the cox_zph() function:

cox_zph( cox_model_table, 
         output_table 
       )

Arguments

cox_model_table

TEXT. The name of the table containing the Cox Proportional-Hazards model.

output_table
TEXT. The name of the table where the test statistics are saved. The output table is named by the output_table argument and has the following columns:
rho FLOAT8[]. Vector of the correlation coefficients between survival time and the scaled Schoenfeld residuals.
chi_square FLOAT8[]. Chi-square test statistic for the correlation analysis.
p_value FLOAT8[]. Two-side p-value for the chi-square statistic.

Additionally, the residual values are outputted to the table named output_table_residual. The table contains the following columns:

time FLOAT8. Time values present in the original source table.
residual FLOAT8[]. Difference between the original covariate values and the expectation of the covariates obtained from the coxph_train model.
scaled_reisdual Residual values scaled by the variance of the coefficients.

Notes
  • Table names can be optionally schema qualified (current_schemas() is used if a schema name is not provided) and table and column names should follow case-sensitivity and quoting rules per the database. For instance, 'mytable' and 'MyTable' both resolve to the same entity—'mytable'. If mixed-case or multi-byte characters are desired for entity names then the string should be double-quoted; in this case the input would be '"MyTable"'.
  • The cox_prop_hazards_regr() and cox_prop_hazards() functions have been deprecated; coxph_train() should be used instead.

Examples
  1. View online help for the proportional hazards training method.
    SELECT madlib.coxph_train();
    
  2. Create an input data set.
    DROP TABLE IF EXISTS sample_data;
    CREATE TABLE sample_data (
        id INTEGER NOT NULL,
        grp DOUBLE PRECISION,
        wbc DOUBLE PRECISION,
        timedeath INTEGER,
        status BOOLEAN
    );
    COPY sample_data FROM STDIN DELIMITED BY '|';
      0 |   0 | 1.45 |        35 | t
      1 |   0 | 1.47 |        34 | t
      3 |   0 |  2.2 |        32 | t
      4 |   0 | 1.78 |        25 | t
      5 |   0 | 2.57 |        23 | t
      6 |   0 | 2.32 |        22 | t
      7 |   0 | 2.01 |        20 | t
      8 |   0 | 2.05 |        19 | t
      9 |   0 | 2.16 |        17 | t
     10 |   0 |  3.6 |        16 | t
     11 |   1 |  2.3 |        15 | t
     12 |   0 | 2.88 |        13 | t
     13 |   1 |  1.5 |        12 | t
     14 |   0 |  2.6 |        11 | t
     15 |   0 |  2.7 |        10 | t
     16 |   0 |  2.8 |         9 | t
     17 |   1 | 2.32 |         8 | t
     18 |   0 | 4.43 |         7 | t
     19 |   0 | 2.31 |         6 | t
     20 |   1 | 3.49 |         5 | t
     21 |   1 | 2.42 |         4 | t
     22 |   1 | 4.01 |         3 | t
     23 |   1 | 4.91 |         2 | t
     24 |   1 |    5 |         1 | t
    
    
  3. Run the Cox regression function.
    SELECT madlib.coxph_train( 'sample_data', 
                               'sample_cox', 
                               'timedeath', 
                               'ARRAY[grp,wbc]', 
                               'status'
                             );
    
  4. View the results of the regression.
    \x on
    SELECT * FROM sample_cox;
    
    Results:
    -[ RECORD 1 ]-----------------------------------------
    coef     | {2.54449137803027,1.67183255057665}
    std_err  | {0.677308807341768,0.387308633304678}
    z_stats  | {3.75676700265663,4.31653830257251}
    p_values | {0.000172122613528057,1.58495189046891e-05}
    
  5. View online help for the function to test Proportional Hazards Assumption.
    SELECT madlib.cox_zph();
    
  6. Run the test for Proportional Hazards assumption to obtain correlation between residuals and time.
    SELECT madlib.cox_zph( 'sample_cox', 
                           'sample_zph_output'
                         ); 
    
  7. View results of the PHA test.
    SELECT * FROM sample_zph_output;
    
    Results:
    covariate  | ARRAY[grp,wbc]
    rho        | {0.00237407653178531,0.0375603884069487}
    chi_square | {0.000100760403707082,0.0232322905624675}
    p_value    | {0.991991010621734,0.878854560410758}
    

Technical Background

Generally, proportional-hazard models start with a list of \( \boldsymbol n \) observations, each with \( \boldsymbol m \) covariates and a time of death. From this \( \boldsymbol n \times m \) matrix, we would like to derive the correlation between the covariates and the hazard function. This amounts to finding the parameters \( \boldsymbol \beta \) that best fit the model described below.

Let us define:

Note that this model does not include a constant term, and the data cannot contain a column of 1s.

By definition,

\[ P[T_k = t_i | \boldsymbol R(t_i)] = \frac{e^{\beta^T x_k} }{ \sum_{j \in R(t_i)} e^{\beta^T x_j}}. \,. \]

The partial likelihood function can now be generated as the product of conditional probabilities:

\[ \mathcal L = \prod_{i = 1}^n \left( \frac{e^{\beta^T x_i}}{ \sum_{j \in R(t_i)} e^{\beta^T x_j}} \right). \]

The log-likelihood form of this equation is

\[ L = \sum_{i = 1}^n \left[ \beta^T x_i - \log\left(\sum_{j \in R(t_i)} e^{\beta^T x_j }\right) \right]. \]

Using this score function and Hessian matrix, the partial likelihood can be maximized using the Newton-Raphson algorithm. Breslow's method is used to resolved tied times of deaths. The time of death for two records are considered "equal" if they differ by less than 1.0e-6

The inverse of the Hessian matrix, evaluated at the estimate of \( \boldsymbol \beta \), can be used as an approximate variance-covariance matrix for the estimate, and used to produce approximate standard errors for the regression coefficients.

\[ \mathit{se}(c_i) = \left( (H)^{-1} \right)_{ii} \,. \]

The Wald z-statistic is

\[ z_i = \frac{c_i}{\mathit{se}(c_i)} \,. \]

The Wald \( p \)-value for coefficient \( i \) gives the probability (under the assumptions inherent in the Wald test) of seeing a value at least as extreme as the one observed, provided that the null hypothesis ( \( c_i = 0 \)) is true. Letting \( F \) denote the cumulative density function of a standard normal distribution, the Wald \( p \)-value for coefficient \( i \) is therefore

\[ p_i = \Pr(|Z| \geq |z_i|) = 2 \cdot (1 - F( |z_i| )) \]

where \( Z \) is a standard normally distributed random variable.

The condition number is computed as \( \kappa(H) \) during the iteration immediately preceding convergence (i.e., \( A \) is computed using the coefficients of the previous iteration). A large condition number (say, more than 1000) indicates the presence of significant multicollinearity.

Literature

A somewhat random selection of nice write-ups, with valuable pointers into further literature:

[1] John Fox: Cox Proportional-Hazards Regression for Survival Data, Appendix to An R and S-PLUS companion to Applied Regression Feb 2012, http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-cox-regression.pdf

[2] Stephen J Walters: What is a Cox model? http://www.medicine.ox.ac.uk/bandolier/painres/download/whatis/cox_model.pdf

Related Topics

File cox_prop_hazards.sql_in documenting the functions