[TOC]
Imports System
Imports System.Collections
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 descriptive statistics functions of the
' StatsFunctions class.
Module StatsFunctionsExample
Sub Main()
' Read in data from a comma-delimited file. Data has age, gender, grade columns.
' Specify the data file, whether it includes headers, whether it has row keys,
' the delimiter, and whether to try to parse data into non-generic types.
Dim Data As DataFrame = DataFrame.Load("..\\..\\StatsFunctionsExample.dat", True, False, ",", True)
' Print out all data.
Console.WriteLine()
Console.WriteLine(Data)
Console.WriteLine()
' Print out top grade
Console.WriteLine("highest grade.. " & StatsFunctions.MaxValue(Data("Grade")))
Console.WriteLine()
' How many male and female students are there?
Dim Counts As IDictionary = StatsFunctions.Counts(Data("Gender"))
Console.WriteLine("There are " & Counts("Male") & " male students and " & Counts("Female") & " female students.")
Console.WriteLine()
' Mean, median, mode grades
Console.WriteLine("mean grade: " & StatsFunctions.Mean(Data("Grade")))
Console.WriteLine("median grade: " & StatsFunctions.Median(Data("Grade")))
Console.WriteLine("most common grade: " & StatsFunctions.Mode(Data("Grade")))
Console.WriteLine()
' Is there a correlation between age and grade?
Dim Correlation As Double = StatsFunctions.Correlation(Data("Age"), Data("Grade"))
If Correlation > 0.0 Then
Console.WriteLine("There is a positive correlation of " & Correlation & " between age and grade.")
Console.WriteLine()
End If
' What percentage of students pass with a 50% passing rate? 60%?
Console.WriteLine("Percentage passing with 50% cutoff: " & Math.Round(((1.0 - StatsFunctions.PercentileRank(Data("Grade"), 50)) * 100.0), 2) & "%")
Console.WriteLine("Percentage passing with 60% cutoff: " & Math.Round(((1.0 - StatsFunctions.PercentileRank(Data("Grade"), 60)) * 100.0), 2) & "%")
Console.WriteLine()
' Percentiles
Console.WriteLine("Interquartile range is " & StatsFunctions.InterquartileRange(Data("Grade")))
Console.WriteLine("What's the 80th percentile? " & StatsFunctions.Percentile(Data("Grade"), 0.8))
Console.WriteLine()
' Split by gender and do descriptive statistics
Dim Gender As Factor = Data.GetFactor("Gender")
Dim GenderGroups() As Subset = Subset.GetGroupings(Gender)
Dim I As Integer
For I = 0 To GenderGroups.Length - 1
Dim GenderSpecific As DataFrame = Data.GetRows(GenderGroups(I))
Console.WriteLine(Gender.Levels(I) & " students...")
Console.WriteLine("mean grade: " & Math.Round(StatsFunctions.Mean(GenderSpecific("Grade")), 2))
Console.WriteLine("median grade: " & StatsFunctions.Median(GenderSpecific("Grade")))
Console.WriteLine("most common grade: " & StatsFunctions.Mode(GenderSpecific("Grade")))
Console.WriteLine()
Next
' Young students
Dim Young As DataFrame = Data(New Subset(StatsFunctions.If(Data("Age"), New StatsFunctions.LogicalDoubleFunction(AddressOf IsYoung))), Slice.All)
Console.WriteLine("Young students...")
Console.WriteLine("mean grade: " & Math.Round(StatsFunctions.Mean(Young("Grade")), 2))
Console.WriteLine("median grade: " & StatsFunctions.Median(Young("Grade")))
Console.WriteLine("most common grade: " & StatsFunctions.Mode(Young("Grade")))
Console.WriteLine()
' Top grade by a young student?
Dim Top As Double = StatsFunctions.MaxValue(Young("Grade"))
Console.WriteLine("Top grade by a young student was... " & Top)
' What percentage of the whole group beat the top-ranked young student?
Console.WriteLine("Percentage beating top-ranked young student... " & Math.Round(((1.0 - StatsFunctions.PercentileRank(Data("Grade"), Top)) * 100.0), 2) & "%")
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
Function IsYoung(ByVal Age As Double) As Boolean
IsYoung = Age < 20
End Function
End Module
End Namespace
[TOC]