C# Advanced One Way Ranova Example

← All NMath Stats Code Examples

```using System;

using CenterSpace.NMath.Core;
using CenterSpace.NMath.Stats;

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

static void Main( string[] args )
{
// Construct a OneWayRanova using a DoubleMatrix for the data to be
// analyzed. Each row of the matrix holds the data for an individual
// subject while each column represents a treatment.
var M = new DoubleMatrix( "10x4 [ 180 200 160 200  230 250 200 220  280 310 260 270 180 200 160 200 190 210 170 210 140 160 120 110  270 300 250 260  110 130 100 100 190 210 170 210 230 250 200 220 ]" );
var ranova = new OneWayRanova( M );

// Print out the grand mean and mean for each subject.
Console.WriteLine();
Console.WriteLine( "GrandMean = {0}", ranova.GrandMean );
Console.WriteLine();

DoubleVector subjectMeans = ranova.SubjectMeans;
for ( int i = 0; i < subjectMeans.Length; ++i )
{
Console.WriteLine( "Subject {0} mean = {1}", i, subjectMeans[i] );
}
Console.WriteLine();

// Fetch the RANOVA table and use it to print out the information
// in a traditional RANOVA table.
OneWayRanovaTable ranovaTable = ranova.RanovaTable;
Console.WriteLine( "Source: Within Subject" );
Console.WriteLine( "  Sum of Squares     = {0}", ranovaTable.SumOfSquaresWithinSubject.ToString( "G7" ) );
Console.WriteLine( "  Degrees of Freedom = {0}", ranovaTable.DegreesOfFreedomWithinSubject );
Console.WriteLine( "  Mean Square        = {0}" + Environment.NewLine, ranovaTable.MeanSquareWithinSubject.ToString( "G7" ) );

Console.WriteLine( "Source: Treatment" );
Console.WriteLine( "  Sum of Squares     = {0}", ranovaTable.SumOfSquaresTreatment.ToString( "G7" ) );
Console.WriteLine( "  Degrees of Freedom = {0}", ranovaTable.DegreesOfFreedomTreatment.ToString( "G7" ) );
Console.WriteLine( "  Mean Square        = {0}" + Environment.NewLine, ranovaTable.MeanSquareTreatment.ToString( "G7" ) );

Console.WriteLine( "Source: Error" );
Console.WriteLine( "  Sum of Squares     = {0}", ranovaTable.SumOfSquaresError.ToString( "G7" ) );
Console.WriteLine( "  Degrees of Freedom = {0}", ranovaTable.DegreesOfFreedomError.ToString( "G7" ) );
Console.WriteLine( "  Mean Square        = {0}" + Environment.NewLine, ranovaTable.MeanSquareError.ToString( "G7" ) );

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

// Print out the F statistic for the test of the null hypothesis that
// there is no treatment effect.
double F = ranovaTable.FStatistic;

// What is the critical value for F for alpha = 0.05?
double alpha = 0.05;
double criticalValue = ranova.FStatisticCriticalValue( alpha );

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

// Based on the value of F and the critical value decide whether or not
// to reject the null hypothesis.
if ( F > criticalValue )
{
Console.WriteLine( "Reject the null hypothesis of no treatment effect and" );
Console.WriteLine( "conclude that at least one treatment produces a different" );
Console.WriteLine( "result {0} significance level.", alpha );
}
else
{
Console.WriteLine( "Accept the null hypothesis of no treatment effect " );
Console.WriteLine( "at the {0} significance level.", alpha );
}

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