Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy t-test one sample from statistics?

Tags:

Is it possible to do a t-test using scipy.stats.ttest_1samp where the input is a statistic rather than an array? For example, with difference in means you have two options: ttest_ind() and ttest_ind_from_stats().

import numpy as np
import scipy.stats as stats
from scipy.stats import norm

mean1=35.6
std1=11.3
nobs1=84
mean2=44.7
std2=8.9
nobs2=84
print(stats.ttest_ind_from_stats(mean1, std1, nobs1, mean2, std2, nobs2, equal_var=False))
# alternatively, you can pass 2 arrays
print(stats.ttest_ind(
stats.norm.rvs(loc=mean1, scale=std1, size=84), 
stats.norm.rvs(loc=mean2, scale=std2, size=84),
equal_var=False)
 )

Is there an equivalent function with a one-sample t-test? Thank you for your help.

like image 417
user2822693 Avatar asked Dec 21 '17 12:12

user2822693


1 Answers

TL;DR

There is no such function for the one sample test, but you can use the two sample function. In short, to perform a one sample t-test do this:

sp.stats.ttest_ind_from_stats(mean1=sample_mean, 
                              std1=sample_std, 
                              nobs1=n_samples, 
                              mean2=population_mean, 
                              std2=0, 
                              nobs2=2, 
                              equal_var=False)

Note that the result is completely independent from nobs2 (as it should be, since there is no n2 in the one sample test). Just make sure to pass in a value >1 to avoid a division by zero.


How does it work?

Check out the Wikipedia page about the different types of t-test.

The one sample t-test uses the statistic

enter image description here

with n - 1 degrees of freedom.

The ttest_ind_from_stats function can do Welch's t-test (unequal sample size, unequal variance), which is defined as

enter image description here with enter image description here

and degrees of freedom:

enter image description here

We can transform the definition of Welch's t-test to the one sample t-test. If we set mean2 to the population mean and std2 to 0 the equations for the t-statistic are the same, and the degrees of freedom reduces to n - 1.

like image 51
MB-F Avatar answered Nov 22 '22 12:11

MB-F