Пример #1
0
        protected override void EndProcessing()
        {
            var model = new LogisticRegressionAnalysis();

            double[][] inputs;
            double[]   outputs;

            if (ParameterSetName == "XY")
            {
                inputs  = Converter.ToDoubleJaggedArray(X);
                outputs = Converter.ToDoubleArray(Y);
            }
            else
            {
                outputs = _data.GetColumn(OutputName).ToDoubleArray();

                _data.RemoveColumn(OutputName);
                inputs = _data.ToDoubleJaggedArray();

                model.Inputs = _data.ColumnNames.ToArray <string>();
                model.Output = OutputName;
            }

            double[] w = null;
            if (Weights != null)
            {
                w = Converter.ToDoubleArray(Weights);
            }

            model.Learn(inputs, outputs, w);

            WriteObject(model);
        }
        public void LogisticRegresssionAanlysis(float[,] datas, int rowCount, int columnCount, out double[] coef,
                                                out double[] odds, out double[] stde, out double[] min, out double[] max)
        {
            double[][] inputs  = new double[rowCount][];
            double[]   outputs = new double[rowCount];

            //根据给定的数据得到输入、输出数组
            SetInputAndOutputData(datas, rowCount, columnCount, ref inputs, ref outputs);

            // Create a Logistic Regression analysis
            var regression = new LogisticRegressionAnalysis();

            regression.Learn(inputs, outputs);
            //regression.Compute(); // compute the analysis.

            // We can also investigate all parameters individually. For
            // example the coefficients values will be available at the
            // vector
            coef = regression.CoefficientValues;

            // The first value refers to the model's intercept term. We
            // can also retrieve the odds ratios and standard errors:
            odds = regression.OddsRatios;
            stde = regression.StandardErrors;

            //得到置信区间
            Accord.DoubleRange[] confidence = regression.Confidences;
            min = new double[confidence.Length];
            max = new double[confidence.Length];
            for (int i = 0; i < confidence.Length; i++)
            {
                min[i] = confidence[i].Min;
                max[i] = confidence[i].Max;
            }
        }
        public void ComputeTest1()
        {

            double[][] inputs = training.Submatrix(null, 0, 3);
            double[] outputs = training.GetColumn(4);

            var regression = new LogisticRegressionAnalysis(inputs, outputs);

            bool converged = regression.Compute();
            Assert.IsTrue(converged);

            double[] actual = regression.Result;

            double[] expected = 
            {
                0.000012, 0.892611, 0.991369, 0.001513, 0.904055,
                0.001446, 0.998673, 0.001260, 0.629312, 0.004475,
                0.505362, 0.999791, 0.000050, 1.000000, 0.990362,
                0.985265, 1.000000, 1.000000, 0.001319, 0.000001,
                0.000001, 0.000050, 0.702488, 0.003049, 0.000046,
                0.000419, 0.026276, 0.036813, 0.000713, 0.001484,
                0.000008, 0.000009, 0.278950, 0.001402, 0.025764,
                0.002464, 0.000219, 0.007328, 0.000106, 0.002619,
                0.002913, 0.000002,
            };

            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual(expected[i], actual[i], 1e-6);
        }
Пример #4
0
        public void ComputeTest1()
        {
            double[][] inputs  = training.Submatrix(null, 0, 3);
            double[]   outputs = training.GetColumn(4);

            var regression = new LogisticRegressionAnalysis(inputs, outputs);

            bool converged = regression.Compute();

            Assert.IsTrue(converged);

            double[] actual = regression.Result;

            double[] expected =
            {
                0.000012, 0.892611, 0.991369, 0.001513, 0.904055,
                0.001446, 0.998673, 0.001260, 0.629312, 0.004475,
                0.505362, 0.999791, 0.000050, 1.000000, 0.990362,
                0.985265, 1.000000, 1.000000, 0.001319, 0.000001,
                0.000001, 0.000050, 0.702488, 0.003049, 0.000046,
                0.000419, 0.026276, 0.036813, 0.000713, 0.001484,
                0.000008, 0.000009, 0.278950, 0.001402, 0.025764,
                0.002464, 0.000219, 0.007328, 0.000106, 0.002619,
                0.002913, 0.000002,
            };

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 1e-6);
            }
        }
Пример #5
0
        public void learn2()
        {
            // Test instance 01
            double[][] trainInput =
            {
                new double[] { 1, 1 },
                new double[] { 0, 0 },
            };

            double[] trainOutput = { 1, 0 };
            double[] testInput   = { 0, 0.2 };

            var target = new LogisticRegressionAnalysis();

            target.Regularization = 1e-10;

            var regression = target.Learn(trainInput, trainOutput);

            Assert.AreSame(regression, target.Regression);

            double[] coef = target.Coefficients.Apply(x => x.Value);
            Assert.AreEqual(coef[0], -19.360661491141897, 1e-6);
            Assert.AreEqual(coef[1], 19.702873967721807, 1e-6);
            Assert.AreEqual(coef[2], 19.702873967721807, 1e-6);

            double output = target.Regression.Score(testInput);

            Assert.AreEqual(0, output, 1e-6);

            // Test instance 02
            trainInput = new double[][]
            {
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
            };

            trainOutput = new double[6] {
                1, 1, 0, 0, 1, 1
            };

            target = new LogisticRegressionAnalysis();

            regression = target.Learn(trainInput, trainOutput);

            double[] actual   = regression.Score(trainInput);
            double[] expected = { 0.500000000158903, 0.999999998410966, 0.500000000913694, 0.500000000158903, 0.999999998410966, 0.500000000913694 };
            Assert.IsTrue(actual.IsEqual(expected, 1e-6));

            coef = target.Coefficients.Apply(x => x.Value);
            //string str = coef.ToCSharp();
            expected = new double[] { 1.86680346470929, -3.87720719574071, 2.44120453079343, -0.574401066088034, 5.16960959435804, 2.44120453079343, -3.87720719574087, 5.16960959435804, 2.44120453079343, -3.87720719574087, 2.44120453079343 };
            Assert.IsTrue(coef.IsEqual(expected, 1e-6));
        }
        public void ComputeTest2()
        {
            // Test instance 01
            double[][] trainInput =
            {
                new double[] { 1, 1 },
                new double[] { 0, 0 },
            };

            double[] trainOutput = { 1, 0 };
            double[] testInput   = { 0, 0.2 };

            LogisticRegressionAnalysis target = new LogisticRegressionAnalysis(trainInput, trainOutput);

            target.Regularization = 0;

            target.Compute();

            foreach (var coefficient in target.Coefficients)
            {
                Assert.IsFalse(double.IsNaN(coefficient.Value));
            }

            double output = target.Regression.Compute(testInput);

            Assert.AreEqual(0, output, 1e-10);
            Assert.IsFalse(double.IsNaN(output));

            // Test instance 02
            trainInput = new double[][]
            {
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
            };

            trainOutput = new double[6] {
                1, 1, 0, 0, 1, 1
            };

            target = new LogisticRegressionAnalysis(trainInput, trainOutput);

            bool expected = true;
            bool actual   = target.Compute();

            foreach (LogisticCoefficient coefficient in target.Coefficients)
            {
                Assert.IsFalse(double.IsNaN(coefficient.Value));
            }

            Assert.AreEqual(expected, actual);
        }
