Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Two Way Repeated 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 repeated
    /// analysis of variance by using the TwoWayRepeatedAnova 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("Store", typeof(int));
            columns.Add("Color", typeof(string));
            columns.Add("Shape", typeof(string));
            columns.Add("Sales", typeof(double));

            // Populate data.
            RowList rows = sheet.Rows;
            rows.Add(1, "Blue", "Square", 6);
            rows.Add(2, "Blue", "Square", 14);
            rows.Add(3, "Blue", "Rectangle", 19);
            rows.Add(4, "Blue", "Rectangle", 17);
            rows.Add(5, "Red", "Square", 18);
            rows.Add(6, "Red", "Square", 11);
            rows.Add(7, "Red", "Rectangle", 20);
            rows.Add(8, "Red", "Rectangle", 23);
            rows.Add(9, "Green", "Square", 7);
            rows.Add(10, "Green", "Square", 11);
            rows.Add(11, "Green", "Rectangle", 18);
            rows.Add(12, "Green", "Rectangle", 10);
            Console.WriteLine(sheet);

            // Perform analysis of variance.
            // Factor A is stored in the "Color" column.
            // Factor B is stored in the "Shape" column.
            // Observations are stored in the "Sales" column.
            TwoWayRepeatedAnova anova = new TwoWayRepeatedAnova(
                sheet, "Color", "Shape", "Sales");

            // Access ANOVA table. This holds the results.
            TwoWayRepeatedAnovaTable 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(
                "Subjects sum of squares: {0}",
                anovaTable.SumOfSquareSubjects);

            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(
                "Subjects degrees of freedom: {0}",
                anovaTable.DegreesOfFreedomSubjects);

            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(
                "Subjects mean square: {0}",
                anovaTable.MeanSquareSubjects);

            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);
        }
    }
}