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.
using System;
using Imsl.Stat;

public class KaplanMeierEstimatesEx1
{
    public static void  Main(String[] args)
    {
        double[,] x = new double[,]{
            {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.GetLength(0), censorIndex = 2, frequencyIndex = 3;
        int stratumIndex = 1, responseIndex = 0;
        int i, groupValue;
        int[] atRisk = new int[nobs];
        int[] nFailing = new int[nobs];
        double[] prob = new double[nobs];
        double[] se = new double[nobs];
        
        // Get Kaplan-Meir Estimates
        
        KaplanMeierEstimates km = new KaplanMeierEstimates(x);
        km.CensorColumn = censorIndex;
        km.FrequencyColumn = frequencyIndex;
        km.StratumColumn = stratumIndex;
        atRisk = km.GetNumberAtRisk();
        nFailing = km.GetNumberOfFailures();
        prob = km.GetSurvivalProbabilities();
        se = km.GetStandardErrors();
        
        // Print Results
        i = 0;
        while (i < nobs)
        {
            groupValue = (int) x[i,stratumIndex];
            Console.Out.WriteLine("\n       Kaplan-Meier Survival "+
                "Probabilities");
            Console.Out.WriteLine("               For Group Value = "+
                "{0,5:0.####}", groupValue);
            Console.Out.WriteLine("\nNumber     Number" +
                     "                Survival     Estimated");
            Console.Out.WriteLine("at risk    Failing     Time" +
                "     Probability   Std. Error");
            while (i < nobs && ((int) x[i,stratumIndex] == groupValue))
            {
                if ((int) x[i,censorIndex] != 1)
                {
                    Console.Out.WriteLine("{0,5:0.####}    "+
                        "{1,5:0.####}        {2,5:0.####}       {3,5:0.####}"+
                        "       {4,5:0.####}",atRisk[i],nFailing[i],
                        ((int) x[i,responseIndex]),(prob[i]),(se[i]));
                }
                i++;
            }
            Console.Out.WriteLine("\nTotal number in group = "+
                "{0,5:0.####}",km.GetGroupTotal(groupValue));
            Console.Out.WriteLine("Total number failing = "+
                "{0,5:0.####}",km.GetTotalNumberOfFailures(groupValue));
            Console.Out.WriteLine("Product Limit likelihood = "+
                "{0,5:0.####}",km.GetLogLikelihood(groupValue));
        }
        Console.Out.WriteLine(
         "\n\n\nThe number of rows of x with missing values is {0,5:0.####}",
         km.NumberOfRowsMissing);
    }
}

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.101
   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         NaN

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 C# source.