C# Advanced One Way Anova Example

← All NMath Stats Code Examples

 

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 )
    {
      // Construct a DataFrame from the data in the file. The file contains 
      // data for three groups of people, healthy 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 = DataFrame.Load( "AdvancedOneWayAnovaExample.dat", false, false, " ", true );

      // The names of the three groups in the study.
      string healthy = "Healthy";
      string nonMelancholic = "Nonmelancholic";
      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 indices).
      var 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.ToString( "G7" ) );
      Console.WriteLine( "NonMelancholic group mean = {0}", nonMelancholicGroupMean.ToString( "G7" ) );
      Console.WriteLine( "Melancholic group mean    = {0}", melancholicGroupMean.ToString( "G7" ) );

      // 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.ToString( "G7" ) );
      Console.WriteLine( "  Degrees of Freedom = {0}", anova.AnovaTable.DegreesOfFreedomBetween );
      Console.WriteLine( "  Mean Square        = {0}" + Environment.NewLine, anova.AnovaTable.MeanSquareBetween.ToString( "G7" ) );

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

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

      // 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.ToString( "G5" ) );

      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.
← All NMath Stats Code Examples
Top