using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace CenterSpace.NMath.Examples.CSharp { /// <summary> /// A .NET example in C# showing how to use class TwoWayRanova to perform a two-way ANOVA /// with repeated measures on one factor. /// </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 TwoWayRanovaExample. /// </remarks> public class TwoWayRanovaExample : 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 TwoWayRanovaExmaple. /// </summary> public TwoWayRanovaExample() { // // Required for Windows Form Designer support // InitializeComponent(); // Read in data from a file. The file contains data for a test for // contamination of surgical scrub suits. One group of workers changed // their scrub suit at lunch, while the other group wore a surgical // gown to lunch and did not change their scrub suits. Bacterial // contamination level of the scrub suits was measured at 3 times // during the workers shifts. The nonrepeated factor is the scrub suit // group (change or not change), the repeated factor is time. DataFrame anovaData = DataFrame.Load( "TwoWayRanovaExample.dat", true, false, " ", true ); // Class TwoWayRanova performs a balanced two-way analysis of variance with // repeated measures on one factor. Multiple linear regression is used to // compute the RANOVA values. The constructor takes a DataFrame // containing the data, and the column indices of the repeated factor, // the nonrepeated factor, and the contamination level. var anova = new TwoWayRanova( anovaData, 1, 2, 0 ); // This example simply displays a traditional RANOVA 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 TwoWayRanovaTable is a // subclass of DataFrame. TwoWayRanovaTable anovaTable = anova.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 are displayed when the table is // converted to a string. ( (DFNumericColumn) anovaTable[1] ).NumericFormat = "F0"; for ( int i = 2; 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.) var displayFrame = new DataFrame( anovaTable.ToString(), true, true, "\t", false ); // Note that in the previous line we passed a value of false for the // parse parameter to the DataFrame constructor (the last parameter). // This gives us a DataFrame object in which all the column types // are strings. This lets us format the data easily. // If the p-value for the F statistic is too small, it is 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". int rowIndex, colIndex; if ( anovaTable.FstatisticPvalue( "ScrubSuit" ) < .001 ) { colIndex = displayFrame.IndexOfColumn( TwoWayAnovaTable.PvalueColName ); rowIndex = displayFrame.IndexOfKey( "ScrubSuit" ); displayFrame[rowIndex, colIndex] = "< 0.001"; } if ( anovaTable.FstatisticPvalue( "Time" ) < .001 ) { colIndex = displayFrame.IndexOfColumn( TwoWayAnovaTable.PvalueColName ); rowIndex = displayFrame.IndexOfKey( "Time" ); 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(); // The interaction effect indicates that the change in bacterial counts // over time is different in the two groups (F = 24.163, p < 0.001). The // main effect of time is also highly significant (F = 25.9, p 0.001). // The difference between scrub suit groups is insignificant (F = 3.86, // p = 0.057). } /// <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, 112 ); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.PreferredColumnWidth = 110; this.dataGrid1.ReadOnly = true; this.dataGrid1.RowHeadersVisible = false; this.dataGrid1.Size = new System.Drawing.Size( 664, 128 ); 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, 64 ); this.label1.TabIndex = 1; this.label1.Text = "Two Way Anova With Repeated Measures\r\nOn One Factor Example"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // TwoWayRanovaExample // this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 ); this.ClientSize = new System.Drawing.Size( 714, 272 ); this.Controls.AddRange( new System.Windows.Forms.Control[] { this.label1, this.dataGrid1} ); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "TwoWayRanovaExample"; this.Text = "TwoWayRanovaExample"; ( (System.ComponentModel.ISupportInitialize) ( this.dataGrid1 ) ).EndInit(); this.ResumeLayout( false ); } #endregion static void Main() { Application.Run( new TwoWayRanovaExample() ); } } // class } // namespace← All NMath Code Examples