Skip to content
Advertisement

stats.ttest_ind() vs. “manual” computation of Student’s independent t-test: different results

I am comparing stats.ttest_ind() vs “manual” computation of the same test, and get different results.

JavaScript

stats.ttest_ind() method:

JavaScript

Out:

JavaScript

Manual method:

JavaScript

Out:

JavaScript

We can see there’s a small difference. Why? Maybe because of how stats.ttest_ind() computes degrees of freedom? Any insight much appreciated.

Advertisement

Answer

The following works. It is your code from above, with only two rows changed.

JavaScript

and it outputs

JavaScript

The reason for the non-consistency in your code is this:

On the line test_stat, test_p = stats.ttest_ind(men, women) you accepted the default setting that the t-test is to be computed by the equal-variances assumption. So the computation that scipy.stats gives you is a pure equal-variance t-test. That is described in the documentation for scipy.stats.ttest_ind

In your own code, you followed the Welch test in general: you computed the estimate of the mean and its standard error for the men and women separately, and computed the t-statistic in that way.

You did deviate from the Welch test in one place: the degrees-of-freedom computation. The degrees of freedom should be approximated with the formula I entered in the code (and linked to above), but you used the computation applicable under equal-variance assumptions.

IF you want more details on how to compute these statistics, or why they are defined as they are, or why your code is not what you expected it, I suggest you check out https://stats.stackexchange.com/ and https://datascience.stackexchange.com/ that are more appropriate for statistics questions, in comparison to https://stackoverflow.com/ that is more about the programming. Both those communities are proficient in python as well, so they should be able to help you out perfectly good.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement