internal NestedLogisticCoefficient(StepwiseLogisticRegressionModel analysis, int index)
 {
     this.analysis = analysis;
     this.index    = index;
 }
        /// <summary>
        ///   Computes one step of the Stepwise Logistic Regression Analysis.
        /// </summary>
        /// <returns>
        ///   Returns the index of the variable discarded in the step or -1
        ///   in case no variable could be discarded.
        /// </returns>
        ///
        public int DoStep()
        {
            ChiSquareTest[] tests = null;

            // Check if we are performing the first step
            if (currentModel == null)
            {
                // This is the first step. We should create the full model.
                int inputCount = inputData[0].Length;
                LogisticRegression regression = new LogisticRegression(inputCount);
                int[] variables = Matrix.Indices(0, inputCount);

                fit(regression, inputData, outputData);

                ChiSquareTest test = regression.ChiSquare(inputData, outputData);
                fullLikelihood = regression.GetLogLikelihood(inputData, outputData);

                if (Double.IsNaN(fullLikelihood))
                {
                    throw new ConvergenceException(
                              "Perfect separation detected. Please rethink the use of logistic regression.");
                }

                tests         = new ChiSquareTest[regression.Coefficients.Length];
                currentModel  = new StepwiseLogisticRegressionModel(this, regression, variables, test, tests);
                completeModel = currentModel;
            }


            // Verify first if a variable reduction is possible
            if (currentModel.Regression.Inputs == 1)
            {
                return(-1); // cannot reduce further
            }
            // Now go and create the diminished nested models
            var nestedModels = new StepwiseLogisticRegressionModel[currentModel.Regression.Inputs];

            for (int i = 0; i < nestedModels.Length; i++)
            {
                // Create a diminished nested model without the current variable
                LogisticRegression regression = new LogisticRegression(currentModel.Regression.Inputs - 1);
                int[]      variables          = currentModel.Variables.RemoveAt(i);
                double[][] subset             = inputData.Submatrix(0, inputData.Length - 1, variables);

                fit(regression, subset, outputData);

                // Check the significance of the nested model
                double logLikelihood = regression.GetLogLikelihood(subset, outputData);
                double ratio         = 2.0 * (fullLikelihood - logLikelihood);

                ChiSquareTest test = new ChiSquareTest(ratio, inputNames.Length - variables.Length)
                {
                    Size = threshold
                };

                if (tests != null)
                {
                    tests[i + 1] = test;
                }

                // Store the nested model
                nestedModels[i] = new StepwiseLogisticRegressionModel(this, regression, variables, test, null);
            }

            // Select the model with the highest p-value
            double pmax = 0; int imax = -1;

            for (int i = 0; i < nestedModels.Length; i++)
            {
                if (nestedModels[i].ChiSquare.PValue >= pmax)
                {
                    imax = i;
                    pmax = nestedModels[i].ChiSquare.PValue;
                }
            }

            // Create the read-only nested model collection
            this.nestedModelCollection = new StepwiseLogisticRegressionModelCollection(nestedModels);


            // If the model with highest p-value is not significant,
            if (imax >= 0 && pmax > threshold)
            {
                // Then this means the variable can be safely discarded from the full model
                int removed = currentModel.Variables[imax];

                // Our diminished nested model will become our next full model.
                this.currentModel = nestedModels[imax];

                // Finally, return the index of the removed variable
                return(removed);
            }
            else
            {
                // Else we can not safely remove any variable from the model.
                return(-1);
            }
        }