Пример #7
0
        public LogisticRegressionAnalysis Regress()
        {
            var analyzer = new LogisticRegressionAnalysis()
            {
                Iterations = 100,
                Inputs     = GetInputNames(),
            };

            analyzer.Learn(InputData(), Filter.FilteredResult().Select(i => resultsFunc(i)?1.0:0.0).ToArray());
            return(analyzer);
        }
        public void ComputeTest3()
        {
            double[][] inputs  = training.Submatrix(null, 0, 3);
            double[]   outputs = training.GetColumn(4);

            LogisticRegressionAnalysis regression = new LogisticRegressionAnalysis(inputs, outputs);

            bool expected = false;
            bool actual   = regression.Compute(1e-5, 3);

            Assert.AreEqual(expected, actual);
        }
        public void ComputeTest2()
        {
            // Test instance 01
            double[][] trainInput =
            {
               new double[] { 1, 1 },
               new double[] { 0, 0 },
            };

            double[] trainOutput = { 1, 0 };
            double[] testInput = { 0, 0.2 };

            LogisticRegressionAnalysis target = new LogisticRegressionAnalysis(trainInput, trainOutput);

            target.Regularization = 0;

            target.Compute();

            foreach (var coefficient in target.Coefficients)
                Assert.IsFalse(double.IsNaN(coefficient.Value));

            double output = target.Regression.Compute(testInput);
            Assert.AreEqual(0, output, 1e-10);
            Assert.IsFalse(double.IsNaN(output));

            // Test instance 02
            trainInput = new double[][]
            {
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
            };

            trainOutput = new double[6] { 1, 1, 0, 0, 1, 1 };

            target = new LogisticRegressionAnalysis(trainInput, trainOutput);

            bool expected = true;
            bool actual = target.Compute();

            foreach (LogisticCoefficient coefficient in target.Coefficients)
                Assert.IsFalse(double.IsNaN(coefficient.Value));

            Assert.AreEqual(expected, actual);
        }
        public static void Train()
        {
            DataHandler.ImportReviewData(3);

            var maxCount = 1;

            double[][] input = new double[maxCount][];
            for (int i = 0; i < maxCount; i++)
            {
                input[i] = CalculateProbabilities(DataHandler.Reviews[i].reviewText);
            }

            double[] output = DataHandler.Reviews.Take(maxCount).Select(r => r.overall).ToArray();

            LogisticRegressionAnalysis regression = new LogisticRegressionAnalysis();
            LogisticRegression         lr         = regression.Learn(input, output);
        }
        public void DoStepTest()
        {
            double[][] inputs = Matrix.Expand(
                new double[][] {
                new double[] { 0, 0, 0 },
                new double[] { 1, 0, 0 },
                new double[] { 0, 1, 0 },
                new double[] { 1, 1, 0 },
                new double[] { 0, 0, 1 },
                new double[] { 1, 0, 1 },
                new double[] { 0, 1, 1 },
                new double[] { 1, 1, 1 },
            }, new int[] { 60, 17, 8, 2, 187, 85, 51, 23 });

            double[] outputs = Matrix.Expand(
                new double[] { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
                new int[] { 5, 60 - 5, 2, 17 - 2, 1, 8 - 1, 0, 2 - 0, 35, 187 - 35, 13, 85 - 13, 15, 51 - 15, 8, 23 - 8 });



            var target2 = new LogisticRegressionAnalysis(inputs, outputs);

            target2.Compute();

            Assert.AreEqual(target2.CoefficientValues[0], -2.377661, 0.0001);
            Assert.AreEqual(target2.CoefficientValues[1], -0.067775, 0.0001);
            Assert.AreEqual(target2.CoefficientValues[2], 0.69531, 0.0001);
            Assert.AreEqual(target2.CoefficientValues[3], 0.871939, 0.0001);


            var target = new StepwiseLogisticRegressionAnalysis(
                inputs, outputs,
                new string[] { "x1", "x2", "x3" }, "Y"
                );

            target.Threshold = 0.15;

            int actual;

            actual = target.DoStep();
            Assert.AreEqual(0, actual);

            actual = target.DoStep();
            Assert.AreEqual(-1, actual);
        }
        public void Learn(out double misclassificationError)
        {
            var inputs = trainingExamples.Select(e => e.Features.Values).ToArray();
            var outputs = trainingExamples.Select(e => (double)e.Expected).ToArray();

            regression = new LogisticRegressionAnalysis(inputs, outputs);
            regression.Compute();

            misclassificationError = 0.0;
            foreach (var example in trainingExamples)
            {
                var expected = example.Expected;
                var actual = Classify(example.Features);
                if (Math.Abs(actual - expected) > 1e-5)
                {
                    misclassificationError += 1.0;
                }
            }
            misclassificationError /= trainingExamples.Count;
        }
Пример #13
0
        private void logReg()
        {
            int numObs = terrainVisResults[0].visScore.Count * terrainVisResults[0].visScore[0].Count;


            foreach (RefPlaneVis rpv in terrainVisResults)
            {
                double[][] input  = new double[numObs][];
                double[]   output = new double[numObs];
                int        oNum   = 0;
                for (int i = 0; i < rpv.visScore.Count; i++)
                {
                    for (int s = 0; s < rpv.visScore[i].Count; s++)
                    {
                        double score = rpv.visScore[i][s];
                        if (Double.IsNaN(score))
                        {
                            score = 0;
                        }
                        input[oNum] = new double[] { score, rpv.groupNum, score + rpv.groupNum };//score * rpv.groupNum,
                        if (rpv.observationPts[i][s])
                        {
                            output[oNum] = 1;
                        }
                        else
                        {
                            output[oNum] = 0;
                        }
                        oNum++;
                    }
                }
                printInputOutput(input, output);
                var lra = new LogisticRegressionAnalysis();


                // Now, we can use the learner to finally estimate our model:
                LogisticRegression regression = lra.Learn(input, output);
                var cf = lra.Coefficients;
            }
        }
Пример #14
0
        public void learn1()
        {
            double[][] inputs  = training.Submatrix(null, 0, 3);
            double[]   outputs = training.GetColumn(4);

            var lra = new LogisticRegressionAnalysis()
            {
                ComputeInnerModels = true
            };

            var regression = lra.Learn(inputs, outputs);

            double[] actual = regression.Score(inputs);

            double[] expected =
            {
                0.000012, 0.892611, 0.991369, 0.001513, 0.904055,
                0.001446, 0.998673, 0.001260, 0.629312, 0.004475,
                0.505362, 0.999791, 0.000050, 1.000000, 0.990362,
                0.985265, 1.000000, 1.000000, 0.001319, 0.000001,
                0.000001, 0.000050, 0.702488, 0.003049, 0.000046,
                0.000419, 0.026276, 0.036813, 0.000713, 0.001484,
                0.000008, 0.000009, 0.278950, 0.001402, 0.025764,
                0.002464, 0.000219, 0.007328, 0.000106, 0.002619,
                0.002913, 0.000002,
            };

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 1e-6);
            }

            Assert.AreEqual(5, lra.LikelihoodRatioTests.Length);
            Assert.IsNull(lra.LikelihoodRatioTests[0]);
            Assert.AreEqual(0.99999995244237416, lra.LikelihoodRatioTests[1].PValue, 1e-7);
            Assert.AreEqual(0.99999993274336052, lra.LikelihoodRatioTests[2].PValue, 1e-7);
            Assert.AreEqual(0.99999992480479116, lra.LikelihoodRatioTests[3].PValue, 1e-7);
            Assert.AreEqual(0.0000000059730130622305527, lra.LikelihoodRatioTests[4].PValue, 1e-20);
        }
        public void learn1()
        {
            double[][] inputs = training.Submatrix(null, 0, 3);
            double[] outputs = training.GetColumn(4);

            var lra = new LogisticRegressionAnalysis()
            {
                ComputeInnerModels = true
            };

            var regression = lra.Learn(inputs, outputs);

            double[] actual = regression.Score(inputs);

            double[] expected = 
            {
                0.000012, 0.892611, 0.991369, 0.001513, 0.904055,
                0.001446, 0.998673, 0.001260, 0.629312, 0.004475,
                0.505362, 0.999791, 0.000050, 1.000000, 0.990362,
                0.985265, 1.000000, 1.000000, 0.001319, 0.000001,
                0.000001, 0.000050, 0.702488, 0.003049, 0.000046,
                0.000419, 0.026276, 0.036813, 0.000713, 0.001484,
                0.000008, 0.000009, 0.278950, 0.001402, 0.025764,
                0.002464, 0.000219, 0.007328, 0.000106, 0.002619,
                0.002913, 0.000002,
            };

            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual(expected[i], actual[i], 1e-6);

            Assert.AreEqual(5, lra.LikelihoodRatioTests.Length);
            Assert.IsNull(lra.LikelihoodRatioTests[0]);
            Assert.AreEqual(0.99999995244237416, lra.LikelihoodRatioTests[1].PValue, 1e-7);
            Assert.AreEqual(0.99999993274336052, lra.LikelihoodRatioTests[2].PValue, 1e-7);
            Assert.AreEqual(0.99999992480479116, lra.LikelihoodRatioTests[3].PValue, 1e-7);
            Assert.AreEqual(0.0000000059730130622305527, lra.LikelihoodRatioTests[4].PValue, 1e-20);
        }
