Changeset 11696


Ignore:
Timestamp:
12/18/14 16:12:19 (8 years ago)
Author:
ascheibe
Message:

#2031

  • improved code of statistical testing view
  • improved documentation
Location:
branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/StatisticalTesting/HeuristicLab.Analysis.Statistics/3.3/StatisticalTestsView.cs

    r11695 r11696  
    9191    protected override void RegisterContentEvents() {
    9292      base.RegisterContentEvents();
    93       Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    94       Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
     93      Content.ColumnsChanged += Content_ColumnsChanged;
     94      Content.RowsChanged += Content_RowsChanged;
    9595      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    9696      Content.UpdateOfRunsInProgressChanged += Content_UpdateOfRunsInProgressChanged;
     
    9999    protected override void DeregisterContentEvents() {
    100100      base.DeregisterContentEvents();
    101       Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
    102       Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
     101      Content.ColumnsChanged -= Content_ColumnsChanged;
     102      Content.RowsChanged -= Content_RowsChanged;
    103103      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
    104104      Content.UpdateOfRunsInProgressChanged -= Content_UpdateOfRunsInProgressChanged;
    105105    }
    106106
     107    void Content_RowsChanged(object sender, EventArgs e) {
     108      RebuildDataTable();
     109    }
     110
     111    void Content_ColumnsChanged(object sender, EventArgs e) {
     112      RebuildDataTable();
     113    }
     114
    107115    private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    108       RebuildDataTable();
    109     }
    110 
    111     private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    112       RebuildDataTable();
    113     }
    114 
    115     private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
    116116      RebuildDataTable();
    117117    }
     
    301301
    302302    private void CalculateValuesAsync(string groupName) {
    303       TestAllGroups();
    304       CalculateNormality();
    305       CalculateNormalityDetails();
     303      CalculateAllGroupsTest();
     304      CalculateNormalityTest();
    306305      CalculatePairwiseTest(groupName);
    307       CalculatePairwiseTestDetails(groupName);
    308306
    309307      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
     
    320318    private void CalculatePairwiseAsync(string groupName) {
    321319      CalculatePairwiseTest(groupName);
    322       CalculatePairwiseTestDetails(groupName);
    323320
    324321      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(pairwiseTestGroupBox);
    325322    }
    326323
    327     private void TestAllGroups() {
     324    private void CalculateAllGroupsTest() {
    328325      double pval = KruskalWallisTest.Test(data);
    329326      pValTextBox.Text = pval.ToString();
     
    341338    }
    342339
    343     private void CalculateNormality() {
     340    private void CalculateNormalityTest() {
    344341      double val;
    345342      List<double> res = new List<double>();
     343      DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
     344      pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
     345      pValsMatrix.RowNames = new string[] { "p-Value" };
    346346
    347347      for (int i = 0; i < data.Length; i++) {
    348348        alglib.jarqueberatest(data[i], data[i].Length, out val);
    349349        res.Add(val);
    350       }
    351 
    352       // p-value is below significance level and thus the null hypothesis (data is normally distributed) is rejected.
     350        pValsMatrix[0, i] = val;
     351      }
     352
     353      // p-value is below significance level and thus the null hypothesis (data is normally distributed) is rejected
    353354      if (res.Any(x => x < significanceLevel)) {
    354355        this.Invoke(new Action(() => {
     
    361362          normalityTextLabel.Text = "All sample data is normally distributed";
    362363        }));
    363       }
    364     }
    365 
    366     private void CalculateNormalityDetails() {
    367       DoubleMatrix pValsMatrix = new DoubleMatrix(1, stringConvertibleMatrixView.Content.Columns);
    368       pValsMatrix.ColumnNames = stringConvertibleMatrixView.Content.ColumnNames;
    369       pValsMatrix.RowNames = new string[] { "p-Value" };
    370 
    371       double val;
    372       for (int i = 0; i < data.Length; i++) {
    373         alglib.jarqueberatest(data[i], data[i].Length, out val);
    374         pValsMatrix[0, i] = val;
    375364      }
    376365
     
    381370    }
    382371
    383     private void CalculatePairwiseTest(string groupName) {
    384       int colIndex = 0;
    385       IEnumerable<string> columnNames = null;
    386       this.Invoke(new Action(() => { columnNames = stringConvertibleMatrixView.Content.ColumnNames; }));
    387 
    388       foreach (string col in columnNames) {
    389         if (col == groupName) {
    390           break;
    391         }
    392         colIndex++;
    393       }
    394 
    395       double[][] newData = FilterDataForPairwiseTest(colIndex);
    396       int cnt = 0;
    397       for (int i = 0; i < newData.Length; i++) {
    398         double mwuBothtails = PairwiseTest.MannWhitneyUTest(data[colIndex], newData[i]);
    399         if (mwuBothtails > significanceLevel) {
    400           cnt++;
    401         }
    402       }
    403 
    404       double ratio = ((double)cnt) / (data.Length - 1) * 100.0;
     372    private void ShowPairwiseResult(int nrOfEqualDistributions) {
     373      double ratio = ((double)nrOfEqualDistributions) / (data.Length - 1) * 100.0;
    405374      equalDistsTextBox.Text = ratio.ToString() + " %";
    406375
    407       if (cnt == 0) {
     376      if (nrOfEqualDistributions == 0) {
    408377        this.Invoke(new Action(() => {
    409378          pairwiseLabel.Image = HeuristicLab.Analysis.Statistics.Resources.Default;
     
    416385        }));
    417386      }
     387    }
     388
     389    private void CalculatePairwiseTest(string groupName) {
     390      var columnNames = stringConvertibleMatrixView.Content.ColumnNames.ToList();
     391      int colIndex = columnNames.IndexOf(groupName);
     392      columnNames = columnNames.Where(x => x != groupName).ToList();
     393
     394      double[][] newData = FilterDataForPairwiseTest(colIndex);
     395
     396      var rowNames = new string[] { "p-Value of Mann-Whitney U", "Adjusted p-Value of Mann-Whitney U",
     397            "p-Value of T-Test", "Adjusted p-Value of T-Test", "Cohen's d", "Hedges' g" };
     398
     399      DoubleMatrix pValsMatrix = new DoubleMatrix(rowNames.Length, columnNames.Count());
     400      pValsMatrix.ColumnNames = columnNames;
     401      pValsMatrix.RowNames = rowNames;
     402
     403      double mwuBothTails;
     404      double tTestBothTails;
     405      double[] mwuPValues = new double[newData.Length];
     406      double[] tTestPValues = new double[newData.Length];
     407      bool[] decision = null;
     408      double[] adjustedMwuPValues = null;
     409      double[] adjustedTtestPValues = null;
     410      int cnt = 0;
     411
     412      for (int i = 0; i < newData.Length; i++) {
     413        mwuBothTails = PairwiseTest.MannWhitneyUTest(data[colIndex], newData[i]);
     414        tTestBothTails = PairwiseTest.TTest(data[colIndex], newData[i]);
     415        mwuPValues[i] = mwuBothTails;
     416        tTestPValues[i] = tTestBothTails;
     417
     418        if (mwuBothTails > significanceLevel) {
     419          cnt++;
     420        }
     421      }
     422
     423      adjustedMwuPValues = BonferroniHolm.Calculate(significanceLevel, mwuPValues, out decision);
     424      adjustedTtestPValues = BonferroniHolm.Calculate(significanceLevel, tTestPValues, out decision);
     425
     426      for (int i = 0; i < newData.Length; i++) {
     427        pValsMatrix[0, i] = mwuPValues[i];
     428        pValsMatrix[1, i] = adjustedMwuPValues[i];
     429        pValsMatrix[2, i] = tTestPValues[i];
     430        pValsMatrix[3, i] = adjustedTtestPValues[i];
     431        pValsMatrix[4, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], newData[i]);
     432        pValsMatrix[5, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], newData[i]);
     433      }
     434
     435      this.Invoke(new Action(() => {
     436        pairwiseStringConvertibleMatrixView.Content = pValsMatrix;
     437        pairwiseStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
     438      }));
     439
     440      ShowPairwiseResult(cnt);
    418441    }
    419442
     
    447470    }
    448471
    449     private void CalculatePairwiseTestDetails(string groupName) {
    450       int colIndex = 0;
    451       IEnumerable<string> columnNames = null;
    452       this.Invoke(new Action(() => { columnNames = stringConvertibleMatrixView.Content.ColumnNames; }));
    453 
    454       foreach (string col in columnNames) {
    455         if (col == groupName) {
    456           break;
    457         }
    458         colIndex++;
    459       }
    460 
    461       double[][] newData = FilterDataForPairwiseTest(colIndex);
    462 
    463       columnNames = columnNames.Where(x => x != groupName).ToList();
    464 
    465       var rowNames = new string[] { "p-Value of Mann-Whitney U", "Adjusted p-Value of Mann-Whitney U",
    466             "p-Value of T-Test", "Adjusted p-Value of T-Test", "Cohen's d", "Hedges' g" };
    467 
    468       DoubleMatrix pValsMatrix = new DoubleMatrix(rowNames.Length, columnNames.Count());
    469       pValsMatrix.ColumnNames = columnNames;
    470       pValsMatrix.RowNames = rowNames;
    471 
    472       double mwuBothtails;
    473       double tTestBothTails;
    474       double[] mwuPValues = new double[newData.Length];
    475       double[] tTestPValues = new double[newData.Length];
    476       bool[] decision = null;
    477       double[] adjustedMwuPValues = null;
    478       double[] adjustedTtestPValues = null;
    479 
    480       for (int i = 0; i < newData.Length; i++) {
    481         mwuBothtails = PairwiseTest.MannWhitneyUTest(data[colIndex], newData[i]);
    482         tTestBothTails = PairwiseTest.TTest(data[colIndex], newData[i]);
    483         mwuPValues[i] = mwuBothtails;
    484         tTestPValues[i] = tTestBothTails;
    485       }
    486 
    487       adjustedMwuPValues = BonferroniHolm.Calculate(significanceLevel, mwuPValues, out decision);
    488       adjustedTtestPValues = BonferroniHolm.Calculate(significanceLevel, tTestPValues, out decision);
    489 
    490       for (int i = 0; i < newData.Length; i++) {
    491         pValsMatrix[0, i] = mwuPValues[i];
    492         pValsMatrix[1, i] = adjustedMwuPValues[i];
    493         pValsMatrix[2, i] = tTestPValues[i];
    494         pValsMatrix[3, i] = adjustedTtestPValues[i];
    495         pValsMatrix[4, i] = SampleSizeDetermination.CalculateCohensD(data[colIndex], newData[i]);
    496         pValsMatrix[5, i] = SampleSizeDetermination.CalculateHedgesG(data[colIndex], newData[i]);
    497       }
    498 
    499       this.Invoke(new Action(() => {
    500         pairwiseStringConvertibleMatrixView.Content = pValsMatrix;
    501         pairwiseStringConvertibleMatrixView.DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
    502       }));
    503     }
    504 
    505472    private void openBoxPlotToolStripMenuItem_Click(object sender, EventArgs e) {
    506473      RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
Note: See TracChangeset for help on using the changeset viewer.