VB One Way Ranova Example

← All NMath Stats Code Examples


Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats

Namespace CenterSpace.NMath.Stats.Examples.VisualBasic

  ' A .NET example in Visual Basic showing one way RANOVA.
  Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

      'This call is required by the Windows Form Designer.

      'Add any initialization after the InitializeComponent() call

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

      ' Now, use the DataFrame to construct a OneWayRanova instance.
      Dim Ranova As 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.
      Dim RanovaTable As OneWayRanovaTable = Ranova.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 will be displayed when the table is
      ' converted to a string.
      Dim I As Integer
      For I = 1 To RanovaTable.Cols - 1
        CType(RanovaTable(I), DFNumericColumn).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).
      Dim DisplayFrame As New DataFrame(RanovaTable.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 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 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".

      ' First, use the FStatisticPValue property on the OneWayRanovaTable
      ' class to determine if the p-value is too small.
      If RanovaTable.FStatisticPValue < 0.001 Then

        ' 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).
        Dim RowIndex As Integer = RanovaTable.IndexOfKey(OneWayRanovaTable.TreatmentRowKey)
        Dim ColIndex As Integer = 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, let's change the string from 0 to something more sensible.
        DisplayFrame(RowIndex, ColIndex) = "< 0.001"
      End If

      ' 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.
      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
        End If
      End If
    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 Label1 As System.Windows.Forms.Label
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.Label1 = New System.Windows.Forms.Label()
      Me.DataGrid1 = New System.Windows.Forms.DataGrid()
      CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
      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(192, 24)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(264, 23)
      Me.Label1.TabIndex = 0
      Me.Label1.Text = "One Way Ranova Example"
      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(24, 72)
      Me.DataGrid1.Name = "DataGrid1"
      Me.DataGrid1.PreferredColumnWidth = 110
      Me.DataGrid1.ReadOnly = True
      Me.DataGrid1.RowHeadersVisible = False
      Me.DataGrid1.Size = New System.Drawing.Size(664, 96)
      Me.DataGrid1.TabIndex = 1
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(714, 192)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1, Me.Label1})
      Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
      Me.Name = "Form1"
      Me.Text = "One Way Ranova Example"
      CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

  End Class

End Namespace

← All NMath Stats Code Examples