Пример #16
0
        public string ProcessLinearRegression(DataTable sourceTable, string _dependentName, string _independentName)
        {
            try
            {
                double[][]             inputs;
                double[]               outputs;
                LinearRegressionOutput output = new LinearRegressionOutput();

                // Gets the column of the dependent variable
                String    dependentName = _dependentName;
                DataTable dependent     = sourceTable.DefaultView.ToTable(false, dependentName);

                String[] independentNames = _independentName.Split(',');
                //String[] independentNames = names.ToArray();
                DataTable independent = sourceTable.DefaultView.ToTable(false, independentNames);

                // Creates the input and output matrices from the source data table
                inputs  = independent.ToJagged();
                outputs = dependent.Columns[dependentName].ToArray();

                // Creates the Simple Descriptive Analysis of the given source
                var sda = new DescriptiveAnalysis()
                {
                    ColumnNames = independentNames
                }.Learn(inputs);

                // TODO: Standardize the InputNames/OutputNames properties

                // Populates statistics overview tab with analysis data
                DescriptiveMeasureCollection DMC = sda.Measures;

                DistributionMesure dm = new DistributionMesure()
                {
                    //Column = sda.Measures.GetEnumerator(x => x),
                };

                //DMC.s
                //output.DistributionMeasuresDataSource = dm;

                // Creates the Logistic Regression Analysis of the given source
                this.lra = new LogisticRegressionAnalysis()
                {
                    Inputs = independentNames,
                    Output = dependentName
                };

                // Create the Multiple Linear Regression Analysis of the given source
                this.mlr = new MultipleLinearRegressionAnalysis(intercept: true)
                {
                    Inputs = independentNames,
                    Output = dependentName
                };

                // Compute the Linear Regression Analysis
                MultipleLinearRegression reg = mlr.Learn(inputs, outputs);

                LinearRegressionCoefficientCollection LCC = mlr.Coefficients;
                List <LinearCoefficients>             lcs = new List <LinearCoefficients>();
                foreach (var rc in LCC)
                {
                    LinearCoefficients lc = new LinearCoefficients()
                    {
                        Name            = rc.Name,
                        Value           = rc.Value,
                        StandardError   = rc.StandardError,
                        TStatistic      = rc.TTest.Statistic,
                        P_ValueofT      = rc.TTest.PValue,
                        FStatistic      = rc.FTest.Statistic,
                        P_ValueofF      = rc.FTest.PValue,
                        ConfidenceUpper = rc.ConfidenceUpper,
                        ConfidenceLower = rc.ConfidenceLower
                    };
                    lcs.Add(lc);
                }
                output.LinearCoefficientsDataSource = lcs;

                //AnovaSourceCollection RDS = mlr.Table;
                List <RegressionAnova> acs = new List <RegressionAnova>();
                foreach (var RDS in mlr.Table)
                {
                    RegressionAnova ra = new RegressionAnova()
                    {
                        Source             = RDS.Source,
                        DegreesOfFreedom   = RDS.DegreesOfFreedom,
                        SumOfSquares       = RDS.SumOfSquares,
                        MeanSquares        = RDS.MeanSquares,
                        FStatistic         = RDS.Statistic,
                        PValueSignificance = (RDS.Significance == null) ? 0 : RDS.Significance.PValue
                    };
                    acs.Add(ra);
                }

                output.RegressionAnovaDataSource = acs;

                output.RSquared              = mlr.RSquared.ToString("N5");
                output.RSquaredAdj           = mlr.RSquareAdjusted.ToString("N5");
                output.ChiPValue             = mlr.ChiSquareTest.PValue.ToString("N5");
                output.FPValue               = mlr.FTest.PValue.ToString("N5");
                output.ZPValue               = mlr.ZTest.PValue.ToString("N5");
                output.ChiStatistic          = mlr.ChiSquareTest.Statistic.ToString("N5");
                output.FStatistic            = mlr.FTest.Statistic.ToString("N5");
                output.ZStatistic            = mlr.ZTest.Statistic.ToString("N5");
                output.ChiSignificantChecked = mlr.ChiSquareTest.Significant;
                output.FSignificantChecked   = mlr.FTest.Significant;
                output.ZSignificantChecked   = mlr.ZTest.Significant;

                // Populate projection source table
                string[] cols = independentNames;
                if (!independentNames.Contains(dependentName))
                {
                    cols = independentNames.Concatenate(dependentName);
                }

                DataTable projSource = sourceTable.DefaultView.ToTable(false, cols);
                //output.ProjectionSourceDataSource = projSource;

                DataTable independentProj = projSource.DefaultView.ToTable(false, lra.Inputs);
                DataTable dependentProj   = projSource.DefaultView.ToTable(false, lra.Output);

                double[][] input = independentProj.ToJagged();
                double[]   output1;
                output1 = mlr.Regression.Transform(input);

                DataTable result = projSource.Clone();
                for (int i = 0; i < input.Length; i++)
                {
                    DataRow row = result.NewRow();
                    for (int j = 0; j < lra.Inputs.Length; j++)
                    {
                        row[lra.Inputs[j]] = input[i][j];
                    }
                    row[lra.Output] = output1[i];

                    result.Rows.Add(row);
                }

                output.ProjectionResultDataSource = result;

                var jsonResult = JsonConvert.SerializeObject(output, Formatting.Indented);
                return(jsonResult);
            }
            catch (Exception ex)
            {
                throw new Exception("Error:" + ex);
            }
        }
        public void ComputeTest4()
        {
            // 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 = new double[][]
            {
                //            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 = new double[]
            {
                0, 0, 0, 1, 1, 1, 0, 0, 0, 1
            };

            // Create a Logistic Regression analysis
            var regression = new LogisticRegressionAnalysis(inputs, output);

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

            // Now we can show a summary of the analysis
            // 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 also use the analysis to classify a new patient
            double y = regression.Regression.Compute(new double[] { 87, 1 });

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

            Assert.AreEqual(0.085627701654393359, odds[0], 1e-10);
            Assert.AreEqual(1.0208597028836701, odds[1], 1e-10);
            Assert.AreEqual(5.8584748789881331, odds[2], 1e-10);
            Assert.IsFalse(odds.HasNaN());

            Assert.AreEqual(2.15901268940475, stde[0], 1e-10);
            Assert.AreEqual(0.033789966967853677, stde[1], 1e-10);
            Assert.AreEqual(1.4729620910282104, stde[2], 1e-10);
            Assert.IsFalse(stde.HasNaN());

            Assert.AreEqual(0.75143272827425545, y, 1e-10);
            Assert.IsFalse(Double.IsNaN(y));
        }
Пример #18
0
        static void Main(string[] args)
        {
            /*int n = 9;
             * double[] a = new double[n];
             * double[,] c = new double[n,n];
             * a[0] = 0.1;
             * double sum = a[0];
             * for (int i = 1; i < n; i++)
             * {
             *  a[i] = a[i - 1] + 0.1;
             *  sum += a[i];
             * }
             * double[] b = new double[n];
             * b[0] = 0.1;
             * for (int i = 1; i < n / 2 + 1; i++)
             *  b[i] = b[i - 1] + 0.01;
             * for (int i = n / 2 + 1; i < n; i++)
             *  b[i] = b[i - 1] - 0.01;
             *
             * for (int i = 1; i < n; i++)
             * {
             *  c[1, i] = ProbabilityNormalOne(a, b, c[1, i]);
             *  c[0, i] = a[i];
             * }
             *
             *
             *
             *
             *
             * // Console.WriteLine(Round(Dispersion(b, a)));
             * //Console.WriteLine(Round(ExpectedValue(b, a)));
             * Console.WriteLine(ErrorNormal(b, a));
             * var chi = new ChiSquareTest(b, a, n-1);
             * double pvalue = chi.PValue;
             * bool significant = chi.Significant;
             *
             * Console.WriteLine("___________");
             * for (int i = 0; i < 9; i++)
             * {
             *  Console.Write(b[i]);
             *  Console.Write(' ');
             *  Console.WriteLine(Round(ProbabilityNormal(b, a, b[i]), 3));
             * }
             *
             * Console.WriteLine(pvalue);
             * Console.WriteLine(significant);
             * Console.Read();*/
            double[][] inputs = new double[4][] { new double[] { 1, 2 }, new double[] { 1, 3 },
                                                  new double[] { 1, 4 }, new double[] { 1, 5 } };

            double[] outputs = new double[] { 1, 1, 1, 0 };

            double alpha = 0.03;

            double[] theta = new double[] { 1, 1 };
            double[] newt  = Learn(inputs, outputs, theta, alpha);
            Console.WriteLine("_____________");
            foreach (var i in newt)
            {
                Console.WriteLine(i);
            }
            var lra = new LogisticRegressionAnalysis()
            {
                Regularization = 0.03
            };

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

            _ = Accord.Controls.DataGridBox.Show(regression.Coefficients);
            Console.ReadLine();
        }
Пример #19
0
        private void btnGenerateInputData_Click(object sender, EventArgs e)
        {
            //if (this.strProjectPath.Trim() == "")
            //    return;
            //if (this.cmbBoundary.Text.Trim() == "")
            //    return;
            //if (this.cmbRstraint.Text.Trim() == "")
            //    return;
            //if (this.txtParameter.Text.Trim() == "")
            //    return;
            if (lsbLayerLandUse.Items.Count == 0)
                return;
            //if (lsbLayerDriverFactor.Items.Count == 0)
            //    return;
            int iLanduseType = 5;

            ILayer pLayer = null;
            IRaster pRaster =new RasterClass();

            IGeoDataset pGdsMask = null;
            IGeoDataset pGdsRstraint = null;
            IGeoDataset pGdsLanduse = null;
            IGeoDataset pGdsDriverFactor = null;
            IRasterBandCollection pRasterBandColection=(new RasterClass()) as IRasterBandCollection;
            //try
            //{
                string sLyrMask = this.cmbBoundary.Text;

                //boundaty-->mask
                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    ILayer pLyr = pMap.get_Layer(i);
                    if (pLyr is IRasterLayer)
                    {
                        if (pLyr.Name == sLyrMask)
                        {
                            pRaster = (pLyr as IRasterLayer).Raster;
                            pGdsMask = pRaster as IGeoDataset;
                        }
                    }
                }
                //data
                //限制区
                string sLyrRstraint = this.cmbBoundary.Text;
                //土地利用数据
                string sLyrLanduse = this.lsbLayerLandUse.Items[0].ToString();
                //驱动因子
                string[] arr = new string[this.lsbLayerDriverFactor.Items.Count];

                for (int i = 0; i < this.lsbLayerDriverFactor.Items.Count; i++)
                {
                    arr[i]=this.lsbLayerDriverFactor.Items[i].ToString();
                }

                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    ILayer pLyr = pMap.get_Layer(i);
                    if (pLyr is IRasterLayer)
                    {
                        if (pLyr.Name == sLyrRstraint)
                        {
                            pRaster = (pLyr as IRasterLayer).Raster;
                            pGdsRstraint = pRaster as IGeoDataset;
                        }

                        //土地利用数据
                        if (pLyr.Name == sLyrLanduse)
                        {
                            this.rtxtState.AppendText("读取土地利用参数数据...\n");
                            this.rtxtState.ScrollToCaret();
                            pRaster = (pLyr as IRasterLayer).Raster;
                            pGdsLanduse = pRaster as IGeoDataset;
                            //land use 添加到 IRasterBandCollection
                            pRasterBandColection=pRaster as IRasterBandCollection;
                            //pRasterBandColection.AppendBand(pRaster as IRasterBand);

                            string ascFileNameLanduse = strProjectPath + "\\cov1_all.0";
                            //cov1_0.0;cov1_1.0;
                            Rater2Ascii(pGdsMask, 100, pGdsLanduse, ascFileNameLanduse);

                            //将土地利用数据拆分

                            StreamReader sr = new StreamReader(ascFileNameLanduse, System.Text.Encoding.Default);
                            //try
                            //{

                            //使用StreamReader类来读取文件
                            sr.BaseStream.Seek(0, SeekOrigin.Begin);
                            // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
                            //读取头文件
                            string[] header = new string[6];

                            for (int j = 0; j < 6; j++)
                            {
                                header[j] = sr.ReadLine();
                            }
                            //行列数
                            string[] ncols = header[0].Split(' ');
                            string[] nrows = header[1].Split(' ');
                            int icol = int.Parse(ncols[ncols.Length - 1]);
                            int irow = int.Parse(nrows[nrows.Length - 1]);

                            int[,] iLanduse = new int[irow, icol];
                            //
                            string strLine = sr.ReadLine();
                            string[] strData;
                            int ir = 0;
                            while (ir < irow)
                            //while (strLine != null)
                            {
                                strData = strLine.Split(' ');
                                for (int ic = 0; ic < icol; ic++)
                                {
                                    iLanduse[ir, ic] = int.Parse(strData[ic]);
                                }
                                strLine = sr.ReadLine();
                                ir++;
                            }
                            //关闭此StreamReader对象
                            sr.Close();
                            //输出相应的土地利用数据
                            DataTable2Txt(header, iLanduseType, iLanduse, strProjectPath);

                            //}
                            //catch (Exception ex)
                            //{
                            //MessageBox.Show(ex.Message);
                            sr.Close();
                            //}
                        }
                    }
                }
                this.rtxtState.AppendText("输出土地利用参数数据成功。\n");
                this.rtxtState.AppendText("读取驱动因子数据...\n");
                this.rtxtState.ScrollToCaret();
                for (int ifac = 0; ifac < lsbLayerDriverFactor.Items.Count; ifac++)
                {
                    for (int i = 0; i < pMap.LayerCount; i++)
                    {
                        ILayer pLyr = pMap.get_Layer(i);
                        if (pLyr is IRasterLayer)
                        {
                        //输出驱动因子数据  sc1gr0.grid

                            string sFacName = lsbLayerDriverFactor.Items[ifac].ToString();
                            if (pLyr.Name == sFacName)
                            {
                                pRaster = (pLyr as IRasterLayer).Raster;
                                pGdsDriverFactor = pRaster as IGeoDataset;

                                string ascFileNameFac = strProjectPath + "\\sc1gr"+ifac.ToString()+".grid";
                                //cov1_0.0;cov1_1.0;
                                this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】\n");
                                Rater2Ascii(pGdsMask, 100, pGdsDriverFactor, ascFileNameFac);
                                this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】成功。\n");
                                this.rtxtState.ScrollToCaret();
                                //mask 添加到 IRasterBandCollection
                                IRasterBandCollection rasterbands = (IRasterBandCollection)pRaster;
                                IRasterBand rasterband = rasterbands.Item(0);
                                pRasterBandColection.AppendBand(rasterband);
                                //pRasterBandColection.Add(pRaster as IRasterBand,ifac+1);

                            }
                        }
                    }
                }
                this.rtxtState.AppendText("开始制备驱动因子参数...\n");
                this.rtxtState.ScrollToCaret();
                //sample data
                ITable itFactors=ExportSample(pGdsMask,pRasterBandColection);
                //logistic 回归分析
                DataTable dtFactors = ITable2DTable(itFactors);
                itFactors = null;

                //制备驱动力参数文件
                this.rtxtState.AppendText("读取驱动因子数据表格数据...\n");
                this.rtxtState.ScrollToCaret();
                LogisticRegressionAnalysis lra;
                // Gets the columns of the independent variables
                List<string> names = new List<string>();
                foreach (string name in lsbLayerDriverFactor.Items)
                    names.Add(name);

                String[] independentNames = names.ToArray();
                DataTable independent = dtFactors.DefaultView.ToTable(false, independentNames);
                // Creates the input and output matrices from the source data table
                double[][] input = independent.ToArray();
                double[,] sourceMatrix = dtFactors.ToMatrix(independentNames);

                for(int ild=0;ild<iLanduseType;ild++)
                {
                    String landuseName = (string)this.lsbLayerLandUse.Items[0].ToString();
                    this.rtxtState.AppendText("开始制备土地利用类型【" + ild.ToString() + "】驱动因子参数...\n");
                    this.rtxtState.ScrollToCaret();
                    DataColumn taxColumn =new DataColumn();
                    taxColumn.DataType = System.Type.GetType("System.Int32");
                    taxColumn.ColumnName ="sysland"+ild.ToString();//列名
                    taxColumn.Expression = "iif("+landuseName+" = "+ild.ToString()+",1,0)";//设置该列得表达式,用于计算列中的值或创建聚合列
                    dtFactors.Columns.Add(taxColumn);
                    string dependentName = "sysland" + ild.ToString();

                    DataTable dependent = dtFactors.DefaultView.ToTable(false, dependentName);
                    double[] output = dependent.Columns[dependentName].ToArray();

                    // Creates the Simple Descriptive Analysis of the given source
                    DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, independentNames);
                    sda.Compute();

                    // Populates statistics overview tab with analysis data
                    //dgvDistributionMeasures.DataSource = sda.Measures;

                    // Creates the Logistic Regression Analysis of the given source
                    lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName);

                    // Compute the Logistic Regression Analysis
                    lra.Compute();

                    // Populates coefficient overview with analysis data
                    //lra.Coefficients;
                    MessageBox.Show(lra.Coefficients.Count.ToString());

                    //// Populate details about the fitted model
                    //tbChiSquare.Text = lra.ChiSquare.Statistic.ToString("N5");
                    //tbPValue.Text = lra.ChiSquare.PValue.ToString("N5");
                    //checkBox1.Checked = lra.ChiSquare.Significant;
                    //tbDeviance.Text = lra.Deviance.ToString("N5");
                    //tbLogLikelihood.Text = lra.LogLikelihood.ToString("N5");
                }
                this.rtxtState.AppendText("制备驱动因子参数成功。\n");
                this.rtxtState.ScrollToCaret();
                MessageBox.Show(dtFactors.Rows.Count.ToString());
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);

            //}
        }
        public void learn2()
        {
            // Test instance 01
            double[][] trainInput =
            {
               new double[] { 1, 1 },
               new double[] { 0, 0 },
            };

            double[] trainOutput = { 1, 0 };
            double[] testInput = { 0, 0.2 };

            var target = new LogisticRegressionAnalysis();

            target.Regularization = 1e-10;

            var regression = target.Learn(trainInput, trainOutput);
            Assert.AreSame(regression, target.Regression);

            double[] coef = target.Coefficients.Apply(x => x.Value);
            Assert.AreEqual(coef[0], -19.360661491141897, 1e-6);
            Assert.AreEqual(coef[1], 19.702873967721807, 1e-6);
            Assert.AreEqual(coef[2], 19.702873967721807, 1e-6);

            double output = target.Regression.Score(testInput);
            Assert.AreEqual(0, output, 1e-6);

            // Test instance 02
            trainInput = new double[][]
            {
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
                new double[] { 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 },
                new double[] { 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
                new double[] { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 },
            };

            trainOutput = new double[6] { 1, 1, 0, 0, 1, 1 };

            target = new LogisticRegressionAnalysis();

            regression = target.Learn(trainInput, trainOutput);

            double[] actual = regression.Score(trainInput);
            double[] expected = { 0.500000000158903, 0.999999998410966, 0.500000000913694, 0.500000000158903, 0.999999998410966, 0.500000000913694 };
            Assert.IsTrue(actual.IsEqual(expected, 1e-6));

            coef = target.Coefficients.Apply(x => x.Value);
            //string str = coef.ToCSharp();
            expected = new double[] { 1.86680346470929, -3.87720719574071, 2.44120453079343, -0.574401066088034, 5.16960959435804, 2.44120453079343, -3.87720719574087, 5.16960959435804, 2.44120453079343, -3.87720719574087, 2.44120453079343 };
            Assert.IsTrue(coef.IsEqual(expected, 1e-6));
        }
        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);
        }
