Accord.Tests.Statistics.ProportionalHazardsAnalysisTest.ComputeTest1 C# (CSharp) Method

ComputeTest1() private method

private ComputeTest1 ( ) : void
return void
        public void ComputeTest1()
        {
            // Consider the following example data, adapted from John C. Pezzullo's
            // example for his great Cox's proportional hazards model example in
            // JavaScript (http://statpages.org/prophaz2.html). 

            // In this data, we have three columns. The first column denotes the
            // input variables for the problem. The second column, the survival
            // times. And the last one is the output of the experiment (if the
            // subject has died [1] or has survived [0]).

            double[,] example =
            {
                // input  time censor
                {   50,    1,    0   },
                {   70,    2,    1   },
                {   45,    3,    0   },
                {   35,    5,    0   },
                {   62,    7,    1   },
                {   50,   11,    0   },
                {   45,    4,    0   },
                {   57,    6,    0   },
                {   32,    8,    0   },
                {   57,    9,    1   },
                {   60,   10,    1   },
            };

            // First we will extract the input, times and outputs
            double[,] inputs = example.GetColumns(new[] { 0 });
            double[] times = example.GetColumn(1);
            SurvivalOutcome[] output = example.GetColumn(2).To<SurvivalOutcome[]>();

            // Now we can proceed and create the analysis
            var cox = new ProportionalHazardsAnalysis(inputs, times, output);

            cox.Compute(); // compute the analysis

            // Now we can show an analysis summary
            // Accord.Controls.DataGridBox.Show(cox.Coefficients);


            // We can also investigate all parameters individually. For
            // example the coefficients values will be available at

            double[] coef = cox.CoefficientValues;
            double[] stde = cox.StandardErrors;

            // We can also obtain the hazards ratios
            double[] ratios = cox.HazardRatios;

            // And other information such as the partial
            // likelihood, the deviance and also make 
            // hypothesis tests on the parameters

            double partial = cox.LogLikelihood;
            double deviance = cox.Deviance;

            // Chi-Square for whole model
            ChiSquareTest chi = cox.ChiSquare;

            // Wald tests for individual parameters
            WaldTest wald = cox.Coefficients[0].Wald;


            // Finally, we can also use the model to predict
            // scores for new observations (without considering time)

            double y1 = cox.Regression.Compute(new double[] { 63 });
            double y2 = cox.Regression.Compute(new double[] { 32 });

            // Those scores can be interpreted by comparing then
            // to 1. If they are greater than one, the odds are
            // the patient will not survive. If the value is less
            // than one, the patient is likely to survive.

            // The first value, y1, gives approximately 86.138,
            // while the second value, y2, gives about 0.00072.


            // We can also consider instant estimates for a given time:
            double p1 = cox.Regression.Compute(new double[] { 63 }, 2);
            double p2 = cox.Regression.Compute(new double[] { 63 }, 10);

            // Here, p1 is the score after 2 time instants, with a 
            // value of 0.0656. The second value, p2, is the time
            // after 10 time instants, with a value of 6.2907.


            Assert.AreEqual(86.138421225296526, y1);
            Assert.AreEqual(0.00072281400325299814, y2);

            Assert.AreEqual(0.17989138010770425, p1);
            Assert.AreEqual(15.950244161356357, p2);

            Assert.AreEqual(1, coef.Length);
            Assert.AreEqual(0.37704239281490765, coef[0]);
            Assert.AreEqual(0.25415746361167235, stde[0]);
            Assert.AreEqual(1.4579661153488215, ratios[0]);

            Assert.AreEqual(-2.0252666205735466, partial, 1e-6);
            Assert.AreEqual(4.0505332411470931, deviance, 1e-6);

            Assert.AreEqual(0.13794183001851756, wald.PValue, 1e-4);

            Assert.AreEqual(1, chi.DegreesOfFreedom);
            Assert.AreEqual(7.3570, chi.Statistic, 1e-4);
            Assert.AreEqual(0.0067, chi.PValue, 1e-3);
        }