public void LeukemiaExampleCensoring_FlemingHarrington_NelsonAalen()
        {
            // http://www-personal.umich.edu/~yili/lect2notes.pdf
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):

            double[] t = { 6, 6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35 };
            int[]    c = { 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 };

            var distribution = EmpiricalHazardDistribution.Estimate(t, c,
                                                                    SurvivalEstimator.FlemingHarrington, HazardEstimator.BreslowNelsonAalen);

            int[] intervals = { 6, 7, 9, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 34, 35 };

            double[] expected =
            {
                0.8571, 0.8067, 0.8067, 0.7529, 0.7529, 0.6902,
                0.6275, 0.6275, 0.6275, 0.6275, 0.5378, 0.4482,
                0.4482, 0.4482, 0.4482, 0.4482
            };

            for (int i = 0; i < intervals.Length; i++)
            {
                double x      = intervals[i];
                double actual = distribution.ComplementaryDistributionFunction(x);

                double e = expected[i];
                Assert.AreEqual(e, actual, 0.1);
            }
        }
        public void LeukemiaExampleCensoring_KaplanMeier_KaplanMeier()
        {
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):

            double[] t = { 6, 6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35 };
            int[]    c = { 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 };

            var distribution = EmpiricalHazardDistribution.Estimate(t, c,
                                                                    SurvivalEstimator.KaplanMeier, HazardEstimator.KaplanMeier);

            int[] intervals = { 6, 7, 9, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 34, 35 };

            double[] expected =
            {
                0.8571, 0.8067, 0.8067, 0.7529, 0.7529, 0.6902,
                0.6275, 0.6275, 0.6275, 0.6275, 0.5378, 0.4482,
                0.4482, 0.4482, 0.4482, 0.4482
            };

            for (int i = 0; i < intervals.Length; i++)
            {
                double x      = intervals[i];
                double actual = distribution.ComplementaryDistributionFunction(x);

                double e = expected[i];
                Assert.AreEqual(e, actual, 1e-4);
            }
        }
        public void KaplanMeierTest1()
        {
            // Example from
            // http://sas-and-r.blogspot.fr/2010/05/example-738-kaplan-meier-survival.html

            double[]          times;
            SurvivalOutcome[] censor;
            CreateExample1(out times, out censor);

            var distribution = EmpiricalHazardDistribution.Estimate(times, outcome: censor,
                                                                    survival: SurvivalEstimator.KaplanMeier, hazard: HazardEstimator.KaplanMeier);

            Assert.AreEqual(SurvivalEstimator.KaplanMeier, distribution.Estimator);

            int[]    t = { 1, 2, 3, 4, 6, 8, 9, 12, 14, 20 };
            double[] e = { 0.889, 0.833, 0.774, 0.714, 0.649, 0.577, 0.505, 0.421, 0.337, 0.168 };

            double[] actual = t.ToDouble().Apply(distribution.ComplementaryDistributionFunction);

            for (int i = 0; i < e.Length; i++)
            {
                Assert.AreEqual(e[i], actual[i], 1e-3);
            }

            // Assert.AreEqual(11.177, distribution.Mean);
            Assert.AreEqual(12, distribution.Median, 1e-5);
        }
        public void NelsonAalenTest1()
        {
            // Example from
            // http://sas-and-r.blogspot.fr/2010/05/example-738-kaplan-meier-survival.html
            // http://sas-and-r.blogspot.fr/2010/05/example-739-nelson-aalen-estimate-of.html

            double[]          times;
            SurvivalOutcome[] censor;
            CreateExample1(out times, out censor);

            // Test with Breslow method

            {
                var distribution = EmpiricalHazardDistribution.Estimate(times, censor, HazardTiesMethod.Breslow);

                double[] expectedCHF =
                {
                    0.0000000, 0.1111111, 0.1111111, 0.1736111, 0.1736111, 0.2450397, 0.3219628,
                    0.3219628, 0.4128719, 0.4128719, 0.5239830, 0.6489830, 0.6489830, 0.8156496,
                    1.0156496, 1.0156496, 1.0156496, 1.5156496, 1.5156496
                };

                double[] actualCHF = times.Apply(distribution.CumulativeHazardFunction);

                for (int i = 0; i < actualCHF.Length; i++)
                {
                    Assert.AreEqual(expectedCHF[i], actualCHF[i], 1e-6);
                }


                //Assert.AreEqual(11.177, distribution.Mean);
                Assert.AreEqual(12, distribution.Median, 1e-5);
            }

            // Test with Effron method
            {
                var distribution = EmpiricalHazardDistribution.Estimate(times, censor);

                double[] expectedCHF =
                {
                    0.0000000, 0.1111111, 0.1111111, 0.1756496, 0.1756496, 0.2497576, 0.3298003,
                    0.3298003, 0.4251104, 0.4251104, 0.5428935, 0.6764249, 0.6764249, 0.8587464,
                    1.0818900, 1.0818900, 1.0818900, 1.7750372, 1.7750372
                };

                double[] actualCHF = times.Apply(distribution.CumulativeHazardFunction);

                for (int i = 0; i < actualCHF.Length; i++)
                {
                    Assert.AreEqual(expectedCHF[i], actualCHF[i], 1e-6);
                }


                //Assert.AreEqual(11.177, distribution.Mean);
                Assert.AreEqual(12, distribution.Median, 1e-5);
            }
        }
        public void LeukemiaExample_KaplanMeier()
        {
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):
            // http://www-personal.umich.edu/~yili/lect2notes.pdf

            double[] t = { 1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23 };

            var distribution = EmpiricalHazardDistribution.Estimate(t,
                                                                    survival: SurvivalEstimator.KaplanMeier, hazard: HazardEstimator.KaplanMeier);

            Assert.AreEqual(1, distribution.Survivals[0]);
            Assert.AreEqual(0.905, distribution.Survivals[1], 1e-3);
            Assert.AreEqual(0.809, distribution.Survivals[2], 1e-3);
            Assert.AreEqual(0.762, distribution.Survivals[3], 1e-3);

            /*
             * http://statpages.org/prophaz2.html
             *  1, 1
             *  1, 1
             *  2, 1
             *  2, 1
             *  3, 1
             *  4, 1
             *  4, 1
             *  5, 1
             *  5, 1
             *  8, 1
             *  8, 1
             *  8, 1
             *  8, 1
             *  11, 1
             *  11, 1
             *  12, 1
             *  12, 1
             *  15, 1
             *  17, 1
             *  22, 1
             *  23, 1
             */
        }
        public void ConstructorTest1()
        {
            double[]          times;
            SurvivalOutcome[] censor;
            CreateExample1(out times, out censor);

            var distribution = EmpiricalHazardDistribution.Estimate(times, censor,
                                                                    SurvivalEstimator.FlemingHarrington, HazardEstimator.BreslowNelsonAalen);

            double[] t = distribution.Times;
            double[] s = distribution.Survivals;
            double[] h = distribution.Hazards;

            double[] nt = distribution.Times.Distinct();
            double[] nh = nt.Apply(distribution.HazardFunction);

            var target = new EmpiricalHazardDistribution(nt, nh, SurvivalEstimator.FlemingHarrington);

            for (int i = 0; i < times.Length; i++)
            {
                double expected = distribution.HazardFunction(times[i]);
                double actual   = target.HazardFunction(times[i]);
                Assert.AreEqual(expected, actual);
            }

            for (int i = 0; i < times.Length; i++)
            {
                double expected = distribution.CumulativeHazardFunction(times[i]);
                double actual   = target.CumulativeHazardFunction(times[i]);
                Assert.AreEqual(expected, actual, 1e-5);
            }

            for (int i = 0; i < times.Length; i++)
            {
                double expected = distribution.ProbabilityDensityFunction(times[i]);
                double actual   = target.ProbabilityDensityFunction(times[i]);
                Assert.AreEqual(expected, actual, 1e-5);
            }
        }
