Visual Basic and Excel: How do I replicate the results of excel’s internal function for paired dual tailed t-test in Visual Basic. I tried and failed, where did I go wrong?
I’m admittedly a novice at Visual basic, but I succeeded in reproducing the t-test for Two-sample equal variance (homoscedastic) in Visual Basic. TTEST(array1,array2,2,2). I’m at a loss why I can’t be able to do the same for the paired t-test.
One possible explanation could be that I’m not using the same formula as excel does, but the formula that excel uses for the TTEST(array1,array2,2,1) is nowhere to be found. I used the formula in my statistics book that agreed with the one called Dependent t-test from the
Wikipedia page for student’s T-test .
Is that the one excel uses for its TTEST(array1,array2,2,1) function?
Where else could I have gone wrong?
This is the code I made (again, I’m a novice, be gentle):
'The input consist of the area the data is located in, the columns containing the data we will use and the number of rows the area consist of
Function t_test1(area, column1, column2, NoOfRows)
Dim i, X_SubD, X_SubD2, Sample_stdev, T_TestValue
'Will eventually become the average difference
X_SubD = 0
'Will eventually become the sum of squared difference
X_SubD2 = 0
Sample_stdev = 0
For i = 1 To NoOfRows
X_SubD = X_SubD + (area(i, column1) - area(i, column2))
X_SubD2 = X_SubD2 + (area(i, column1) - area(i, column2)) ^ 2
Next i
'Calculate the average
X_SubD = X_SubD / NoOfRows
'Calculate the sample standard deviation
Sample_stdev = ((X_SubD2 - NoOfRows * X_SubD ^ 2) / (NoOfRows - 1)) ^ 0.5
'Calculate T-value
T_TestValue = X_SubD / (Sample_stdev / (NoOfRows ^ 0.5))
'Look up the significance and return the value
If T_TestValue < 0 Then
t_test1 = Application.WorksheetFunction.TDist(-T_TestValue, 2 * NoOfRows - 1, 2)
Else
t_test1 = Application.WorksheetFunction.TDist(T_TestValue, 2 * NoOfRows - 1, 2)
End If
End Function
posted by a robot made out of meat at 4:48 AM on May 16