C# One Way Ranova Example

← All NMath Code Examples

 

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

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use class OneWayRanova to perform a simple one-way
  /// ANOVA with repeated measures.
  /// </summary>
  /// <remarks>This example was constructed with a Visual Studio wizard. The
  /// code generated by the wizard is contained in the region labelled
  /// "Windows Form Designer generated code". The example code is contained
  /// in the constructor for the class OneWayRanovaExample.
  /// </remarks>
  public class OneWayRanovaExample : 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 the example. All relevant example code is contained
    /// in this constructor.
    /// </summary>
    public OneWayRanovaExample()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      // Construct a DataFrame containing the data to be analyzed. Each
      // column of the DataFrame represents a treatment and each row 
      // represents a subject.
      var data = new DataFrame();
      data.RowKeyHeader = "Dog Number";
      data.AddColumn( new DFNumericColumn( "Normal" ) );
      data.AddColumn( new DFNumericColumn( "Stomach" ) );
      data.AddColumn( new DFNumericColumn( "Eating" ) );
      int dog = 1;
      data.AddRow( dog++, 104, 91, 22 );
      data.AddRow( dog++, 106, 94, 14 );
      data.AddRow( dog++, 111, 105, 14 );
      data.AddRow( dog++, 114, 106, 15 );
      data.AddRow( dog++, 117, 120, 18 );
      data.AddRow( dog++, 139, 111, 8 );

      // Now, use the DataFrame to construct a OneWayRanova instance.
      var ranova = new OneWayRanova( data );

      // This example will just display a traditional RANOVA 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
      // OneWayRanovaTable is a subclass of DataFrame.
      OneWayRanovaTable ranovaTable = ranova.RanovaTable;

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

      // Next convert the RANOVA 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 RANOVA 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).
      var displayFrame = new DataFrame( ranovaTable.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 me 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 doesnt
      // look good to have a p-value of 0 in our display, so lets display
      // something a bit more sensible like display "< 0.001".

      // First, use the FStatisticPValue property on the OneWayRanovaTable
      // class to determine if the p-value is too small.
      if ( ranovaTable.FStatisticPValue < 0.001 )
      {
        // Use the IndexOfKey() and IndexOfColumn() methods of the DataFrame
        // to determine the row and column indices of the p-value in our
        // display table (the display table, being a copy of the original
        // RANOVA table, has the same rows and columns as the original).
        int rowIndex = ranovaTable.IndexOfKey( OneWayRanovaTable.TreatmentRowKey );
        int colIndex = ranovaTable.IndexOfColumn( OneWayRanovaTable.PvalueColName );

        // Note how we used the static variables OneWayRanovaTable.TreatmentRowKey
        // and OneWayRanovaTable.PvalueColName to get the names of the row 
        // and column that contain the p-value.
        // Finally, lets change the string from 0 to something more sensible.
        displayFrame[rowIndex, colIndex] = "< 0.001";
      }

      // We are now ready to display the RANOVA table in the DataGrid. We use
      // the DataFrames ToDataTable() method to create an ADO DataTable which
      // we use to set the grids data source.
      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.AllowSorting = false;
      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( 32, 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, 96 );
      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( 216, 16 );
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size( 280, 23 );
      this.label1.TabIndex = 1;
      this.label1.Text = "One Way Ranova Example";
      // 
      // OneWayRanovaExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 );
      this.ClientSize = new System.Drawing.Size( 728, 190 );
      this.Controls.AddRange( new System.Windows.Forms.Control[] {
                                                                  this.label1,
                                                                  this.dataGrid1} );
      this.Name = "OneWayRanovaExample";
      this.Text = "OneWayRanovaExample";
      ( (System.ComponentModel.ISupportInitialize) ( this.dataGrid1 ) ).EndInit();
      this.ResumeLayout( false );

    }
    #endregion

    static void Main()
    {
      Application.Run( new OneWayRanovaExample() );
    }
  }
}

← All NMath Code Examples
Top