Пример #22
0
        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);
        }
Пример #23
0
        public void RegressTest()
        {
            double[,] inputGrouped =
            {
                { 1, 4, 5 }, // product 1 has four occurrences of class 1 and five  of class 0
                { 2, 1, 3 }, // product 2 has one  occurrence  of class 1 and three of class 0
            };

            double[,] inputGroupProb =
            {
                { 1, 4.0 / (4 + 5) }, // product 1 has 0.44 probability of belonging to class 1
                { 2, 1.0 / (1 + 3) }, // product 2 has 0.25 probability of belonging to class 1
            };


            double[,] inputExtended =
            {
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 2, 1 }, // observation of product 2 in class 1
                { 2, 0 }, // observation of product 2 in class 0
                { 2, 0 }, // observation of product 2 in class 0
                { 2, 0 }, // observation of product 2 in class 0
            };


            // Fit using extended data
            double[][]         inputs            = Matrix.ColumnVector(inputExtended.GetColumn(0)).ToArray();
            double[]           outputs           = inputExtended.GetColumn(1);
            LogisticRegression target            = new LogisticRegression(1);
            IterativeReweightedLeastSquares irls = new IterativeReweightedLeastSquares(target);

            irls.Run(inputs, outputs);

            // Fit using grouped data
            double[][]         inputs2            = Matrix.ColumnVector(inputGroupProb.GetColumn(0)).ToArray();
            double[]           outputs2           = inputGroupProb.GetColumn(1);
            LogisticRegression target2            = new LogisticRegression(1);
            IterativeReweightedLeastSquares irls2 = new IterativeReweightedLeastSquares(target2);

            irls2.Run(inputs2, outputs2);


            Assert.IsTrue(Matrix.IsEqual(target.Coefficients, target2.Coefficients, 0.000001));



            double[,] data = new double[, ]
            {
                { 1, 0 },
                { 2, 0 },
                { 3, 0 },
                { 4, 0 },
                { 5, 1 },
                { 6, 0 },
                { 7, 1 },
                { 8, 0 },
                { 9, 1 },
                { 10, 1 }
            };


            double[][] inputs3  = Matrix.ColumnVector(data.GetColumn(0)).ToArray();
            double[]   outputs3 = data.GetColumn(1);
            LogisticRegressionAnalysis analysis = new LogisticRegressionAnalysis(inputs3, outputs3);

            analysis.Compute();

            Assert.IsFalse(double.IsNaN(analysis.Deviance));
            Assert.IsFalse(double.IsNaN(analysis.ChiSquare.PValue));

            Assert.AreEqual(analysis.Deviance, 8.6202, 0.0005);
            Assert.AreEqual(analysis.ChiSquare.PValue, 0.0278, 0.0005);

            // Check intercept
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[0].Value));
            Assert.AreEqual(analysis.Coefficients[0].Value, -4.3578, 0.0005);

            // Check coefficients
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].Value));
            Assert.AreEqual(analysis.Coefficients[1].Value, 0.6622, 0.0005);

            // Check statistics
            Assert.AreEqual(analysis.Coefficients[1].StandardError, 0.4001, 0.0005);
            Assert.AreEqual(analysis.Coefficients[1].Wald.PValue, 0.0979, 0.0005);

            Assert.AreEqual(analysis.Coefficients[1].OddsRatio, 1.9391, 0.0005);

            Assert.AreEqual(analysis.Coefficients[1].ConfidenceLower, 0.8852, 0.0005);
            Assert.AreEqual(analysis.Coefficients[1].ConfidenceUpper, 4.2478, 0.0005);


            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].Wald.PValue));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].StandardError));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].OddsRatio));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].ConfidenceLower));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].ConfidenceUpper));
        }
