Accord.Tests.Statistics.LogisticRegressionAnalysisTest.FromSummaryTest1 C# (CSharp) Method

FromSummaryTest1() private method

private FromSummaryTest1 ( ) : void
return void
        public void FromSummaryTest1()
        {
            // Suppose we have a (fictitious) data set about patients who 
            // underwent cardiac surgery. The first column gives the number
            // of arterial bypasses performed during the surgery. The second
            // column gives the number of patients whose surgery went well,
            // while the third column gives the number of patients who had
            // at least one complication during the surgery.
            // 
            int[,] data =
            {
                // # of stents       success     complications
                {       1,             140,           45       },
                {       2,             130,           60       },
                {       3,             150,           31       },
                {       4,              96,           65       }
            };


            double[][] inputs = data.GetColumn(0).ToDouble().ToJagged();

            int[] positive = data.GetColumn(1);
            int[] negative = data.GetColumn(2);

            // Create a new Logistic Regression Analysis from the summary data
            var regression = LogisticRegressionAnalysis.FromSummary(inputs, positive, negative);

            regression.Compute(); // compute the analysis.

            // Now we can show a summary of the analysis
            // Accord.Controls.DataGridBox.Show(regression.Coefficients);


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

            double[] coef = regression.CoefficientValues;

            // The first value refers to the model's intercept term. We
            // can also retrieve the odds ratios and standard errors:

            double[] odds = regression.OddsRatios;
            double[] stde = regression.StandardErrors;


            // Finally, we can use it to estimate risk for a new patient
            double y = regression.Regression.Compute(new double[] { 4 });

            Assert.AreEqual(3.7586367581050162, odds[0], 1e-8);
            Assert.AreEqual(0.85772731075090014, odds[1], 1e-8);

            Assert.AreEqual(0.20884336554629004, stde[0], 1e-8);
            Assert.AreEqual(0.075837785246620285, stde[1], 1e-8);

            Assert.AreEqual(0.67044096045332713, y, 1e-8);

            LogisticRegressionAnalysis expected;


            {
                int[] qtr = data.GetColumn(0);

                var expanded = Accord.Statistics.Tools.Expand(qtr, positive, negative);

                double[][] inp = expanded.GetColumn(0).ToDouble().ToJagged();
                double[] outputs = expanded.GetColumn(1).ToDouble();

                expected = new LogisticRegressionAnalysis(inp, outputs);

                expected.Compute();

                double slope = expected.Coefficients[1].Value; // should return -0.153
                double inter = expected.Coefficients[0].Value;
                double value = expected.ChiSquare.PValue;      // should return 0.042
                Assert.AreEqual(-0.15346904821339602, slope, 1e-8);
                Assert.AreEqual(1.324056323049271, inter, 1e-8);
                Assert.AreEqual(0.042491262992507946, value, 1e-8);
            }



            var actual = regression;
            Assert.AreEqual(expected.Coefficients[0].Value, actual.Coefficients[0].Value, 1e-8);
            Assert.AreEqual(expected.Coefficients[1].Value, actual.Coefficients[1].Value, 1e-8);

            Assert.AreEqual(expected.ChiSquare.PValue, actual.ChiSquare.PValue, 1e-8);
            Assert.AreEqual(expected.WaldTests[0].PValue, actual.WaldTests[0].PValue, 1e-8);
            Assert.AreEqual(expected.WaldTests[1].PValue, actual.WaldTests[1].PValue, 1e-8);

            Assert.AreEqual(expected.Confidences[0].Max, actual.Confidences[0].Max, 1e-6);
            Assert.AreEqual(expected.Confidences[0].Min, actual.Confidences[0].Min, 1e-6);
            Assert.AreEqual(expected.Confidences[1].Max, actual.Confidences[1].Max, 1e-6);
            Assert.AreEqual(expected.Confidences[1].Min, actual.Confidences[1].Min, 1e-6);
        }