C# Advanced One Way Anova Example

[TOC]

using System;
using System.IO;

using CenterSpace.NMath.Stats;

namespace CenterSpace.NMath.Stats.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing some of the advanced features of class OneWayAnova.
  /// </summary>
  class AdvancedOneWayAnovaExample
  {

    static void Main(string[] args)
    {
      // Name of the file containing the data on which we will operate.
      string filename = "..\\..\\AdvancedOneWayAnovaExample.dat";

      // Construct a DataFrame from the data in the file. The file contains 
      // data for three goups of people, healty people, melancholic depressed
      // people, and nonmelancholic depressed people. For each of these 
      // groups the cortisol levels in the blood are measured*
      // The data consists of two columns. The first column contains the name
      // of the group that an individual belongs to and the second column
      // contains the level of cortisol for the individual.
      DataFrame anovaData = anovaData = DataFrame.Load(filename, false, false, " ", true);

      // The names of the three groups in the study.
      string healthy = "Healthy";
      string nonMelancholic = "Nonmelanchonic";
      string melancholic = "Melancholic";

      // Construct a OneWayAnova instance from the DataFrame. The first 
      // integer argument specifies which column contains the group names
      // and the second integer argument specifies which column contains
      // the numeric data (as zero based indicies).
      OneWayAnova anova = new OneWayAnova(anovaData, 0, 1);

      // Print out the means for each of the three groups.
      double healthGroupMean = anova.GetGroupMean(healthy);
      double nonMelancholicGroupMean = anova.GetGroupMean(nonMelancholic);
      double melancholicGroupMean = anova.GetGroupMean(melancholic);

      Console.WriteLine();
      Console.WriteLine("Healthy group mean        = {0}", healthGroupMean);
      Console.WriteLine("NonMelancholic group mean = {0}", nonMelancholicGroupMean);
      Console.WriteLine("Melancholic group mean    = {0}", melancholicGroupMean);

      // Print out the sum of squares, degrees of freedom and mean square
      // for each of the source between groups, within groups, and total.
      Console.WriteLine(Environment.NewLine + "Between Groups:");
      Console.WriteLine("  Sum of Squares     = {0}", anova.AnovaTable.SumOfSquaresBetween);
      Console.WriteLine("  Degrees of Freedom = {0}", anova.AnovaTable.DegreesOfFreedomBetween);
      Console.WriteLine("  Mean Square        = {0}" + Environment.NewLine, anova.AnovaTable.MeanSquareBetween);

      Console.WriteLine("Within Groups:");
      Console.WriteLine("  Sum of Squares     = {0}", anova.AnovaTable.SumOfSquaresWithin);
      Console.WriteLine("  Degrees of Freedom = {0}", anova.AnovaTable.DegreesOfFreedomWithin);
      Console.WriteLine("  Mean Square        = {0}" + Environment.NewLine, anova.AnovaTable.MeanSquareWithin);

      Console.WriteLine("Total:");
      Console.WriteLine("  Sum of Squares     = {0}", anova.AnovaTable.SumOfSquaresTotal);
      Console.WriteLine("  Degrees of Freedom = {0}", anova.AnovaTable.DegreesOfFreedomTotal);
      Console.WriteLine("  Mean Square        = {0}" + Environment.NewLine, anova.AnovaTable.MeanSquareTotal);

      // Fetch the F statistic and the critical value for the F statistic and
      // decide whether or not to reject the null hypothesis that all group
      // means are the same.
      double F = anova.AnovaTable.FStatistic;
      double alpha = 0.01;
      double criticalValue = anova.FStatisticCriticalValue(alpha);

      Console.WriteLine("F = {0}", F);
      Console.WriteLine("Critical value for alpha < {0} = {1}", alpha, criticalValue);

      if (F > criticalValue)
      {
        Console.WriteLine("Reject the null hypothesis that the means are the ");
        Console.WriteLine("same in all three groups at the {0} significance level.", alpha);
      }
      else
      {
        Console.WriteLine("Accept the null hypothesis that the means are the ");
        Console.WriteLine("same in all three groups at the {0} significance level.", alpha);
      }

      Console.WriteLine();
      Console.WriteLine("Press Enter Key");
      Console.Read();

    }  // Main

  }  // class

}  // namespace

// * Source: L. H. Price, D. S. Charney, A. L. Rubin, and F. R. 
// Henniger, "alpha2 Adregenric Receptor Function in Depression:
// The Cortisol Response to Yohimbine," Arch. Gen. Psychiatry 
// 43:849-858, 1986.
[TOC]