Пример #24
0
 public override void Train(double[][] predictors, double[] results)
 {
     regression = new LogisticRegressionAnalysis(predictors, results);
     regression.Compute();
 }
Пример #25
0
        public void RegressTest()
        {

            double[,] inputGrouped =
            {
                { 1, 4, 5 }, // product 1 has four occurances of class 1 and five  of class 0
                { 2, 1, 3 }, // product 2 has one  occurance  of class 1 and three of class 0
            };

            double[,] inputGroupProb =
            {
                { 1, 4.0 / (4 + 5) }, // product 1 has 0.44 probability of belonging to class 1
                { 2, 1.0 / (1 + 3) }, // product 2 has 0.25 probability of belonging to class 1
            };


            double[,] inputExtended =
            {
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 1 }, // observation of product 1 in class 1
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 1, 0 }, // observation of product 1 in class 0
                { 2, 1 }, // observation of product 2 in class 1
                { 2, 0 }, // observation of product 2 in class 0
                { 2, 0 }, // observation of product 2 in class 0
                { 2, 0 }, // observation of product 2 in class 0
            };


            // Fit using extended data
            double[][] inputs = Matrix.ColumnVector(inputExtended.GetColumn(0)).ToArray();
            double[] outputs = inputExtended.GetColumn(1);
            LogisticRegression target = new LogisticRegression(1);
            IterativeReweightedLeastSquares irls = new IterativeReweightedLeastSquares(target);
            irls.Run(inputs, outputs);

            // Fit using grouped data
            double[][] inputs2 = Matrix.ColumnVector(inputGroupProb.GetColumn(0)).ToArray();
            double[] outputs2 = inputGroupProb.GetColumn(1);
            LogisticRegression target2 = new LogisticRegression(1);
            IterativeReweightedLeastSquares irls2 = new IterativeReweightedLeastSquares(target2);
            irls2.Run(inputs2, outputs2);


            Assert.IsTrue(Matrix.IsEqual(target.Coefficients, target2.Coefficients, 0.000001));



            double[,] data = new double[,]
            {
                {  1, 0 },
                {  2, 0 },
                {  3, 0 },
                {  4, 0 },
                {  5, 1 },
                {  6, 0 },
                {  7, 1 },
                {  8, 0 },
                {  9, 1 },
                { 10, 1 }
            };


            double[][] inputs3 = Matrix.ColumnVector(data.GetColumn(0)).ToArray();
            double[] outputs3 = data.GetColumn(1);
            LogisticRegressionAnalysis analysis = new LogisticRegressionAnalysis(inputs3, outputs3);

            analysis.Compute();

            Assert.IsFalse(double.IsNaN(analysis.Deviance));
            Assert.IsFalse(double.IsNaN(analysis.ChiSquare.PValue));

            Assert.AreEqual(analysis.Deviance, 8.6202, 0.0005);
            Assert.AreEqual(analysis.ChiSquare.PValue, 0.0278, 0.0005);

            // Check intercept
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[0].Value));
            Assert.AreEqual(analysis.Coefficients[0].Value, -4.3578, 0.0005);

            // Check coefficients
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].Value));
            Assert.AreEqual(analysis.Coefficients[1].Value, 0.6622, 0.0005);

            // Check statistics
            Assert.AreEqual(analysis.Coefficients[1].StandardError, 0.4001, 0.0005);
            Assert.AreEqual(analysis.Coefficients[1].Wald.PValue, 0.0979, 0.0005);

            Assert.AreEqual(analysis.Coefficients[1].OddsRatio, 1.9391, 0.0005);

            Assert.AreEqual(analysis.Coefficients[1].ConfidenceLower, 0.8852, 0.0005);
            Assert.AreEqual(analysis.Coefficients[1].ConfidenceUpper, 4.2478, 0.0005);


            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].Wald.PValue));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].StandardError));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].OddsRatio));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].ConfidenceLower));
            Assert.IsFalse(double.IsNaN(analysis.Coefficients[1].ConfidenceUpper));
        }
        public void gh_937()
        {
            #region doc_learn_database
            // Note: this example uses a System.Data.DataTable to represent input data,
            // but note that this is not required. The data could have been represented
            // as jagged double matrices (double[][]) directly.

            // If you have to handle heterogeneus data in your application, such as user records
            // in a database, this data is best represented within the framework using a .NET's
            // DataTable object. In order to try to learn a classification or regression model
            // using this datatable, first we will need to convert the table into a representation
            // that the machine learning model can understand. Such representation is quite often,
            // a matrix of doubles (double[][]).
            var data = new DataTable("Customer Revenue Example");

            data.Columns.Add("Day", "CustomerId", "Time (hour)", "Weather", "Buy");
            data.Rows.Add("D1", 0, 8, "Sunny", true);
            data.Rows.Add("D2", 1, 10, "Sunny", true);
            data.Rows.Add("D3", 2, 10, "Rain", false);
            data.Rows.Add("D4", 3, 16, "Rain", true);
            data.Rows.Add("D5", 4, 15, "Rain", true);
            data.Rows.Add("D6", 5, 20, "Rain", false);
            data.Rows.Add("D7", 6, 12, "Cloudy", true);
            data.Rows.Add("D8", 7, 12, "Sunny", false);

            // One way to perform this conversion is by using a Codification filter. The Codification
            // filter can take care of converting variables that actually denote symbols (i.e. the
            // weather in the example above) into representations that make more sense given the assumption
            // of a real vector-based classifier.

            // Create a codification codebook
            var codebook = new Codification()
            {
                { "Weather", CodificationVariable.Categorical },
                { "Time (hour)", CodificationVariable.Continuous },
                { "Revenue", CodificationVariable.Continuous },
            };

            // Learn from the data
            codebook.Learn(data);

            // Now, we will use the codebook to transform the DataTable into double[][] vectors. Due
            // the way the conversion works, we can end up with more columns in your output vectors
            // than the ones started with. If you would like more details about what those columns
            // represent, you can pass then as 'out' parameters in the methods that follow below.
            string[] inputNames;  // (note: if you do not want to run this example yourself, you
            string   outputName;  // can see below the new variable names that will be generated)

            // Now, we can translate our training data into integer symbols using our codebook:
            double[][] inputs  = codebook.Apply(data, "Weather", "Time (hour)").ToJagged(out inputNames);
            double[]   outputs = codebook.Apply(data, "Buy").ToVector(out outputName);
            // (note: the Apply method transform a DataTable into another DataTable containing the codified
            //  variables. The ToJagged and ToVector methods are then used to transform those tables into
            //  double[][] matrices and double[] vectors, respectively.

            // If we would like to learn a logistic regression model for this data, there are two possible
            // ways depending on which aspect of the logistic regression we are interested the most. If we
            // are interested in interpreting the logistic regression, performing hypothesis tests with the
            // coefficients and performing an actual _logistic regression analysis_, then we can use the
            // LogisticRegressionAnalysis class for this. If however we are only interested in using
            // the learned model directly to predict new values for the dataset, then we could be using the
            // LogisticRegression and IterativeReweightedLeastSquares classes directly instead.

            // This example deals with the former case. For the later, please see the documentation page
            // for the LogisticRegression class.

            // We can create a new multiple linear analysis for the variables
            var lra = new LogisticRegressionAnalysis()
            {
                // We can also inform the names of the new variables that have been created by the
                // codification filter. Those can help in the visualizing the analysis once it is
                // data-bound to a visual control such a Windows.Forms.DataGridView or WPF DataGrid:

                Inputs = inputNames, // will be { "Weather: Sunny", "Weather: Rain, "Weather: Cloudy", "Time (hours)" }
                Output = outputName  // will be "Revenue"
            };

            // Compute the analysis and obtain the estimated regression
            LogisticRegression regression = lra.Learn(inputs, outputs);

            // And then predict the label using
            double predicted = lra.Transform(inputs[0]); // result will be ~0.287

            // Because we opted for doing a MultipleLinearRegressionAnalysis instead of a simple
            // linear regression, we will have further information about the regression available:
            int           inputCount        = lra.NumberOfInputs;  // should be 4
            int           outputCount       = lra.NumberOfOutputs; // should be 1
            double        logl              = lra.LogLikelihood;   // should be -4.6035570737785525
            ChiSquareTest x2                = lra.ChiSquare;       // should be 1.37789 (p=0.8480, non-significant)
            double[]      stdErr            = lra.StandardErrors;  // should be high except for the last value of 0.27122079214927985 (due small data)
            double[]      or                = lra.OddsRatios;      // should be 1.1116659950687609 for the last coefficient (related to time of day)
            LogisticCoefficientCollection c = lra.Coefficients;    // coefficient table (bind to a visual control for quick inspection)
            double[][] h = lra.InformationMatrix;                  // should contain Fisher's information matrix for the problem
            #endregion

            Assert.AreEqual(0.28703150858677107, predicted, 1e-8);
            Assert.AreEqual(4, inputCount, 1e-8);
            Assert.AreEqual(1, outputCount, 1e-8);
            Assert.AreEqual(-4.6035570737785525, logl, 1e-8);
            Assert.IsTrue(new[] { 0.0019604927838235376, 88.043929817973222, 101.42211648160144, 2.1954970044905113E-07, 1.1116659950687609 }.IsEqual(or, 1e-4));

            Assert.AreEqual(1.377897662970609, x2.Statistic, 1e-8);
            Assert.AreEqual(0.84802726696077046, x2.PValue, 1e-8);
        }
