# VB Stats Functions Example

← All NMath Stats Code Examples

```Imports System
Imports System.Collections

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 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()

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()

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.ToString("F2") _
& " 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: " & _
Console.WriteLine("Percentage passing with 50% cutoff: " & _
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()
Next

' Young students
Dim Young As DataFrame = Data(New Subset(StatsFunctions.If(Data("Age"), New Func(Of Double, Boolean)(AddressOf IsYoung))), Slice.All)
Console.WriteLine("Young students...")
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.Write("Percentage beating top-ranked young student... ")

Console.WriteLine()
Console.WriteLine("Press Enter Key")
End Sub

Function IsYoung(ByVal Age As Double) As Boolean
IsYoung = Age < 20
End Function

End Module

End Namespace

```
← All NMath Stats Code Examples
Top