private void computeInformation(double[][] inputData, double[] outputData, double[] weights) { // Store model information #pragma warning disable 612, 618 this.result = regression.Compute(inputData); #pragma warning restore 612, 618 this.NumberOfSamples = inputData.Length; if (weights == null) { this.deviance = regression.GetDeviance(inputData, outputData); this.logLikelihood = regression.GetLogLikelihood(inputData, outputData); this.chiSquare = regression.ChiSquare(inputData, outputData); } else { this.deviance = regression.GetDeviance(inputData, outputData, weights); this.logLikelihood = regression.GetLogLikelihood(inputData, outputData, weights); this.chiSquare = regression.ChiSquare(inputData, outputData, weights); } // Store coefficient information for (int i = 0; i < regression.NumberOfParameters; i++) { this.standardErrors[i] = regression.StandardErrors[i]; this.waldTests[i] = regression.GetWaldTest(i); this.coefficients[i] = regression.GetCoefficient(i); this.confidences[i] = regression.GetConfidenceInterval(i); this.oddsRatios[i] = regression.GetOddsRatio(i); } }
private void computeInformation() { // Store model information this.result = regression.Compute(inputData); if (weights == null) { this.deviance = regression.GetDeviance(inputData, outputData); this.logLikelihood = regression.GetLogLikelihood(inputData, outputData); this.chiSquare = regression.ChiSquare(inputData, outputData); } else { this.deviance = regression.GetDeviance(inputData, outputData, weights); this.logLikelihood = regression.GetLogLikelihood(inputData, outputData, weights); this.chiSquare = regression.ChiSquare(inputData, outputData, weights); } // Store coefficient information for (int i = 0; i < regression.Coefficients.Length; i++) { this.standardErrors[i] = regression.StandardErrors[i]; this.waldTests[i] = regression.GetWaldTest(i); this.coefficients[i] = regression.Coefficients[i]; this.confidences[i] = regression.GetConfidenceInterval(i); this.oddsRatios[i] = regression.GetOddsRatio(i); } }
/// <summary> /// Computes the Logistic Regression Analysis. /// </summary> /// <remarks>The likelihood surface for the /// logistic regression learning is convex, so there will be only one /// peak. Any local maxima will be also a global maxima. /// </remarks> /// <param name="limit"> /// The difference between two iterations of the regression algorithm /// when the algorithm should stop. If not specified, the value of /// 10e-4 will be used. The difference is calculated based on the largest /// absolute parameter change of the regression. /// </param> /// <param name="maxIterations"> /// The maximum number of iterations to be performed by the regression /// algorithm. /// </param> /// <returns> /// True if the model converged, false otherwise. /// </returns> /// public bool Compute(double limit, int maxIterations) { double delta; int iteration = 0; do // learning iterations until convergence { delta = regression.Regress(inputData, outputData); iteration++; } while (delta > limit && iteration < maxIterations); // Check if the full model has converged bool converged = iteration <= maxIterations; // Store model information this.result = regression.Compute(inputData); this.deviance = regression.GetDeviance(inputData, outputData); this.logLikelihood = regression.GetLogLikelihood(inputData, outputData); this.chiSquare = regression.ChiSquare(inputData, outputData); // Store coefficient information for (int i = 0; i < regression.Coefficients.Length; i++) { this.waldTests[i] = regression.GetWaldTest(i); this.standardErrors[i] = regression.GetStandardError(i); this.coefficients[i] = regression.Coefficients[i]; this.confidences[i] = regression.GetConfidenceInterval(i); this.oddsRatios[i] = regression.GetOddsRatio(i); } // Perform likelihood-ratio tests against diminished nested models for (int i = 0; i < inputCount; i++) { // Create a diminished inner model without the current variable double[][] data = inputData.RemoveColumn(i); LogisticRegression inner = new LogisticRegression(inputCount - 1); iteration = 0; do // learning iterations until convergence { delta = inner.Regress(data, outputData); iteration++; } while (delta > limit && iteration < maxIterations); double ratio = 2.0 * (logLikelihood - inner.GetLogLikelihood(data, outputData)); ratioTests[i + 1] = new ChiSquareTest(ratio, 1); } // Returns true if the full model has converged, false otherwise. return(converged); }
/// <summary> /// Constructs a new Logistic regression model. /// </summary> /// internal StepwiseLogisticRegressionModel(StepwiseLogisticRegressionAnalysis analysis, LogisticRegression regression, int[] variables, ChiSquareTest chiSquare, ChiSquareTest[] tests) { this.Analysis = analysis; this.Regression = regression; int coefficientCount = regression.NumberOfInputs + 1; this.Inputs = analysis.Inputs.Get(variables); this.ChiSquare = chiSquare; this.LikelihoodRatioTests = tests; this.Variables = variables; this.StandardErrors = new double[coefficientCount]; this.WaldTests = new WaldTest[coefficientCount]; this.CoefficientValues = new double[coefficientCount]; this.Confidences = new DoubleRange[coefficientCount]; this.OddsRatios = new double[coefficientCount]; // Store coefficient information for (int i = 0; i < regression.NumberOfInputs + 1; i++) { this.StandardErrors[i] = regression.StandardErrors[i]; this.WaldTests[i] = regression.GetWaldTest(i); this.CoefficientValues[i] = regression.GetCoefficient(i); this.Confidences[i] = regression.GetConfidenceInterval(i); this.OddsRatios[i] = regression.GetOddsRatio(i); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < Inputs.Length; i++) { sb.Append(Inputs[i]); if (i < Inputs.Length - 1) { sb.Append(", "); } } this.Names = sb.ToString(); var logCoefs = new List <NestedLogisticCoefficient>(coefficientCount); for (int i = 0; i < coefficientCount; i++) { logCoefs.Add(new NestedLogisticCoefficient(this, i)); } this.Coefficients = new NestedLogisticCoefficientCollection(logCoefs); }
public void learn_new_mechanism() { Accord.Math.Random.Generator.Seed = 0; #region doc_log_reg_1 // 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). // We also know if they have had lung cancer or not, and // we would like to know whether smoking has any connection // with lung cancer (This is completely fictional data). double[][] input = { // age, smokes?, had cancer? new double[] { 55, 0 }, // false - no cancer new double[] { 28, 0 }, // false new double[] { 65, 1 }, // false new double[] { 46, 0 }, // true - had cancer new double[] { 86, 1 }, // true new double[] { 56, 1 }, // true new double[] { 85, 0 }, // false new double[] { 33, 0 }, // false new double[] { 21, 1 }, // false new double[] { 42, 1 }, // true }; bool[] output = // Whether each patient had lung cancer or not { false, false, false, true, true, true, false, false, false, true }; // To verify this hypothesis, we are going to create a logistic // regression model for those two inputs (age and smoking), learned // using a method called "Iteratively Reweighted Least Squares": var learner = new IterativeReweightedLeastSquares <LogisticRegression>() { Tolerance = 1e-4, // Let's set some convergence parameters Iterations = 100, // maximum number of iterations to perform Regularization = 0 }; // Now, we can use the learner to finally estimate our model: LogisticRegression regression = learner.Learn(input, output); // At this point, we can compute the odds ratio of our variables. // In the model, the variable at 0 is always the intercept term, // with the other following in the sequence. Index 1 is the age // and index 2 is whether the patient smokes or not. // For the age variable, we have that individuals with // higher age have 1.021 greater odds of getting lung // cancer controlling for cigarette smoking. double ageOdds = regression.GetOddsRatio(1); // 1.0208597028836701 // For the smoking/non smoking category variable, however, we // have that individuals who smoke have 5.858 greater odds // of developing lung cancer compared to those who do not // smoke, controlling for age (remember, this is completely // fictional and for demonstration purposes only). double smokeOdds = regression.GetOddsRatio(2); // 5.8584748789881331 // We can also obtain confidence intervals for the odd ratios: DoubleRange ageRange = regression.GetConfidenceInterval(1); // { 0.955442466180864, 1.09075592717851 } DoubleRange smokeRange = regression.GetConfidenceInterval(2); // { 0.326598216009923, 105.088535240304 } // If we would like to use the model to predict a probability for // each patient regarding whether they are at risk of cancer or not, // we can use the Probability function: double[] scores = regression.Probability(input); // Finally, if we would like to arrive at a conclusion regarding // each patient, we can use the Decide method, which will transform // the probabilities (from 0 to 1) into actual true/false values: bool[] actual = regression.Decide(input); #endregion double[] expected = { 0.21044171509541, 0.132425274863516, 0.657478034489772, 0.181224847711481, 0.747556618035989, 0.614500418479497, 0.331167053803838, 0.144741108525755, 0.436271096256738, 0.544193832738005 }; string str = scores.ToCSharp(); for (int i = 0; i < scores.Length; i++) { Assert.AreEqual(expected[i], scores[i], 1e-8); } double[] transform = regression.Transform(input, scores); for (int i = 0; i < scores.Length; i++) { Assert.AreEqual(expected[i], transform[i], 1e-8); } Assert.AreEqual(1.0208597028836701, ageOdds, 1e-10); Assert.AreEqual(5.8584748789881331, smokeOdds, 1e-6); Assert.AreEqual(-2.4577464307294092, regression.Intercept, 1e-8); Assert.AreEqual(-2.4577464307294092, regression.Coefficients[0], 1e-8); Assert.AreEqual(0.020645118265359252, regression.Coefficients[1], 1e-10); Assert.AreEqual(1.7678893101571855, regression.Coefficients[2], 1e-8); Assert.IsTrue(new[] { 0.955442466180864, 1.09075592717851 }.IsEqual(ageRange, atol: 1e-10)); Assert.IsTrue(new[] { 0.326598216009923, 105.088535240304 }.IsEqual(smokeRange, atol: 1e-10)); Assert.IsFalse(actual[0]); Assert.IsFalse(actual[1]); Assert.IsTrue(actual[2]); Assert.IsFalse(actual[3]); Assert.IsTrue(actual[4]); Assert.IsTrue(actual[5]); Assert.IsFalse(actual[6]); Assert.IsFalse(actual[7]); Assert.IsFalse(actual[8]); Assert.IsTrue(actual[9]); }