Solved – How to implement formula for a independent groups t-test in C#

I am writing a program in C# that requires me to use the Ttest formula. I have to effectively interpret the Excel formula:

=TTEST(range1,range2,1,3) 

I am using the formula given here

and have interpreted into code as such:

 double TStatistic = (mean1 - mean2) / Math.Sqrt((Math.Pow(variance1, 2) /          count1) + (Math.Pow(variance2, 2) / count2)); 

However, I don't fully understand t-test and the values I am getting are completely different than those calculated within Excel.

I have been using the following ranges:

R1: 91.17462277, 118.3936425, 96.6746393, 102.488785, 91.26831043  R2: 17.20546254, 19.56969811, 19.2831241, 13.03360631, 13.86577314 

The value I am getting using my attempt is 1.8248, however that from Excel is 1.74463E-05. Could somebody please point me in the right direction?

There are at least two problems with what you have done.

  1. You have misinterpreted the formula $$t = frac{bar{x}_1-bar{x}_2}{sqrt{s_1^2 / n_1 + s_2^2 / n_2}}$$ since $s^2$ is already a variance (square of standard deviation) and does not need to be squared again.
  2. You are comparing eggs and omelettes: you need compare your "calculated $t$-value, with $k$ degrees of freedom … to the $t$ distribution table". Excel has already done this with TTEST().

There are other possible issues such as using a population variance or sample variance formula.

Similar Posts:

Rate this post

Leave a Comment