VB Stats Functions Example

← All NMath Code Examples

 

Imports System
Imports System.Collections

Imports CenterSpace.NMath.Core

Imports System.IO

Namespace CenterSpace.NMath.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()

      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.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: " & _
        (1.0 - StatsFunctions.PercentileRank(Data("Grade"), 50)).ToString("P2"))
      Console.WriteLine("Percentage passing with 50% cutoff: " & _
        (1.0 - StatsFunctions.PercentileRank(Data("Grade"), 60)).ToString("P2"))
      Console.WriteLine()

      Percentiles
      Console.WriteLine("Interquartile range is " & StatsFunctions.InterquartileRange(Data("Grade")))
      Console.WriteLine("Whats 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: " & StatsFunctions.Mean(GenderSpecific("Grade")).ToString("F2"))
        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 Func(Of Double, Boolean)(AddressOf IsYoung))), Slice.All)
      Console.WriteLine("Young students...")
      Console.WriteLine("mean grade: " & StatsFunctions.Mean(Young("Grade")).ToString("F2"))
      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.Write("Percentage beating top-ranked young student... ")
      Console.WriteLine((1.0 - StatsFunctions.PercentileRank(Data("Grade"), Top)).ToString("P2"))

      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


← All NMath Code Examples
Top