# ttest()¶

Returns data tables as Pandas DataFrames with relevant information pertaining to the statistical test conducted. Returns 2 DataFrames so all information can easily be exported, except for Wilcoxon ranked-sign test- only 1 DataFrame is returned.

DataFrame 1 (all except Wilcoxon ranked-sign test) has summary statistic
information including variable name, total
number of non-missing observations, standard deviation, standard error, and
the 95% confidence interval. This is the same information returned from the
*summary_cont()* method.

DataFrame 2 (all except Wilcoxon ranked-sign test) has the test results for the statistical tests. Included in this is an effect size measures of r, Cohen’s d, Hedge’s g, and Glass’s \(\Delta\) for the independent sample t-test, paired sample t-test, and Welch’s t-test.

For the Wilcoxon ranked-sign test, the returned DataFrame contains the mean for both comparison points, the T-value, the Z-value, the two-sided p-value, and effect size measure r.

- This method can perform the following tests:

## Arguments¶

**ttest(group1, group2, group1_name= None, group2_name= None, equal_variances= True, paired= False, correction= None)**

group1andgroup2, requires the data to be a Pandas Series

group1_nameandgroup2_name, will override the series name

equal_variances, tells whether equal variances is assumed or not.If not, Welch’s t-test is used if data is unpaired, or Wilcoxon rank-signed test is used if data is paired. The default is True.

paired, tells whether the data is paired. If data is paired and equalvariance is assumed, a paired sample t-test is conducted, otherwise a Wilcoxon ranked-sign test is conducted. The default is False.

**returns**

- 2 Pandas DataFrames as a tuple;

First returned DataFrame is the summary statistics

Second returned DataFrame is the test results.

Except for Wilcoxon ranked-sign test, only 1 DataFrame is returned

Note

Wilcoxon ranked-sign test: a 0 difference between the 2 groups is discarded from the calculation. This is the ‘wilcox’ method apart of scipy.stats.wilcoxon

### Effect size measures formulas¶

#### Cohen’s d_{s} (between subjects design)¶

Cohen’s d_{s} [4] for a between groups design is calculated
with the following equation:

Rosenthal [8] provided the following formula to calculate
Cohen’s d_{s} using the t-value and the number of participants from each
group. This returns an identical Cohen’s d_{s} value as the original
formula.

Computationally speaking, the formula provided by Rosenthal is faster, therefore
it is used to calculate Cohen’s d_{s}.

#### Hedges’s g_{s} (between subjects design)¶

Cohen’s d_{s} gives a biased estimate of the effect size for a population
and Hedges and Olkin [5] provides an unbiased estimation. The
differences between Hedges’s g and Cohen’s d is negligible when sample sizes
are above 20, but it is still preferable to report Hedges’s g [6].
Hedge’s g_{s} is calculated using the following formula:

#### Glass’s \(\Delta\) (between or within subjects design)¶

Glass’s \(\Delta\) is the mean differences between the two groups divided by the standard deviation of the control group. When used in a within subjects design, it is recommended to use the pre- standard deviation in the denominator [7]; the following formula is used to calculate Glass’s \(\Delta\):

#### Cohen’s d_{z} (within subject design)¶

Another version of Cohen’s d is used in within subject designs. This is noted
by the subscript “z”. The formula for Cohen’s d_{z} [4] is
as follows:

Cohen’s d_{z} can also be calculated with the following formula using the
t-value and number of participants provided by Rosenthal [8].
This formula is used to calculate Cohen’s d_{z} since it is computationally
quicker.

#### Pearson correlation coefficient r (between or within subjects design)¶

Rosenthal [8] provided the following formula to calculate the Pearson correlation coefficient r using the t-value and degrees of freedom:

Rosenthal [8] provided the following formula to calculate the Pearson correlation coefficient r using the z-value and N. This formula is used to calculate the r coefficient for the Wilcoxon ranked-sign test.

\[r = \sqrt{\frac{Z}{\sqrt{N}}}\]

## Examples¶

```
import numpy, pandas, researchpy
numpy.random.seed(12345678)
df = pandas.DataFrame(numpy.random.randint(10, size= (100, 2)),
columns= ['healthy', 'non-healthy'])
```

```
# Independent t-test
# If you don't store the 2 returned DataFrames, it outputs as a tuple and
# is displayed
researchpy.ttest(df['healthy'], df['non-healthy'])
```

```
( Variable N Mean SD SE 95% Conf. Interval
0 healthy 100.0 4.590 2.749086 0.274909 4.044522 5.135478
1 non-healthy 100.0 4.160 3.132495 0.313250 3.538445 4.781555
2 combined 200.0 4.375 2.947510 0.208420 3.964004 4.785996,
Independent t-test results
0 Difference (healthy - non-healthy) = 0.4300
1 Degrees of freedom = 198.0000
2 t = 1.0317
3 Two side test p value = 0.3035
4 Difference < 0 p value = 0.8483
5 Difference > 0 p value = 0.1517
6 Cohen's d = 0.1459
7 Hedge's g = 0.1454
8 Glass's delta = 0.1564
9 r = 0.0731)
```

```
# Otherwise you can store them as objects
des, res = researchpy.ttest(df['healthy'], df['non-healthy'])
des
```

Variable | N | Mean | SD | SE | 95% Conf. | Interval | |
---|---|---|---|---|---|---|---|

0 | healthy | 100.0 | 4.590 | 2.749086 | 0.274909 | 4.044522 | 5.135478 |

