Sending Custom Statistics to Statistica in VB.NET
The following example will open a new instance of Statistica, open the data set Adstudy.sta from your Examples directory, and then run custom VB.NET Statistical functions.
Public Class StatsMgr
'a custom exception for any member function to throw if 'it can not calculate results for any reason
Public Class BadResults Inherits System.Exception 'note the ability to inherit base classes
Dim ErrMessage As String
End Class
'retrieves the sum of the specified variable of 'the specified spreadsheet
Function Sum(ByVal Spr As Statistica.Spreadsheet, _ ByVal VarNo As Long) As Double
Dim i As Long Dim TotalVal As Double = 0
For i = 1 To Spr.NumberOfCases
'ignore missing data If Spr.SelectionCondition.Evaluate(i) And _ Not (Spr.MissingData(i, VarNo)) Then
TotalVal = TotalVal + Spr.Value(i, VarNo)
End If
Next i
Sum = TotalVal
End Function
'retrieves the number of valid observations Function ValidObs(ByVal Spr As Statistica.Spreadsheet, _ ByVal VarNo As Long) As Double
Dim TotalValid As Long = 0 Dim i As Long
For i = 1 To Spr.NumberOfCases
'ignore missing data and make sure case selections allow its inclusion If Not (Spr.MissingData(i, VarNo)) And Spr.SelectionCondition.Evaluate(i) Then
TotalValid = TotalValid + 1
End If
Next i
ValidObs = TotalValid
End Function
'retrieves the variance of a variable Function Variance(ByVal Spr As Statistica.Spreadsheet, _ ByVal VarNo As Long) As Double
Dim Total As Double Dim N As Long Dim Mean As Double Dim SumOfSquares As Double
SumOfSquares = 0 Total = Sum(Spr, VarNo) N = ValidObs(Spr, VarNo)
'if there aren't any valid observations then thow an error If Total = 0 Or N < 2 Then
Throw New StatsMgr.BadResults()
End If
Mean = Total / N
Dim i As Long
For i = 1 To Spr.NumberOfCases
If Spr.SelectionCondition.Evaluate(i) And _ Not (Spr.MissingData(i, VarNo)) Then
'calculate squares of differences 'iterate through each case, subtract the means from it, and then 'square it. Add all of these up and you will have the sum of squares. SumOfSquares = SumOfSquares + ((Spr.Value(i, VarNo) - Mean) ^ 2)
End If
Next i
Variance = SumOfSquares / (N - 1)
End Function
End Class
Module VBNETCustomStatistics
Sub Main()
'this is the variable that will be analyzed in our dataset Const VARIABLE_TO_ANALYZE As Long = 3
'create a new instance of Statistica Dim StatApp As New Statistica.Application()
StatApp.Visible = True
'open a dataset Dim DataSet As Statistica.Spreadsheet
'also note that you can initialize variables when they 'are declared
Try
'note that you do not need to use "Set" in VB.NET DataSet = StatApp.Spreadsheets.Open(StatApp.Path & _ "\Examples\Datasets\Adstudy.sta", True)
Catch ThisException As System.Runtime.InteropServices.COMException
System.Console.Write(ThisException.ToString()) End
End Try
'create a singleton class to calculate our own custom statistics Dim Statistican As New StatsMgr()
'spreadsheet to hold our custom statistics Dim Results As Statistica.Spreadsheet = StatApp.Spreadsheets.[New]("Descriptive Stats from VB.NET")
Results.SetSize(VARIABLE_TO_ANALYZE, 1) Results.CaseNameWidth = 1 Results.VariableName(1) = "Results" Results.Variable(1).AutoFit() Results.Header.Value = "Results for " & DataSet.Name & ", Variable " & VARIABLE_TO_ANALYZE.ToString 'the Sum case Results.CaseName(1) = "SUM" Results.Case(1).Value = Statistican.Sum(DataSet, VARIABLE_TO_ANALYZE) 'the Valid number of observations case Results.CaseName(2) = "VALID N" Results.Case(2).Value = Statistican.ValidObs(DataSet, VARIABLE_TO_ANALYZE)
'the Variance case Results.CaseName(3) = "VARIANCE"
'our variance function my throw an exception if there are not enough 'valid observations in the given variable
Try
Results.Case(3).Value = Statistican.Variance(DataSet, VARIABLE_TO_ANALYZE)
Catch ThisException As StatsMgr.BadResults
Results.Case(3).Value = "----"
End Try
Results.Visible = True
'clean up StatApp = Nothing Statistican = Nothing
End Sub
End Module