[TOC]
Imports System
Imports System.Text
Imports System.Collections
Imports Microsoft.VisualBasic
Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Stats
Namespace CenterSpace.NMath.Stats.Examples.VisualBasic
' A .NET example in VB.NET 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)
Console.WriteLine()
Console.Write(DF)
Console.WriteLine()
Console.WriteLine()
' This code encapsulates the static StatsFunctions.Mean() function in a
' StatsFunctions.DoubleIDFColumnFunction delegate, then displays the average
' test score for each grade:
Dim Mean As StatsFunctions.DoubleIDFColumnFunction = New StatsFunctions.DoubleIDFColumnFunction(AddressOf StatsFunctions.Mean)
Console.WriteLine(DF.Tabulate("Grade", "Score", Mean))
Console.WriteLine()
' The code shows the average test score for every combination of gender and grade:
Dim Means As DataFrame = DF.Tabulate("Grade", "Gender", "Score", Mean)
Console.WriteLine(Means)
Console.WriteLine()
' 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"))
Console.WriteLine()
' 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 StatsFunctions.IntIDFColumnFunction, then displays the number
' of subjects in each cell:
Dim Count As StatsFunctions.IntIDFColumnFunction = New StatsFunctions.IntIDFColumnFunction(AddressOf StatsFunctions.Count)
Console.WriteLine(DF.Tabulate("Grade", "Gender", "Score", Count))
Console.WriteLine()
' 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 StatsFunctions.GenericIDFColumnFunction = New StatsFunctions.GenericIDFColumnFunction(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()
Console.WriteLine("Press Enter Key")
Console.Read()
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
[TOC]