Пример #27
0
        private void btnSampleRunAnalysis_Click(object sender, EventArgs e)
        {
            // Check requirements
            if (sourceTable == null)
            {
                MessageBox.Show("A sample spreadsheet can be found in the " +
                                "Resources folder in the same directory as this application.",
                                "Please load some data before attempting an analysis");
                return;
            }

            if (checkedListBox1.CheckedItems.Count == 0)
            {
                MessageBox.Show("Please select the dependent input variables to be used in the regression model.",
                                "Please choose at least one input variable");
            }


            // Finishes and save any pending changes to the given data
            dgvAnalysisSource.EndEdit();
            sourceTable.AcceptChanges();

            // Gets the column of the dependent variable
            String    dependentName = (string)comboBox1.SelectedItem;
            DataTable dependent     = sourceTable.DefaultView.ToTable(false, dependentName);

            // Gets the columns of the independent variables
            List <string> names = new List <string>();

            foreach (string name in checkedListBox1.CheckedItems)
            {
                names.Add(name);
            }

            String[]  independentNames = names.ToArray();
            DataTable independent      = sourceTable.DefaultView.ToTable(false, independentNames);


            // Creates the input and output matrices from the source data table
            double[][] input  = independent.ToArray();
            double[]   output = dependent.Columns[dependentName].ToArray();

            double[,] sourceMatrix = sourceTable.ToMatrix(independentNames);

            // Creates the Simple Descriptive Analysis of the given source
            var sda = new DescriptiveAnalysis(sourceMatrix, independentNames);

            sda.Compute();

            // Populates statistics overview tab with analysis data
            dgvDistributionMeasures.DataSource = sda.Measures;

            // Creates the Logistic Regression Analysis of the given source
            lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName);


            // Compute the Logistic Regression Analysis
            lra.Compute();

            // Populates coefficient overview with analysis data
            dgvLogisticCoefficients.DataSource = lra.Coefficients;

            // Populate details about the fitted model
            tbChiSquare.Text     = lra.ChiSquare.Statistic.ToString("N5");
            tbPValue.Text        = lra.ChiSquare.PValue.ToString("N5");
            checkBox1.Checked    = lra.ChiSquare.Significant;
            tbDeviance.Text      = lra.Deviance.ToString("N5");
            tbLogLikelihood.Text = lra.LogLikelihood.ToString("N5");


            // Create the Multiple Linear Regression Analysis of the given source
            mlr = new MultipleLinearRegressionAnalysis(input, output, independentNames, dependentName, true);

            // Compute the Linear Regression Analysis
            mlr.Compute();

            dgvLinearCoefficients.DataSource = mlr.Coefficients;
            dgvRegressionAnova.DataSource    = mlr.Table;

            tbRSquared.Text          = mlr.RSquared.ToString("N5");
            tbRSquaredAdj.Text       = mlr.RSquareAdjusted.ToString("N5");
            tbChiPValue.Text         = mlr.ChiSquareTest.PValue.ToString("N5");
            tbFPValue.Text           = mlr.FTest.PValue.ToString("N5");
            tbZPValue.Text           = mlr.ZTest.PValue.ToString("N5");
            tbChiStatistic.Text      = mlr.ChiSquareTest.Statistic.ToString("N5");
            tbFStatistic.Text        = mlr.FTest.Statistic.ToString("N5");
            tbZStatistic.Text        = mlr.ZTest.Statistic.ToString("N5");
            cbChiSignificant.Checked = mlr.ChiSquareTest.Significant;
            cbFSignificant.Checked   = mlr.FTest.Significant;
            cbZSignificant.Checked   = mlr.ZTest.Significant;

            // Populate projection source table
            string[] cols = independentNames;
            if (!independentNames.Contains(dependentName))
            {
                cols = independentNames.Concatenate(dependentName);
            }

            DataTable projSource = sourceTable.DefaultView.ToTable(false, cols);

            dgvProjectionSource.DataSource = projSource;
        }
        public void DoStepTest()
        {

            double[][] inputs = Matrix.Expand(
                new double[][] {
                    new double[] {0, 0, 0},
                    new double[] {1, 0, 0},
                    new double[] {0, 1, 0},
                    new double[] {1, 1, 0},
                    new double[] {0, 0, 1},
                    new double[] {1, 0, 1},
                    new double[] {0, 1, 1},
                    new double[] {1, 1, 1},
                }, new int[] { 60, 17, 8, 2, 187, 85, 51, 23 });

            double[] outputs = Matrix.Expand(
                new double[] { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
                new int[] { 5, 60 - 5, 2, 17 - 2, 1, 8 - 1, 0, 2 - 0, 35, 187 - 35, 13, 85 - 13, 15, 51 - 15, 8, 23 - 8 });



            var target2 = new LogisticRegressionAnalysis(inputs, outputs);
            target2.Compute();

            Assert.AreEqual(target2.CoefficientValues[0], -2.377661, 0.0001);
            Assert.AreEqual(target2.CoefficientValues[1], -0.067775, 0.0001);
            Assert.AreEqual(target2.CoefficientValues[2], 0.69531, 0.0001);
            Assert.AreEqual(target2.CoefficientValues[3], 0.871939, 0.0001);


            var target = new StepwiseLogisticRegressionAnalysis(
                inputs, outputs,
                new string[] { "x1", "x2", "x3" }, "Y"
            );

            target.Threshold = 0.15;

            int actual;
            actual = target.DoStep();
            Assert.AreEqual(0, actual);

            actual = target.DoStep();
            Assert.AreEqual(-1, actual);


        }
        public void ComputeTest3()
        {
            double[][] inputs = training.Submatrix(null, 0, 3);
            double[] outputs = training.GetColumn(4);

            LogisticRegressionAnalysis regression = new LogisticRegressionAnalysis(inputs, outputs);

            bool expected = false;
            regression.Iterations = 3;
            bool actual = regression.Compute();
            Assert.AreEqual(expected, actual);
        }
        public void ComputeTest4()
        {

            // 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 regression = new LogisticRegressionAnalysis(inputs, output);

            regression.Regularization = 0;

            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 also use the analysis to classify a new patient
            double y = regression.Regression.Compute(new double[] { 87, 1 });

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

            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.IsFalse(odds.HasNaN());

            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.IsFalse(stde.HasNaN());

            Assert.AreEqual(0.75143272858389798, y, 1e-8);
            Assert.IsFalse(Double.IsNaN(y));
        }
Пример #31
0
        public void FromSummary_new_method()
        {
            #region doc_learn_summary
            // 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 }
            };

            // Get input variable and number of positives and negatives
            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 lra = new LogisticRegressionAnalysis();

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

            // 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 = 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;


            // Finally, we can use it to estimate risk for a new patient
            double y = lra.Regression.Score(new double[] { 4 }); // 67.0
            #endregion


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

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

            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();

                expected.Learn(inp, outputs);

                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 = lra;
            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);
        }
        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().ToArray();

            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.IsFalse(odds.HasNaN());

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

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

            LogisticRegressionAnalysis expected;


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

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

                double[][] inp = expanded.GetColumn(0).ToDouble().ToArray();
                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);
                Assert.AreEqual(1.324056323049271, inter);
                Assert.AreEqual(0.042491262992507946, value);
            }



            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);
        }
