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); } }
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); }
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 }; var target = new LogisticRegressionAnalysis(trainInput, trainOutput); target.Regularization = 1e-10; target.Compute(); double[] coef = target.Coefficients.Apply(x => x.Value); Assert.AreEqual(coef[0], -19.360661491141897); Assert.AreEqual(coef[1], 19.702873967721807); Assert.AreEqual(coef[2], 19.702873967721807); double output = target.Regression.Compute(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(trainInput, trainOutput); Assert.IsTrue(target.Compute()); double[] actual = target.Regression.Compute(trainInput); //string str = actual.ToCSharp(); 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); }
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 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; }
public void FromSummaryTest1() { // Suppose we have a (fictitious) data set about patients who // underwent cardiac surgery. The first column gives the number // of arterial bypasses performed during the surgery. The second // column gives the number of patients whose surgery went well, // while the third column gives the number of patients who had // at least one complication during the surgery. // int[,] data = { // # of stents success complications { 1, 140, 45 }, { 2, 130, 60 }, { 3, 150, 31 }, { 4, 96, 65 } }; double[][] inputs = data.GetColumn(0).ToDouble().ToJagged(); int[] positive = data.GetColumn(1); int[] negative = data.GetColumn(2); // Create a new Logistic Regression Analysis from the summary data var regression = LogisticRegressionAnalysis.FromSummary(inputs, positive, negative); regression.Compute(); // compute the analysis. // Now we can show a summary of the analysis // Accord.Controls.DataGridBox.Show(regression.Coefficients); // We can also investigate all parameters individually. For // example the coefficients values will be available at the // vector double[] coef = regression.CoefficientValues; // The first value refers to the model's intercept term. We // can also retrieve the odds ratios and standard errors: double[] odds = regression.OddsRatios; double[] stde = regression.StandardErrors; // Finally, we can use it to estimate risk for a new patient double y = regression.Regression.Compute(new double[] { 4 }); Assert.AreEqual(3.7586367581050162, odds[0], 1e-8); Assert.AreEqual(0.85772731075090014, odds[1], 1e-8); Assert.AreEqual(0.20884336554629004, stde[0], 1e-8); Assert.AreEqual(0.075837785246620285, stde[1], 1e-8); Assert.AreEqual(0.67044096045332713, y, 1e-8); LogisticRegressionAnalysis expected; { int[] qtr = data.GetColumn(0); var expanded = Accord.Statistics.Tools.Expand(qtr, positive, negative); double[][] inp = expanded.GetColumn(0).ToDouble().ToJagged(); double[] outputs = expanded.GetColumn(1).ToDouble(); expected = new LogisticRegressionAnalysis(inp, outputs); expected.Compute(); double slope = expected.Coefficients[1].Value; // should return -0.153 double inter = expected.Coefficients[0].Value; double value = expected.ChiSquare.PValue; // should return 0.042 Assert.AreEqual(-0.15346904821339602, slope, 1e-8); Assert.AreEqual(1.324056323049271, inter, 1e-8); Assert.AreEqual(0.042491262992507946, value, 1e-8); } var actual = regression; Assert.AreEqual(expected.Coefficients[0].Value, actual.Coefficients[0].Value, 1e-8); Assert.AreEqual(expected.Coefficients[1].Value, actual.Coefficients[1].Value, 1e-8); Assert.AreEqual(expected.ChiSquare.PValue, actual.ChiSquare.PValue, 1e-8); Assert.AreEqual(expected.WaldTests[0].PValue, actual.WaldTests[0].PValue, 1e-8); Assert.AreEqual(expected.WaldTests[1].PValue, actual.WaldTests[1].PValue, 1e-8); Assert.AreEqual(expected.Confidences[0].Max, actual.Confidences[0].Max, 1e-6); Assert.AreEqual(expected.Confidences[0].Min, actual.Confidences[0].Min, 1e-6); Assert.AreEqual(expected.Confidences[1].Max, actual.Confidences[1].Max, 1e-6); Assert.AreEqual(expected.Confidences[1].Min, actual.Confidences[1].Min, 1e-6); }
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.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); }
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); //} }
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 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)); }
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 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)); }
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 }; var target = new LogisticRegressionAnalysis(trainInput, trainOutput); target.Regularization = 1e-10; target.Compute(); double[] coef = target.Coefficients.Apply(x => x.Value); Assert.AreEqual(coef[0], -19.360661491141897); Assert.AreEqual(coef[1], 19.702873967721807); Assert.AreEqual(coef[2], 19.702873967721807); double output = target.Regression.Compute(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(trainInput, trainOutput); Assert.IsTrue(target.Compute()); double[] actual = target.Regression.Compute(trainInput); //string str = actual.ToCSharp(); 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 override void Train(double[][] predictors, double[] results) { regression = new LogisticRegressionAnalysis(predictors, results); regression.Compute(); }
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); }
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)); }
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 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); }
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; }
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); //} }