Example : KaplanMeierEstimates

The following example is taken from Kalbfleisch and Prentice (1980, page 1). The first column in x contains the death/censoring times for rats suffering from vaginal cancer. The second column contains information as to which of two forms of treatment were provided, while the third column contains the censoring code. Finally, the fourth column contains the frequency of each observation. The product-limit estimates of the survival probabilities are computed for both groups along with their standard error estimates. Tables containing these values along with other statistics are printed.
import java.text.*;
import com.imsl.stat.*;

public class  KaplanMeierEstimatesEx1 {
    public static void main(String args[]) {
        
        double[][] x = {
            {143, 5, 0, 1}, {164, 5, 0, 1}, {188, 5, 0, 2}, {190, 5, 0, 1},
            {192, 5, 0, 1}, {206, 5, 0, 1}, {209, 5, 0, 1}, {213, 5, 0, 1},
            {216, 5, 0, 1}, {220, 5, 0, 1}, {227, 5, 0, 1}, {230, 5, 0, 1},
            {234, 5, 0, 1}, {246, 5, 0, 1}, {265, 5, 0, 1}, {304, 5, 0, 1},
            {216, 5, 1, 1}, {244, 5, 1, 1}, {142, 7, 0, 1}, {156, 7, 0, 1},
            {163, 7, 0, 1}, {198, 7, 0, 1}, {205, 7, 0, 1}, {232, 7, 0, 2},
            {233, 7, 0, 4}, {239, 7, 0, 1}, {240, 7, 0, 1}, {261, 7, 0, 1},
            {280, 7, 0, 2}, {296, 7, 0, 2}, {323, 7, 0, 1}, {204, 7, 1, 1},
            {344, 7, 1, 1}
        };
        int nobs = x.length, censorIndex = 2, frequencyIndex = 3;
        int stratumIndex = 1, responseIndex = 0;
        int i, totalFailing, groupTotal, groupValue;
        int atRisk[] = new int[nobs];
        int nFailing[] = new int[nobs];
        double prob[] = new double[nobs];
        double se[] = new double[nobs];
        double logLikelihood;
        
        // Get Kaplan-Meir Estimates
        
        KaplanMeierEstimates km = new KaplanMeierEstimates(x);
        km.setCensorColumn(censorIndex);
        km.setFrequencyColumn(frequencyIndex);
        km.setStratumColumn(stratumIndex);
        atRisk = km.getNumberAtRisk();
        nFailing = km.getNumberOfFailures();
        prob = km.getSurvivalProbabilities();
        se = km.getStandardErrors();
        
        // Print Results
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(4);
        nf.setMinimumFractionDigits(4);
        i = 0;
        while(i < nobs){
            groupValue = (int) x[i][stratumIndex];
            System.out.println("\n       Kaplan-Meier Survival Probabilities");
            System.out.println("               For Group Value = "+groupValue);
            System.out.println("\nNumber     Number"
                    +"                Survival     Estimated");
            System.out.println("at risk    Failing     Time"
                    +"     Probability   Std. Error");
            while(i < nobs && ((int)x[i][stratumIndex] == groupValue)){
                if((int)x[i][censorIndex] != 1){
                    System.out.println("  "+atRisk[i]+"          "+nFailing[i]+"" +
                            "         "+((int)x[i][responseIndex])+"       "
                            +nf.format(prob[i])+"        "+nf.format(se[i]));
                }
                i ++;
            }
            System.out.println("\nTotal number in group = "+
                    km.getGroupTotal(groupValue));
            System.out.println("Total number failing = "+
                    km.getTotalNumberOfFailures(groupValue));
            System.out.println("Product Limit likelihood = "+
                    nf.format(km.getLogLikelihood(groupValue)));
        }
        System.out.println("\n\n\nThe number of rows of x with missing values is "+
                km.getNumberOfRowsMissing());
    }
}

Output


       Kaplan-Meier Survival Probabilities
               For Group Value = 5

Number     Number                Survival     Estimated
at risk    Failing     Time     Probability   Std. Error
  19          1         143       0.9474        0.0512
  18          1         164       0.8947        0.0704
  17          2         188       0.7895        0.0935
  15          1         190       0.7368        0.1010
  14          1         192       0.6842        0.1066
  13          1         206       0.6316        0.1107
  12          1         209       0.5789        0.1133
  11          1         213       0.5263        0.1145
  10          1         216       0.4737        0.1145
  8          1         220       0.4145        0.1145
  7          1         227       0.3553        0.1124
  6          1         230       0.2961        0.1082
  5          1         234       0.2368        0.1015
  3          1         246       0.1579        0.0934
  2          1         265       0.0789        0.0728
  1          1         304       0.0000        ?

Total number in group = 19
Total number failing = 17
Product Limit likelihood = -49.1692

       Kaplan-Meier Survival Probabilities
               For Group Value = 7

Number     Number                Survival     Estimated
at risk    Failing     Time     Probability   Std. Error
  21          1         142       0.9524        0.0465
  20          1         156       0.9048        0.0641
  19          1         163       0.8571        0.0764
  18          1         198       0.8095        0.0857
  16          1         205       0.7589        0.0941
  15          2         232       0.6577        0.1053
  13          4         233       0.4554        0.1114
  9          1         239       0.4048        0.1099
  8          1         240       0.3542        0.1072
  7          1         261       0.3036        0.1031
  6          2         280       0.2024        0.0902
  4          2         296       0.1012        0.0678
  2          1         323       0.0506        0.0493

Total number in group = 21
Total number failing = 19
Product Limit likelihood = -50.4277



The number of rows of x with missing values is 0
Link to Java source.