Пример #33
0
        private void btnSampleRunAnalysis_Click(object sender, EventArgs e)
        {
            // Check requirements
            if (sourceTable == null)
            {
                MessageBox.Show("A sample spreadsheet can be found in the " +
                    "Resources folder in the same directory as this application.",
                    "Please load some data before attempting an analysis");
                return;
            }

            if (checkedListBox1.CheckedItems.Count == 0)
            {
                MessageBox.Show("Please select the dependent input variables to be used in the regression model.",
                    "Please choose at least one input variable");
            }


            // Finishes and save any pending changes to the given data
            dgvAnalysisSource.EndEdit();
            sourceTable.AcceptChanges();

            // Gets the column of the dependent variable
            String dependentName = (string)comboBox1.SelectedItem;
            DataTable dependent = sourceTable.DefaultView.ToTable(false, dependentName);

            // Gets the columns of the independent variables
            List<string> names = new List<string>();
            foreach (string name in checkedListBox1.CheckedItems)
                names.Add(name);

            String[] independentNames = names.ToArray();
            DataTable independent = sourceTable.DefaultView.ToTable(false, independentNames);


            // Creates the input and output matrices from the source data table
            double[][] input = independent.ToArray();
            double[] output = dependent.Columns[dependentName].ToArray();

            double[,] sourceMatrix = sourceTable.ToMatrix(independentNames);

            // Creates the Simple Descriptive Analysis of the given source
            DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, independentNames);
            sda.Compute();

            // Populates statistics overview tab with analysis data
            dgvDistributionMeasures.DataSource = sda.Measures;

            // Creates the Logistic Regression Analysis of the given source
            lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName);


            // Compute the Logistic Regression Analysis
            lra.Compute();

            // Populates coefficient overview with analysis data
            dgvLogisticCoefficients.DataSource = lra.Coefficients;

            // Populate details about the fitted model
            tbChiSquare.Text = lra.ChiSquare.Statistic.ToString("N5");
            tbPValue.Text = lra.ChiSquare.PValue.ToString("N5");
            checkBox1.Checked = lra.ChiSquare.Significant;
            tbDeviance.Text = lra.Deviance.ToString("N5");
            tbLogLikelihood.Text = lra.LogLikelihood.ToString("N5");


            // Create the Multiple Linear Regression Analysis of the given source
            mlr = new MultipleLinearRegressionAnalysis(input, output, independentNames, dependentName, true);

            // Compute the Linear Regression Analysis
            mlr.Compute();

            dgvLinearCoefficients.DataSource = mlr.Coefficients;
            dgvRegressionAnova.DataSource = mlr.Table;

            tbRSquared.Text = mlr.RSquared.ToString("N5");
            tbRSquaredAdj.Text = mlr.RSquareAdjusted.ToString("N5");
            tbChiPValue.Text = mlr.ChiSquareTest.PValue.ToString("N5");
            tbFPValue.Text = mlr.FTest.PValue.ToString("N5");
            tbZPValue.Text = mlr.ZTest.PValue.ToString("N5");
            tbChiStatistic.Text = mlr.ChiSquareTest.Statistic.ToString("N5");
            tbFStatistic.Text = mlr.FTest.Statistic.ToString("N5");
            tbZStatistic.Text = mlr.ZTest.Statistic.ToString("N5");
            cbChiSignificant.Checked = mlr.ChiSquareTest.Significant;
            cbFSignificant.Checked = mlr.FTest.Significant;
            cbZSignificant.Checked = mlr.ZTest.Significant;

            // Populate projection source table
            string[] cols = independentNames;
            if (!independentNames.Contains(dependentName))
                cols = independentNames.Concatenate(dependentName);

            DataTable projSource = sourceTable.DefaultView.ToTable(false, cols);
            dgvProjectionSource.DataSource = projSource;
        }
