Two Way ANOVA Demo Example (C#)
[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
* All rights reserved.
*
* Use of this copyright notice is precautionary only, and does not imply
* publication or disclosure. The content of this work contains confidential
* and proprietary information of Simplexar Software Limited. Any duplication,
* modification, distribution, or disclosure in any form, in whole, or in part,
* is strictly prohibited without express prior written permission.
*/
// Get namespaces.
using System;
using Simplexar.Statsar;
using Simplexar.Statsar.Anova;
// Start namespace.
namespace Simplexar.Examples.Statsar
{
/// <summary>
/// This example illustrates how to perform a two way
/// analysis of variance by using the TwoWayAnova class
/// from the Statsar.Simplexar.Anova namespace.
/// </summary>
public static class Example
{
/// <summary>
/// The application entry point.
/// </summary>
public static void Main()
{
try
{
RunExample();
}
catch(Exception exception)
{
Console.WriteLine(exception);
}
Console.WriteLine("Press ENTER to terminate.");
Console.ReadLine();
}
private static void RunExample()
{
// Create sheet.
StatsCalculator calculator = new StatsCalculator();
DataSheet sheet = calculator.Sheet;
ColumnList columns = sheet.Columns;
columns.Add("Labs", typeof(int));
columns.Add("Materials", typeof(int));
columns.Add("Coating", typeof(double));
// Populate data.
RowList rows = sheet.Rows;
rows.Add(1, 1, 4.1);
rows.Add(1, 1, 3.9);
rows.Add(1, 1, 4.3);
rows.Add(1, 2, 3.1);
rows.Add(1, 2, 2.8);
rows.Add(1, 2, 3.3);
rows.Add(1, 3, 3.5);
rows.Add(1, 3, 3.2);
rows.Add(1, 3, 3.6);
rows.Add(2, 1, 2.7);
rows.Add(2, 1, 3.1);
rows.Add(2, 1, 2.6);
rows.Add(2, 2, 1.9);
rows.Add(2, 2, 2.2);
rows.Add(2, 2, 2.3);
rows.Add(2, 3, 2.7);
rows.Add(2, 3, 2.3);
rows.Add(2, 3, 2.5);
Console.WriteLine(sheet);
// Perform analysis of variance.
// Factor A is stored in the "Labs" column.
// Factor B is stored in the "Materials" column.
// Observations are stored in the "Coating" column.
TwoWayAnova anova = new TwoWayAnova(
sheet, "Labs", "Materials", "Coating");
// Access ANOVA table. This holds the results.
TwoWayAnovaTable anovaTable = anova.AnovaTable;
Console.WriteLine(anovaTable);
// Access individual ANOVA properties.
Console.WriteLine(
"Factor A sum of squares: {0}",
anovaTable.SumOfSquareFactorA);
Console.WriteLine(
"Factor B sum of squares: {0}",
anovaTable.SumOfSquareFactorB);
Console.WriteLine(
"Interaction sum of squares: {0}",
anovaTable.SumOfSquareInteraction);
Console.WriteLine(
"Error sum of squares: {0}",
anovaTable.SumOfSquareError);
Console.WriteLine(
"Total sum of squares: {0}",
anovaTable.SumOfSquareTotal);
Console.WriteLine(
"Factor A degrees of freedom: {0}",
anovaTable.DegreesOfFreedomFactorA);
Console.WriteLine(
"Factor B degrees of freedom: {0}",
anovaTable.DegreesOfFreedomFactorB);
Console.WriteLine(
"Interaction degrees of freedom: {0}",
anovaTable.DegreesOfFreedomInteraction);
Console.WriteLine(
"Error degrees of freedom: {0}",
anovaTable.DegreesOfFreedomError);
Console.WriteLine(
"Total degrees of freedom: {0}",
anovaTable.DegreesOfFreedomTotal);
Console.WriteLine(
"Factor A mean square: {0}",
anovaTable.MeanSquareFactorA);
Console.WriteLine(
"Factor B mean square: {0}",
anovaTable.MeanSquareFactorB);
Console.WriteLine(
"Interaction mean square: {0}",
anovaTable.MeanSquareInteraction);
Console.WriteLine(
"Error mean square: {0}",
anovaTable.MeanSquareError);
Console.WriteLine(
"Total mean square: {0}",
anovaTable.MeanSquareTotal);
Console.WriteLine(
"Factor A F-statistic: {0}",
anovaTable.FStatisticFactorA);
Console.WriteLine(
"Factor B F-statistic: {0}",
anovaTable.FStatisticFactorB);
Console.WriteLine(
"Interaction F-statistic: {0}",
anovaTable.FStatisticInteraction);
Console.WriteLine(
"Factor A p-value: {0}",
anovaTable.PValueFactorA);
Console.WriteLine(
"Factor B p-value: {0}",
anovaTable.PValueFactorB);
Console.WriteLine(
"Interaction p-value: {0}",
anovaTable.PValueInteraction);
}
}
}