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

example_learn() private method

private example_learn ( ) : void
return void
        public void example_learn()
        {
            #region doc_learn_part1
            // Suppose we have the following data about some patients.
            // The first variable is continuous and represent patient
            // age. The second variable is dichotomic and give whether
            // they smoke or not (this is completely fictional data).

            double[][] inputs =
            {
                //            Age  Smoking
                new double[] { 55,    0   }, 
                new double[] { 28,    0   }, 
                new double[] { 65,    1   }, 
                new double[] { 46,    0   }, 
                new double[] { 86,    1   }, 
                new double[] { 56,    1   }, 
                new double[] { 85,    0   }, 
                new double[] { 33,    0   }, 
                new double[] { 21,    1   }, 
                new double[] { 42,    1   }, 
            };

            // Additionally, we also have information about whether
            // or not they those patients had lung cancer. The array
            // below gives 0 for those who did not, and 1 for those
            // who did.

            double[] output =
            {
                0, 0, 0, 1, 1, 1, 0, 0, 0, 1
            };

            // Create a Logistic Regression analysis
            var lra = new LogisticRegressionAnalysis()
            {
                Regularization = 0
            };

            // compute the analysis
            LogisticRegression regression = lra.Learn(inputs, output);

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

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

            double[] coef = lra.CoefficientValues;

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

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

            // We can use the analysis to predict a score for a new patient:
            double y = lra.Regression.Score(new double[] { 87, 1 }); // 0.75

            // For those inputs, the answer probability is approximately 75%.

            // We can also obtain confidence intervals for the probability:
            DoubleRange ci = lra.GetConfidenceInterval(new double[] { 87, 1 });
            #endregion

            Assert.AreEqual(0.085627701183146374, odds[0], 1e-8);
            Assert.AreEqual(1.0208597029292648, odds[1], 1e-8);
            Assert.AreEqual(5.8584748981777919, odds[2], 1e-8);

            Assert.AreEqual(2.1590686019473897, stde[0], 1e-8);
            Assert.AreEqual(0.033790422321041035, stde[1], 1e-8);
            Assert.AreEqual(1.4729903935788211, stde[2], 1e-8);

            Assert.AreEqual(0.75143272858389798, y, 1e-8);
            Assert.AreEqual(0.079591541770048527, ci.Min, 1e-8);
            Assert.AreEqual(0.99062645401700389, ci.Max, 1e-8);
        }