VB Two Way Ranova Example

← All NMath Core Code Examples

 

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

  ' A .NET example in Visual Basic showing two-way ranova.
  Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

      ' 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 worker's shifts. The nonrepeated factor is the scrub suit
      ' group (change or not change), the repeated factor is time.
      Dim AnovaData As DataFrame = 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.
      Dim Anova As TwoWayRanova = 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.
      Dim AnovaTable As TwoWayRanovaTable = Anova.RanovaTable

      ' Next, let's 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.
      Dim I As Integer
      For I = 1 To AnovaTable.Cols - 1
        CType(AnovaTable(I), DFNumericColumn).NumericFormat = "F3"
      Next

      ' 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.)
      Dim DisplayFrame As New DataFrame(AnovaTable.ToString(), True, True, ControlChars.Tab, 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 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".
      Dim RowIndex, ColIndex As Integer
      If (AnovaTable.FstatisticPvalue("ScrubSuit") < 0.001) Then
        ColIndex = DisplayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName)
        RowIndex = DisplayFrame.IndexOfKey("ScrubSuit")
        DisplayFrame(RowIndex, ColIndex) = "< 0.001"
      End If

      If (AnovaTable.FstatisticPvalue("Time") < 0.001) Then
        ColIndex = DisplayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName)
        RowIndex = DisplayFrame.IndexOfKey("Time")
        DisplayFrame(RowIndex, ColIndex) = "< 0.001"
      End If

      If (AnovaTable.InteractionFstatisticPvalue < 0.001) Then
        ColIndex = DisplayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName)
        RowIndex = DisplayFrame.IndexOfKey(TwoWayAnovaTable.InteractionRowKey)
        DisplayFrame(RowIndex, ColIndex) = "< 0.001"
      End If

      ' Finally, display the ANOVA table in the DataGrid.
      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).

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
        If Not (components Is Nothing) Then
          components.Dispose()
        End If
      End If
      MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.DataGrid1 = New System.Windows.Forms.DataGrid()
      Me.Label1 = New System.Windows.Forms.Label()
      CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
      Me.SuspendLayout()
      '
      'DataGrid1
      '
      Me.DataGrid1.AllowSorting = False
      Me.DataGrid1.BackgroundColor = System.Drawing.Color.White
      Me.DataGrid1.CaptionVisible = False
      Me.DataGrid1.DataMember = ""
      Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
      Me.DataGrid1.Location = New System.Drawing.Point(16, 96)
      Me.DataGrid1.Name = "DataGrid1"
      Me.DataGrid1.PreferredColumnWidth = 110
      Me.DataGrid1.ReadOnly = True
      Me.DataGrid1.RowHeadersVisible = False
      Me.DataGrid1.Size = New System.Drawing.Size(664, 136)
      Me.DataGrid1.TabIndex = 0
      '
      'Label1
      '
      Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
      Me.Label1.Location = New System.Drawing.Point(144, 24)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(376, 48)
      Me.Label1.TabIndex = 1
      Me.Label1.Text = "Two Way Anova With Repeated Measures On One Factor Example"
      Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
      '
      'Form1
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(698, 264)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.DataGrid1})
      Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
      Me.Name = "Form1"
      Me.Text = "Two Way Anova Example"
      CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
      Me.ResumeLayout(False)

    End Sub

#End Region

  End Class

End Namespace

← All NMath Stats Code Examples
Top