C# Two Way Ranova Two Example

← All NMath Stats Code Examples

 

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

namespace CenterSpace.NMath.Stats.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use class TwoWayRanovaTwo to perform a traditional
  /// two-way ANOVA with repeated measures on both factors.
  /// </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 TwoWayRanovaTwoExample.
  /// </remarks>
  public class TwoWayRanovaTwoExample : 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 TwoWayRanovaTwoExmaple.
    /// </summary>
    public TwoWayRanovaTwoExample()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      // Read in data from a file. The file contains data for the following study*:
      //
      // A researcher was interested in whether frequency of exposure to a 
      // picture of an ugly or attractive person would influence one's liking
      // for the photograph. In order to find out the researcher, at the start 
      // of each class he taught, pinned a large photograph of an ugly and 
      // attractive person in front of the class. At the end of the class period the
      // students in the class were asked to rate on a 7 point liking scale the extent
      // to which they found the persons depicted in the photos to be likeable on 
      // a scale similar to the one below. 
      //
      //                  unlikeable -3 -2 -1 0 1 2 3 likeable
      //
      // The psychologist posted the pictures at the start of each class period 
      // and had the students rate their liking for the two pictures at the end
      // of the lst class, again after 5 classes and finally again after 10 classes.
      // Thus each student gave 3 ratings for each of the two photographs (after
      // 1 exposure, 5 exposures and 10 exposures).
      DataFrame anovaData = DataFrame.Load( "TwoWayRanovaTwoExample.dat", true, false, " ", true );

      // Class TwoWayRanovaTwo performs a balanced two-way analysis of variance with
      // repeated measures on both factors. Multiple linear regression is used to 
      // compute the RANOVA values. The constructor takes a DataFrame containing
      // the data, the column indices of the two factors, and the column index of
      // the data:
      var anova = new TwoWayRanovaTwo( anovaData, 1, 2, 0 );

      // This example simply displays a traditional ANOVA table
      // in a System.Windows.Forms.DataGrid object. The next few lines of
      // code format the table contents.

      // First retrieve the table to display. Class TwoWayRanovaTwoTable is
      // a subclass of DataFrame.
      TwoWayRanovaTwoTable anovaTable = anova.RanovaTable;

      // 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 are 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 RANOVA table into a string. This has the effect of 
      // formatting all the numbers so that they have three digits after the decimal
      // point and all Double.NaN values are 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 );

      // Now display the RANOVA table in the DataGrid.
      this.dataGrid1.DataSource = displayFrame.ToDataTable();

      // Results of the analysis indicated a main effect for the photo
      // attractiveness, F = 147.00, p = 0.001 <= 0.05, with the attractive
      // photo liked more than the ugly photo. There was no main effect for
      // the frequency of exposure to the photo, F = .60, p = 0.579 > 0.05.
      // There was a significant attractiveness of photo by frequency of 
      // exposure interaction, F = 18.00, p = 0.003 <= 0.05.
    }

    /// <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, 88 );
      this.dataGrid1.Name = "dataGrid1";
      this.dataGrid1.PreferredColumnWidth = 110;
      this.dataGrid1.ReadOnly = true;
      this.dataGrid1.RowHeadersVisible = false;
      this.dataGrid1.Size = new System.Drawing.Size( 664, 152 );
      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( 145, 24 );
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size( 424, 48 );
      this.label1.TabIndex = 1;
      this.label1.Text = "Two Way Anova Example\r\nWith Repeated Measures on Both Factors";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      // 
      // TwoWayRanovaTwoExample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 );
      this.ClientSize = new System.Drawing.Size( 714, 264 );
      this.Controls.AddRange( new System.Windows.Forms.Control[] {
                                                                  this.label1,
                                                                  this.dataGrid1} );
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
      this.Name = "TwoWayRanovaTwoExample";
      this.Text = "TwoWayRanovaTwoExample";
      ( (System.ComponentModel.ISupportInitialize) ( this.dataGrid1 ) ).EndInit();
      this.ResumeLayout( false );

    }
    #endregion

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

  }  // class

} // namespace

//
// *Taken from an example in "SPSS Windows INSTRUCTIONS FOR PSYCH. 280 & PSYCH. 290"
// Writen by Dr. Richard Izzett, Psychology Department, SUNY - Oswego, NY
//
← All NMath Stats Code Examples
Top