Scipy stats t-test for the means and degrees of freedom

137 Views Asked by At

I am using the stats module from scipy and in particular the function ttest_ind. I want to extract information related to the degrees of freedom when I apply this test. According to the SciPy v1.11.4 documentation, link, it is mention that the following values are return:

  • statistic: t-statistic
  • pvalue: p-value associated with the given alternative
  • df: degrees of freedom used in calculation of the t-statistic

However using the following reproducible example I don't see that this is possible:

from scipy.stats import ttest_ind

# Example data for two groups
group1 = [25, 30, 22, 28, 32]
group2 = [18, 24, 20, 26, 19]

t_statistic, p_value, degrees_of_freedom = ttest_ind(group1, group2, permutations=None)
#> Traceback (most recent call last):
#> Cell In[6], line 1
#> ----> 1 t_statistic, p_value, degrees_of_freedom = ttest_ind(group1, group2, permutations=None)
#> ValueError: not enough values to unpack (expected 3, got 2)

It is an error in the documentation or there is a way to obtain the degrees of freedom?

1

There are 1 best solutions below

1
On BEST ANSWER

The part right before explains how to handle this:

Returns:

resultTtestResult
An object with the following attributes:

In other words, the function returns an object containing the data, rather than a tuple containing the data.

You can get the data out of that object like so:

from scipy.stats import ttest_ind

# Example data for two groups
group1 = [25, 30, 22, 28, 32]
group2 = [18, 24, 20, 26, 19]

result = ttest_ind(group1, group2, permutations=None)
print("t", result.statistic)
print("pvalue", result.pvalue)
print("df", result.df)