1 | non-healthy | 100.0 | 4.160 | 3.132495 | 0.313250 | 3.538445 | 4.781555 |

2 | combined | 200.0 | 4.375 | 2.947510 | 0.208420 | 3.964004 | 4.785996 |

```
res
```

Independent t-test | results | |
---|---|---|

0 | Difference (healthy - non-healthy) = | 0.4300 |

1 | Degrees of freedom = | 198.0000 |

2 | t = | 1.0317 |

3 | Two side test p value = | 0.3035 |

4 | Difference < 0 p value = | 0.8483 |

5 | Difference > 0 p value = | 0.1517 |

6 | Cohen's d = | 0.1459 |

7 | Hedge's g = | 0.1454 |

8 | Glass's delta = | 0.1564 |

9 | r = | 0.0731 |

```
# Paired samples t-test
des, res = researchpy.ttest(df['healthy'], df['non-healthy'],
paired= True)
des
```

Variable | N | Mean | SD | SE | 95% Conf. | Interval | |
---|---|---|---|---|---|---|---|

0 | healthy | 100.0 | 4.59 | 2.749086 | 0.274909 | 4.044522 | 5.135478 |

1 | non-healthy | 100.0 | 4.16 | 3.132495 | 0.313250 | 3.538445 | 4.781555 |

2 | diff | 100.0 | 0.43 | 4.063275 | 0.406327 | -0.376242 | 1.236242 |

```
res
```

Paired samples t-test | results | |
---|---|---|

0 | Difference (healthy - non-healthy) = | 0.4300 |

1 | Degrees of freedom = | 99.0000 |

2 | t = | 1.0583 |

3 | Two side test p value = | 0.2925 |

4 | Difference < 0 p value = | 0.8537 |

5 | Difference > 0 p value = | 0.1463 |

6 | Cohen's d = | 0.1058 |

7 | Hedge's g = | 0.1054 |

8 | Glass's delta = | 0.1564 |

9 | r = | 0.1058 |

```
# Welch's t-test
des, res = researchpy.ttest(df['healthy'], df['non-healthy'],
equal_variances= False)
des
```

Variable | N | Mean | SD | SE | 95% Conf. | Interval | |
---|---|---|---|---|---|---|---|

0 | healthy | 100.0 | 4.590 | 2.749086 | 0.274909 | 4.044522 | 5.135478 |

1 | non-healthy | 100.0 | 4.160 | 3.132495 | 0.313250 | 3.538445 | 4.781555 |

2 | combined | 200.0 | 4.375 | 2.947510 | 0.208420 | 3.964004 | 4.785996 |

```
res
```

Welch's t-test | results | |
---|---|---|

0 | Difference (healthy - non-healthy) = | 0.4300 |

1 | Degrees of freedom = | 194.7181 |

2 | t = | 1.0317 |

3 | Two side test p value = | 0.3035 |

4 | Difference < 0 p value = | 0.8483 |

5 | Difference > 0 p value = | 0.1517 |

6 | Cohen's d = | 0.1459 |

7 | Hedge's g = | 0.1454 |

8 | Glass's delta = | 0.1564 |

9 | r = | 0.0737 |

```
# Wilcoxon signed-rank test
researchpy.ttest(df['healthy'], df['non-healthy'],
equal_variances= False, paired= True)
```

Wilcoxon signed-rank test | results | |
---|---|---|

0 | Mean for healthy = | 4.5900 |

1 | Mean for non-healthy = | 4.1600 |

2 | T value = | 1849.5000 |

3 | Z value = | -0.9638 |

4 | Two sided p value = | 0.3347 |

5 | r = | -0.0681 |

```
# Exporting descriptive table (des) and result table (res) to same
# csv file
des, res = researchpy.ttest(df['healthy'], df['non-healthy'])
des.to_csv("C:\\Users\\...\\test.csv", index= False)
res.to_csv("C:\\Users\\...\\test.csv", index= False, mode= 'a')
```

## References¶

- 1(1,2)
*scipy.stats.ttest_ind*. The SciPy community, 2018. Retrieved when last updated on May 5, 2018. URL: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html.- 2
*scipy.stats.ttest_rel*. The SciPy community, 2018. Retrieved when last updated on May 5, 2018. URL: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_rel.html.- 3
*scipy.stats.wilcoxon*. The SciPy community, 2018. Retrieved when last updated on May 5, 2018. URL: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.wilcoxon.html.- 4(1,2)
Jacob Cohen.

*Statistical Power Analysis for the Behavioral Sciences*. Lawrence Erlbaum Associates, second edition, 1988. ISBN 0-8058-0283-5.- 5
Larry Hedges and Ingram Olkin.

*Journal of Educational Statistics*, chapter Statistical Methods in Meta-Analysis. Volume 20. Academic Press, Inc., 1985, 10.2307/1164953.- 6
Rex B. Kline.

*Beyond significance testing: Reforming data analysis methods in behavioral research*. American Psychological Association, 2004. http://dx.doi.org/10.1037/10693-000.- 7
Daniel Lakens. Calculating and reporting effect sizes to facilitate cumulative science: a practical primer for t-tests and anovas.

*Frontiers in Psychology*, November 2013. doi:10.3389/fpsyg.2013.00863.- 8(1,2,3,4)
Robert Rosenthal.

*The hand-book of research synthesis*, chapter Parametric measures of effect size, pages 231–244. New York, NY: Russel Sage Foundation, 1994.