VB Two Way Anova 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 analysis of variance (anova).
  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

      ' 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.
      Dim AnovaData As DataFrame = DataFrame.Load("TwoWayAnovaExample.dat", True, False, ControlChars.Tab, True)

      Dim Anova As TwoWayAnova = 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.
      Dim AnovaTable As TwoWayAnovaTable = 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.

      Dim I As Integer
      For I = 1 To AnovaTable.Cols - 1
        CType(AnovaTable(I), DFNumericColumn).NumericFormat = "F3"
      Next

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

      If (AnovaTable.FstatisticPvalue("LIQ") < 0.001) Then
        ColIndex = DisplayFrame.IndexOfColumn(TwoWayAnovaTable.PvalueColName)
        RowIndex = DisplayFrame.IndexOfKey("LIQ")
        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()

    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, 64)
      Me.DataGrid1.Name = "DataGrid1"
      Me.DataGrid1.PreferredColumnWidth = 110
      Me.DataGrid1.ReadOnly = True
      Me.DataGrid1.RowHeadersVisible = False
      Me.DataGrid1.Size = New System.Drawing.Size(664, 112)
      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(184, 24)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(272, 23)
      Me.Label1.TabIndex = 1
      Me.Label1.Text = "Two Way Anova Example"
      '
      'Form1
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(698, 208)
      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