Example #7
0
        public void BaselineHazardTest()
        {
            double[,] data =
            {
                // t   c  in
                {  8, 0,  -1.2372626521865966 },
                {  4, 1,  0.22623087329625477 },
                { 12, 0,  -0.8288458543774289 },
                {  6, 0,  0.49850873850236665 },
                { 10, 0, -0.38639432341749696 },
                {  8, 1,   1.0430644689145904 },
                {  5, 0,  -1.6797141831465285 },
                {  5, 0,   1.0770992020653544 },
                {  3, 1,   1.0770992020653544 },
                { 14, 1, -0.38639432341749696 },
                {  8, 0,  -0.8969153206789568 },
                { 11, 0,   1.6897243987791061 },
                {  7, 0,  -1.2712973853373605 },
                {  7, 0, -0.38639432341749696 },
                {  7, 1, -0.45446378971902495 },
                { 12, 0,   0.4644740053516027 },
                {  8, 0,   1.4514812667237584 },
            };

            double[]          time   = data.GetColumn(0);
            SurvivalOutcome[] censor = data.GetColumn(1).To <SurvivalOutcome[]>();
            double[][]        inputs = data.GetColumn(2).ToJagged();

            var regression = new ProportionalHazards(1);

            var target = new ProportionalHazardsNewtonRaphson(regression);

            target.Normalize           = false;
            target.Lambda              = 0;
            regression.Coefficients[0] = 0.47983261821350764;

            double error = target.Run(inputs, time, censor);

            /* Tested against http://statpages.org/prophaz2.html
             *  13, 8,  0
             *  56, 4,  1
             *  25, 12, 0
             *  64, 6,  0
             *  38, 10, 0
             *  80, 8,  1
             *  0 , 5,  0
             *  81, 5,  0
             *  81, 3,  1
             *  38, 14, 1
             *  23, 8,  0
             *  99, 11, 0
             *  12, 7,  0
             *  38, 7,  0
             *  36, 7,  1
             *  63, 12, 0
             *  92, 8,  0
             */

            double[] baseline =
            {
                regression.Survival(3),  // 0.9465
                regression.Survival(4),  // 0.8919
                regression.Survival(7),  // 0.8231
                regression.Survival(8),  // 0.7436
                regression.Survival(12), // 0.7436
                regression.Survival(14), // 0.0000
            };

            Assert.AreEqual(0.9465, baseline[0], 1e-4);
            Assert.AreEqual(0.8919, baseline[1], 1e-4);
            Assert.AreEqual(0.8231, baseline[2], 1e-4);
            Assert.AreEqual(0.7436, baseline[3], 1e-4);
            Assert.AreEqual(0.7436, baseline[4], 1e-4);
            Assert.AreEqual(0.0000, baseline[5], 1e-4);

            // The value of the baseline must be exact the same if it was computed
            // after the Newton-Raphson or in a standalone EmpiricalHazard computation
            double[] outputs   = inputs.Apply(x => regression.Compute(x));
            var      empirical = EmpiricalHazardDistribution.Estimate(time, censor, outputs);

            baseline = new[]
            {
                empirical.ComplementaryDistributionFunction(3),  // 0.9465
                empirical.ComplementaryDistributionFunction(4),  // 0.8919
                empirical.ComplementaryDistributionFunction(7),  // 0.8231
                empirical.ComplementaryDistributionFunction(8),  // 0.7436
                empirical.ComplementaryDistributionFunction(12), // 0.7436
                empirical.ComplementaryDistributionFunction(14), // 0.0000
            };

            Assert.AreEqual(0.9465, baseline[0], 1e-4);
            Assert.AreEqual(0.8919, baseline[1], 1e-4);
            Assert.AreEqual(0.8231, baseline[2], 1e-4);
            Assert.AreEqual(0.7436, baseline[3], 1e-4);
            Assert.AreEqual(0.7436, baseline[4], 1e-4);
            Assert.AreEqual(0.0000, baseline[5], 1e-4);
        }