Пример #34
0
        private void btnGenerateInputData_Click(object sender, EventArgs e)
        {
            //if (this.strProjectPath.Trim() == "")
            //    return;
            if (this.cmbBoundary.Text.Trim() == "")
            {
                MessageBox.Show("请添加边界数据!","提示!",MessageBoxButtons.OK);
                return;
            }
            if (this.cmbRstraint.Text.Trim() == "")
            {
                MessageBox.Show("请添加限制区域数据!", "提示!", MessageBoxButtons.OK);
                return;
            }
            //if (this.txtParameter.Text.Trim() == "")
            //    return;
            if (lsbLayerLandUse.Items.Count == 0)
                return;
            //if (lsbLayerDriverFactor.Items.Count == 0)
            //    return;
            int iLanduseType = 6;

            //ILayer pLayer = null;
            IRaster pRaster = new RasterClass();

            IGeoDataset pGdsMask = null;
            IGeoDataset pGdsRstraint = null;
            IGeoDataset pGdsLanduse = null;
            IGeoDataset pGdsDriverFactor = null;
            IRasterBandCollection pRasterBandColection=(new RasterClass()) as IRasterBandCollection;

            //读取
            double cellSize = 1000;

            //try
            //{
                string sLyrMask = this.cmbBoundary.Text;

                //boundaty-->mask
                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    ILayer pLyr = pMap.get_Layer(i);
                    if (pLyr is IRasterLayer)
                    {
                        if (pLyr.Name == sLyrMask)
                        {
                            IRasterProps pRasterP = (pLyr as IRasterLayer).Raster as IRasterProps;
                            cellSize = pRasterP.MeanCellSize().X;
                            pGdsMask = (pLyr as IRasterLayer).Raster as IGeoDataset;
                        }
                    }
                }

                //data
                //限制区
                string sLyrRstraint = this.cmbRstraint.Text;
                //土地利用数据
                string sLyrLanduse = this.lsbLayerLandUse.Items[0].ToString();

                // 土地利用数据与驱动因子 数据名称(去除格式名)
                // 顺序不能改变  后面 ITable2DTable 使用这个列表进行了名称替换
                lsbNames.Add(sLyrLanduse.Remove(sLyrLanduse.LastIndexOf(".")));
                foreach (string name in lsbLayerDriverFactor.Items)
                {
                    lsbNames.Add(name.Remove(name.LastIndexOf("."))); //去除文件格式
                }

                //驱动因子
                //string[] arr = new string[this.lsbLayerDriverFactor.Items.Count];

                //for (int i = 0; i < this.lsbLayerDriverFactor.Items.Count; i++)
                //{
                //    arr[i]=this.lsbLayerDriverFactor.Items[i].ToString();
                //}

                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    ILayer pLyr = pMap.get_Layer(i);
                    if (pLyr is IRasterLayer)
                    {
                        //IRaster curRaster = new RasterClass();
                        if (pLyr.Name == sLyrRstraint)
                        {
                            //curRaster = (pLyr as IRasterLayer).Raster;
                            //(pLyr as IRasterLayer).Raster.
                            pGdsRstraint = (pLyr as IRasterLayer).Raster as IGeoDataset;

                            //限制数据 region.grid
                            string ascFileNameRstraint = strProjectPath + "\\region.grid";
                            Rater2Ascii(pGdsMask,cellSize , pGdsRstraint, ascFileNameRstraint);
                        }

                        //土地利用数据
                        if (pLyr.Name == sLyrLanduse)
                        {
                            this.rtxtState.AppendText("读取土地利用参数数据...\n");
                            this.rtxtState.ScrollToCaret();

                            //curRaster = (pLyr as IRasterLayer).Raster;
                            pGdsLanduse = (pLyr as IRasterLayer).Raster as IGeoDataset;
                            //land use 添加到 IRasterBandCollection
                            IRasterBandCollection rasterbands = (IRasterBandCollection)(pLyr as IRasterLayer).Raster;
                            IRasterBand rasterband = rasterbands.Item(0);
                            pRasterBandColection.AppendBand(rasterband);

                            //pRasterBandColection = curRaster as IRasterBandCollection;
                            //pRasterBandColection.AppendBand(pRaster as IRasterBand);

                            string ascFileNameLanduse = strProjectPath + "\\cov1_all.0";
                            //cov1_0.0;cov1_1.0;
                            Rater2Ascii(pGdsMask, cellSize, pGdsLanduse, ascFileNameLanduse);
                            //

                            //将土地利用数据拆分

                            StreamReader sr = new StreamReader(ascFileNameLanduse, System.Text.Encoding.Default);
                            //try
                            //{

                            //使用StreamReader类来读取文件
                            sr.BaseStream.Seek(0, SeekOrigin.Begin);
                            // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
                            //读取头文件
                            string[] header = new string[6];

                            for (int j = 0; j < 6; j++)
                            {
                                header[j] = sr.ReadLine();
                            }
                            //行列数
                            string[] ncols = header[0].Split(' ');
                            string[] nrows = header[1].Split(' ');
                            int icol = int.Parse(ncols[ncols.Length - 1]);
                            int irow = int.Parse(nrows[nrows.Length - 1]);

                            int[,] iLanduse = new int[irow, icol];
                            //
                            string strLine = sr.ReadLine();
                            string[] strData;
                            int ir = 0;
                            while (ir < irow)
                            //while (strLine != null)
                            {
                                strData = strLine.Split(' ');
                                for (int ic = 0; ic < icol; ic++)
                                {
                                    iLanduse[ir, ic] = int.Parse(strData[ic]);
                                }
                                strLine = sr.ReadLine();
                                ir++;
                            }
                            //关闭此StreamReader对象
                            sr.Close();
                            //输出相应的土地利用数据
                            DataTable2Txt(header, iLanduseType, iLanduse, strProjectPath);

                            //}
                            //catch (Exception ex)
                            //{
                            //MessageBox.Show(ex.Message);
                            sr.Close();
                            //}
                        }
                    }
                }
                this.rtxtState.AppendText("输出土地利用参数数据成功。\n");
                this.rtxtState.AppendText("读取驱动因子数据...\n");
                this.rtxtState.ScrollToCaret();
                for (int ifac = 0; ifac < lsbLayerDriverFactor.Items.Count; ifac++)
                {
                    for (int i = 0; i < pMap.LayerCount; i++)
                    {
                        ILayer pLyr = pMap.get_Layer(i);
                        if (pLyr is IRasterLayer)
                        {
                        //输出驱动因子数据  sc1gr0.grid

                            string sFacName = lsbLayerDriverFactor.Items[ifac].ToString();
                            if (pLyr.Name == sFacName)
                            {
                                //IRaster curRaster = new RasterClass();
                                //curRaster = (pLyr as IRasterLayer).Raster;
                                pGdsDriverFactor = (pLyr as IRasterLayer).Raster as IGeoDataset;

                                string ascFileNameFac = strProjectPath + "\\sc1gr"+ifac.ToString()+".grid";
                                //cov1_0.0;cov1_1.0;
                                this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】\n");
                                //IGeoDataset curMask = null;
                                //curMask = pGdsMask;
                                Rater2Ascii(pGdsMask, cellSize, pGdsDriverFactor, ascFileNameFac);
                                this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】成功。\n");
                                this.rtxtState.ScrollToCaret();
                                //mask 添加到 IRasterBandCollection
                                IRasterBandCollection rasterbands = (IRasterBandCollection)(pLyr as IRasterLayer).Raster;
                                IRasterBand rasterband = rasterbands.Item(0);
                                pRasterBandColection.AppendBand(rasterband);
                                //pRasterBandColection.Add(pRaster as IRasterBand,ifac+1);

                            }
                        }
                    }
                }
                this.rtxtState.AppendText("开始制备驱动因子参数...\n");
                this.rtxtState.ScrollToCaret();

                //IGeoDataset curtestGeo = null;
                ////boundaty-->mask
                //for (int i = 0; i < pMap.LayerCount; i++)
                //{
                //    ILayer pLyr = pMap.get_Layer(i);
                //    if (pLyr is IRasterLayer)
                //    {
                //        if (pLyr.Name == sLyrMask)
                //        {
                //            curtestGeo = ((pLyr as IRasterLayer).Raster) as IGeoDataset;
                //        }
                //    }
                //}

                //sample data
                ITable itFactors = ExportSample(pGdsMask, pRasterBandColection);

                //logistic 回归分析

                //lsbNames.AddRange(names);
                DataTable dtFactors = ITable2DTable(itFactors);

                itFactors = null;
                //MessageBox.Show(dtFactors.Columns[0].ColumnName + ";" + dtFactors.Columns[1].ColumnName + ";" + dtFactors.Columns[2].ColumnName);

                //制备驱动力参数文件
                this.rtxtState.AppendText("读取驱动因子数据表格数据...\n");
                this.rtxtState.ScrollToCaret();
                LogisticRegressionAnalysis lra;
                // Gets the columns of the independent variables
                List<string> names = new List<string>();
                foreach (string name in lsbLayerDriverFactor.Items)
                {
                    names.Add(name.Remove(name.LastIndexOf("."))); //去除文件格式
                }

                String[] independentNames = names.ToArray();
                DataTable independent = dtFactors.DefaultView.ToTable(false, independentNames);
                // Creates the input and output matrices from the source data table
                double[][] input = independent.ToArray();
                double[,] sourceMatrix = dtFactors.ToMatrix(independentNames);

                StreamWriter sw = new StreamWriter(strProjectPath + "\\alloc1.reg", false);
                for(int ild=1; ild< iLanduseType; ild++)
                {

                    String landuseName = (string)this.lsbLayerLandUse.Items[0].ToString();
                    this.rtxtState.AppendText("开始制备土地利用类型【" + ild.ToString() + "】驱动因子参数...\n");
                    this.rtxtState.ScrollToCaret();
                    DataColumn taxColumn =new DataColumn();
                    taxColumn.DataType = System.Type.GetType("System.Int32");
                    taxColumn.ColumnName ="sysland"+ild.ToString();//列名
                    taxColumn.Expression = "iif("+lsbNames[0]+" = "+ild.ToString()+",1,0)";//设置该列得表达式,用于计算列中的值或创建聚合列
                    dtFactors.Columns.Add(taxColumn);
                    string dependentName = "sysland" + ild.ToString();

                    DataTable dependent = dtFactors.DefaultView.ToTable(false, dependentName);
                    double[] output = dependent.Columns[dependentName].ToArray();

                    // Creates the Simple Descriptive Analysis of the given source
                    DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, independentNames);
                    sda.Compute();

                    // Populates statistics overview tab with analysis data
                    //dgvDistributionMeasures.DataSource = sda.Measures;

                    // Creates the Logistic Regression Analysis of the given source
                    lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName);

                    // Compute the Logistic Regression Analysis
                    lra.Compute();

                    // Populates coefficient overview with analysis data
                    //lra.Coefficients;

                    //MessageBox.Show(lra.Coefficients.Count.ToString());
                    //MessageBox.Show(lra.CoefficientValues[0].ToString());

                    //string str_check = listBox_deVar.Items[var_count].ToString().ToLower();
                    string st = ild.ToString();
                    int Relength = lra.CoefficientValues.Length;

                    int number = 0;
                    for (int i = 1; i < Relength; i++)
                    {
                        //if (< 0.05)
                        //{
                            number++;
                        //}
                    }
                    RegressionResult.Items.Add(st);
                    RegressionResult.Items.Add("\t" + Math.Round(lra.CoefficientValues[1], 6));
                    RegressionResult.Items.Add(number);
                    int var_number = 0;
                    for (int i = 0; i < Relength; i++)//改过了0=1
                    {
                        //if ( < 0.05)
                        //{
                        RegressionResult.Items.Add("\t" + Math.Round(lra.CoefficientValues[i], 6) + "\t" + var_number);
                        //}
                        var_number = var_number + 1;
                    }

                    // 保存alloc1.reg 文件
                    sw.WriteLine(st);
                    sw.WriteLine("\t" + Math.Round(lra.CoefficientValues[1], 6));//改过了1=0
                    sw.WriteLine(number);
                    int var_number2 = 0;
                    for (int i = 0; i < Relength; i++)//改过了0=1
                    {
                        //if ( < 0.05)
                        //{
                        sw.WriteLine("\t" + Math.Round(lra.CoefficientValues[i],6) + "\t" + var_number2);
                        //}
                        var_number2 = var_number2 + 1;
                    }
                    //progressBar1.Value = var_count + 1;
                }
                sw.Close();

                this.rtxtState.AppendText("制备驱动因子参数成功。\n");
                this.rtxtState.ScrollToCaret();
                MessageBox.Show("制备驱动因子参数成功!","提示",MessageBoxButtons.OK);
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }