VB Advanced One Way Anova Example

← All NMath Core Code Examples


Imports System
Imports System.Collections

Imports CenterSpace.NMath.Core
Imports System.IO

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

  ' A .NET example in Visual Basic showing some of the advanced features of class OneWayAnova.
  Module AdvancedOneWayAnovaExample

    Sub Main()

      ' Construct a DataFrame from the data in the file. The file contains 
      ' data for three groups of people, healthy people, melancholic depressed
      ' people, and nonmelancholic depressed people. For each of these 
      ' groups the cortisol levels in the blood are measured*
      ' The data consists of two columns. The first column contains the name
      ' of the group that an individual belongs to and the second column
      ' contains the level of cortisol for the individual.
      Dim AnovaData As DataFrame = DataFrame.Load("AdvancedOneWayAnovaExample.dat", False, False, " ", True)

      ' The names of the three groups in the study.
      Dim Healthy As String = "Healthy"
      Dim NonMelancholic As String = "Nonmelancholic"
      Dim Melancholic As String = "Melancholic"

      ' Construct a OneWayAnova instance from the DataFrame. The first 
      ' integer argument specifies which column contains the group names
      ' and the second integer argument specifies which column contains
      ' the numeric data (as zero based indices).
      Dim Anova As OneWayAnova = New OneWayAnova(AnovaData, 0, 1)

      ' Print out the means for each of the three groups.
      Dim HealthGroupMean As Double = Anova.GetGroupMean(Healthy)
      Dim NonMelancholicGroupMean As Double = Anova.GetGroupMean(NonMelancholic)
      Dim MelancholicGroupMean As Double = Anova.GetGroupMean(Melancholic)

      Console.WriteLine("Healthy group mean        = " & HealthGroupMean.ToString("G7"))
      Console.WriteLine("NonMelancholic group mean = " & NonMelancholicGroupMean.ToString("G7"))
      Console.WriteLine("Melancholic group mean    = " & MelancholicGroupMean.ToString("G7"))

      ' Print out the sum of squares, degrees of freedom and mean square
      ' for each of the source between groups, within groups, and total.
      Console.WriteLine("Between Groups:")
      Console.WriteLine("  Sum of Squares     = " & Anova.AnovaTable.SumOfSquaresBetween.ToString("G7"))
      Console.WriteLine("  Degrees of Freedom = " & Anova.AnovaTable.DegreesOfFreedomBetween.ToString("G7"))
      Console.WriteLine("  Mean Square        = " & Anova.AnovaTable.MeanSquareBetween.ToString("G7"))

      Console.WriteLine("Within Groups:")
      Console.WriteLine("  Sum of Squares     = " & Anova.AnovaTable.SumOfSquaresWithin.ToString("G7"))
      Console.WriteLine("  Degrees of Freedom = " & Anova.AnovaTable.DegreesOfFreedomWithin.ToString("G7"))
      Console.WriteLine("  Mean Square        = " & Anova.AnovaTable.MeanSquareWithin.ToString("G7"))

      Console.WriteLine("  Sum of Squares     = " & Anova.AnovaTable.SumOfSquaresTotal.ToString("G7"))
      Console.WriteLine("  Degrees of Freedom = " & Anova.AnovaTable.DegreesOfFreedomTotal)
      Console.WriteLine("  Mean Square        = " & Anova.AnovaTable.MeanSquareTotal.ToString("G7"))

      ' Fetch the F statistic and the critical value for the F statistic and
      ' decide whether or not to reject the null hypothesis that all group
      ' means are the same.
      Dim F As Double = Anova.AnovaTable.FStatistic
      Dim Alpha As Double = 0.01
      Dim CriticalValue As Double = Anova.FStatisticCriticalValue(Alpha)

      Console.Write("F = ")
      Console.WriteLine("Critical value for alpha < " & Alpha & " = " & CriticalValue.ToString("G5"))

      If (F > CriticalValue) Then
        Console.WriteLine("Reject the null hypothesis that the means are the ")
        Console.WriteLine("same in all three groups at the " & Alpha & " significance level.")
        Console.WriteLine("Accept the null hypothesis that the means are the ")
        Console.WriteLine("same in all three groups at the " & Alpha & " significance level.")
      End If

      Console.WriteLine("Press Enter Key")

    End Sub

  End Module

End Namespace

' * Source: L. H. Price, D. S. Charney, A. L. Rubin, and F. R. 
' Henniger, "alpha2 Adregenric Receptor Function in Depression:
' The Cortisol Response to Yohimbine," Arch. Gen. Psychiatry 
' 43:849-858, 1986.

← All NMath Stats Code Examples