VB Hypothesis Test Example

← All NMath Code Examples

 

Imports System
Imports System.Collections

Imports CenterSpace.NMath.Core


Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic showing how to use the hypothesis test classes to test statistical
  hypotheses.

  Hypothesis tests use statistics to determine the probability that a
  given hypothesis is true. For example, could the differences between two
  sample means be explained away as sampling error? NMath Stats provides
  classes for many common hypothesis tests, such as the z-test, t-test,
  F-test, and Kolmogorov-Smirnov test, with calculation of p-values,
  critical values, and confidence intervals. All hypothesis test classes
  share substantially the same interface. Once you learn how to use one
  test, it’s easy to use any of the others.

  Module HypothesisTestExample

    Sub Main()

      Suppose we measure the thickness of plaque (mm) in the carotid
      artery of 10 randomly selected patients with mild artherosclerotic
      disease. Two measurements are taken: before treatment with
      Vitamin E (baseline), and after two years of taking Vitamin E daily.
      Dim DF As DataFrame = New DataFrame()
      Dim Col1 As DFNumericColumn = New DFNumericColumn("Baseline")
      Col1.NumericFormat = "F7"
      DF.AddColumn(Col1)
      Dim Col2 As DFNumericColumn = New DFNumericColumn("Vit E")
      Col2.NumericFormat = "F7"
      DF.AddColumn(Col2)
      DF.AddRow("Subj1", 0.66, 0.6)
      DF.AddRow("Subj2", 0.72, 0.65)
      DF.AddRow("Subj3", 0.85, 0.79)
      DF.AddRow("Subj4", 0.62, 0.63)
      DF.AddRow("Subj5", 0.59, 0.54)
      DF.AddRow("Subj6", 0.63, 0.55)
      DF.AddRow("Subj7", 0.64, 0.62)
      DF.AddRow("Subj8", 0.7, 0.67)
      DF.AddRow("Subj9", 0.73, 0.68)
      DF.AddRow("Subj10", 0.68, 0.64)

      Display the data
      Console.WriteLine()
      Console.WriteLine(DF)
      Console.WriteLine()

      Construct a TwoSamplePairedTTest from the two columns of data.
      Dim Test As TwoSamplePairedTTest = New TwoSamplePairedTTest(DF("Baseline"), DF("Vit E"))

      Display the results
      Console.WriteLine(Test)
      Console.WriteLine()

      Change test type and alpha level, and display new results.
      Test.Type = HypothesisType.Right
      Test.Alpha = 0.05
      Console.WriteLine(Test)
      Console.WriteLine()

      Modify the data.
      DF(5, 1) = 0.56
      DF.AddRow("Subject11", 0.65, 0.64)
      Console.WriteLine(DF)
      Console.WriteLine()

      Update the test, and display new results.
      Test.Update(DF("Baseline"), DF("Vit E"))
      Test.Type = HypothesisType.TwoSided
      Console.WriteLine(Test)
      Console.WriteLine()

      Properties provide programmatic access to individual elements in the test.
      Console.WriteLine("Properties")
      Console.WriteLine("number of pairs = " & Test.N)
      Console.WriteLine("mean difference between pairs = " & Test.Xbar)
      Console.WriteLine("standard deviation of differences between pairs = " & Test.S)
      Console.WriteLine("deg of freedom = " & Test.DegreesOfFreedom)
      Console.WriteLine("t-statistic = " & Test.Statistic)
      Console.WriteLine("test type = " & Test.Type)
      Console.WriteLine("alpha level = " & Test.Alpha)
      Console.WriteLine("p-value = " & Test.P)
      Console.WriteLine("critical values: " & Test.LeftCriticalValue & " " & Test.RightCriticalValue)
      Console.WriteLine("reject the null hypothesis? " & Test.Reject)
      Console.WriteLine("confidence interval: " & Test.LowerConfidenceLimit & " " & Test.UpperConfidenceLimit)

      Console.WriteLine()
      Console.WriteLine("Press Enter Key")
      Console.Read()

    End Sub

  End Module

End Namespace

← All NMath Code Examples
Top