VB Cross Tabulation Example

← All NMath Stats Code Examples


Imports System
Imports System.Text
Imports System.Collections
Imports Microsoft.VisualBasic

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats
Imports System.IO

Namespace CenterSpace.NMath.Stats.Examples.VisualBasic

  ' A .NET example in Visual Basic showing how to use the cross-tabulation functionality of DataFrame.

  ' As illustrated in the FactorExample, the DataFrame.GetFactor() method can be used
  ' in conjunction with Subset.GetGroupings() to access "cells" of data based on one
  ' or two grouping factors. This is such a common operation that class DataFrame also
  ' provides the Tabulate() methods as a convenience. This method accepts one or two
  ' grouping columns, a data column, and a delegate to apply to each data column subset.
  ' The results are returned in a new data frame.
  Module CrossTabulationExample

    Sub Main()

      ' Read in data from the file. The data show test scores for 18 children on a
      ' simple reading test. The child's gender ("male" or "female") and grade
      ' (4, 5, or 6) is also recorded.
      Dim DF As DataFrame = DataFrame.Load("CrossTabulationExample.dat", True, False, ControlChars.Tab, True)


      ' This code encapsulates the static StatsFunctions.Mean() function in a
      ' Func(Of IDFColumn, Double) delegate, then displays the average
      ' test score for each grade:
      Dim Mean As New Func(Of IDFColumn, Double)(AddressOf StatsFunctions.Mean)
      Console.WriteLine(DF.Tabulate("Grade", "Score", Mean))

      ' The code shows the average test score for every combination of gender and grade:
      Dim Means As DataFrame = DF.Tabulate("Grade", "Gender", "Score", Mean)

      ' The returned data frame can be easily accessed for individual results:
      Console.WriteLine("Average score for boys in grade 5 = " & Means(5, "male"))
      Console.WriteLine("Average score for grade 5 = " & Means(5, "Overall"))
      Console.WriteLine("Average score for boys = " & Means("Overall", "male"))
      Console.WriteLine("Grand average = " & Means("Overall", "Overall"))

      ' Most of the static descriptive statistics functions on class StatsFunctions
      ' accept an IDFColumn and return a double. A few return integers. For example,
      ' this code encapsulates StatsFunctions.Count(), which returns the number of items
      ' in a column, in a Func(Of IDFColumn, Int), then displays the number
      ' of subjects in each cell:
      Dim Count As New Func(Of IDFColumn, Integer)(AddressOf StatsFunctions.Count)
      Console.WriteLine(DF.Tabulate("Grade", "Gender", "Score", Count))

      ' The delegate the returns a generic object can be especially useful if you want to 
      ' tabulate a variety of summary statistics all at once: 
      Dim GetSummaryDelegate As New Func(Of IDFColumn, Object)(AddressOf CrossTabulationExample.GetSummary)
      Dim SummaryStats As DataFrame = DF.Tabulate("Grade", "Gender", "Score", GetSummaryDelegate)

      Console.WriteLine("Summary Statistics for Boys in Grade 6")
      Console.WriteLine(SummaryStats(6, "male"))

      Console.WriteLine("Press Enter Key")

    End Sub

    Function GetSummary(ByVal Data As IDFColumn) As Object
      Dim MySummary As Summary = New Summary
      MySummary.N = StatsFunctions.Count(Data)
      MySummary.Mean = StatsFunctions.Mean(Data)
      MySummary.StDev = StatsFunctions.StandardDeviation(Data)
      MySummary.Min = StatsFunctions.MinValue(Data)
      MySummary.Max = StatsFunctions.MaxValue(Data)
      Return MySummary
    End Function

  End Module

  Public Class Summary

    Public N As Integer
    Public Mean As Double
    Public StDev As Double
    Public Min As Double
    Public Max As Double

    Overrides Function ToString() As String
      Dim NL As String = System.Environment.NewLine
      Dim Buff As StringBuilder = New StringBuilder()
      Buff.Append("Size = " & N & NL)
      Buff.Append("Mean = " & Mean & NL)
      Buff.Append("Standard Deviation = " & StDev & NL)
      Buff.Append("Minimum = " & Min & NL)
      Buff.Append("Maximum = " & Max & NL)
      Return Buff.ToString()
    End Function

  End Class

End Namespace

← All NMath Stats Code Examples