C# Two Way Anova Example

[TOC]

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CenterSpace.NMath.Stats.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use class TwoWayAnova to perform a traditional
  /// two-way ANOVA.
  /// </summary>
  /// <remarks>This example was constructed with a Visual Studio wizard. The
  /// code generated by the wizard is contained in the region labled
  /// "Windows Form Designer generated code". The example code is contained
  /// in the constructor for the class TwoWayAnovaExample.
  /// </remarks>
  public class TwoWayAnovaExample : System.Windows.Forms.Form
  {
    private System.Windows.Forms.DataGrid dataGrid1;
    private System.Windows.Forms.Label label1;

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    /// <summary>
    /// Constructor for class TwoWayAnovaExmaple.
    /// </summary>
    public TwoWayAnovaExample()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      // Name of the file containing the data on which we will operate.
      string filename = "..\\..\\TwoWayAnovaExample.dat";

      // Construct a DataFrame from the data in the file. The file contains 
      // data for a taste test. The two factors are screen type and liquid
      // level. Screen type has two levels, 0 (course) and 1 (fine). Liquid
      // level has two levels also: 0 (low) and 1 (high). The first column
      // of the data contains the taste score, the second column represents
      // the screen type and the third column represents the liquid level.
      DataFrame anovaData = DataFrame.Load(filename, true, false, "\t", true);

      TwoWayAnova anova = new TwoWayAnova(anovaData, 1, 2, 0);

      // This example will just display a traditional ANOVA table
      // in a System.Windows.Forms.DataGrid object. The next few lines of
      // code are formatting the table contents so it will look nice.

      // First retrieve the table to display. Note that the class
      // TwoWayAnovaTable is a subclass of DataFrame.
      TwoWayAnovaTable anovaTable = anova.AnovaTable;

      // Next, let's set the precision to three decimal places. Note that this
      // does not actually change the numbers in the ANOVA table. It just
      // determines how many decimal places will be displayed when the table is
      // converted to a string.
      for (int i = 1; i < anovaTable.Cols; ++i)
      {
        ((DFNumericColumn)anovaTable[i]).NumericFormat = "F3";
      }

      // Next convert the ANOVA table into a string. This will have the effect of 
      // formatting all the numbers so that they have three digits after the decimal
      // point and all Double.NaN values will be represented as a "." (some rows
      // in the ANOVA table that do not have F statistic values or F statistic
      // p-values associated with them. The cells that correspond to these rows
      // and the F and P columns contain Double.NaN values).
      DataFrame displayFrame = new DataFrame(anovaTable.ToString(), true, true, "\t", false);

      // Notice that in the previous line we passed a value of false for the 
      // parse parameter to the DataFrame constructor (the last parameter). 
      // This will give us a DataFrame object in which all the column types
      // are strings. This lets us format the data easily before I turn it
      // over to the DataGrid.

      // If the p-value for the F statistic is too small, it will be rounded
      // to zero when we set the precision to three decimal places. It doesn't
      // look good to have a p-value of 0 in our display, so let's display
      // something a bit more sensible like display "< 0.001".
      int rowIndex, colIndex;
      if (anovaTable.FstatisticPvalue("SCR") < .001)
      {
        colIndex = displayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName);
        rowIndex = displayFrame.IndexOfKey("SCR");
        displayFrame[rowIndex, colIndex] = "< 0.001";
      }

      if (anovaTable.FstatisticPvalue("LIQ") < .001)
      {
        colIndex = displayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName);
        rowIndex = displayFrame.IndexOfKey("LIQ");
        displayFrame[rowIndex, colIndex] = "< 0.001";
      }

      if (anovaTable.InteractionFstatisticPvalue < 0.001)
      {
        colIndex = displayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName);
        rowIndex = displayFrame.IndexOfKey(TwoWayAnovaTable.InteractionRowKey);
        displayFrame[rowIndex, colIndex] = "< 0.001";
      }

      // Finally, display the ANOVA table in the DataGrid.
      this.dataGrid1.DataSource = displayFrame.ToDataTable();
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.dataGrid1 = new System.Windows.Forms.DataGrid();
      this.label1 = new System.Windows.Forms.Label();
      ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
      this.SuspendLayout();
      // 
      // dataGrid1
      // 
      this.dataGrid1.BackgroundColor = System.Drawing.Color.White;
      this.dataGrid1.CaptionVisible = false;
      this.dataGrid1.DataMember = "";
      this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.dataGrid1.Location = new System.Drawing.Point(24, 64);
      this.dataGrid1.Name = "dataGrid1";
      this.dataGrid1.PreferredColumnWidth = 110;
      this.dataGrid1.ReadOnly = true;
      this.dataGrid1.RowHeadersVisible = false;
      this.dataGrid1.Size = new System.Drawing.Size(664, 112);
      this.dataGrid1.TabIndex = 0;
      // 
      // label1
      // 
      this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label1.Location = new System.Drawing.Point(192, 24);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(256, 32);
      this.label1.TabIndex = 1;
      this.label1.Text = "Two Way Anova Example";
      // 
      // TwoWayAnovaExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(714, 200);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.label1,
                                                                  this.dataGrid1});
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
      this.Name = "TwoWayAnovaExample";
      this.Text = "TwoWayAnovaExample";
      ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.Run(new TwoWayAnovaExample());
    }

  }  // class

} // namespace



[TOC]