2020年11月19日 星期四

[ Python 常見問題 ] How to calculate p-value for two lists of floats?

 Source From Here

Question
So I have lists of floats. Like [1.33,2.555,3.2134,4.123123] etc. Those lists are mean frequencies of something. How do I proof that two lists are different? I thought about calculating p-value. Is there a function to do that? I looked through scipy documentation, but couldn't figure out what to use.

HowTo
Let's say you have a list of floats like this:
  1. data = {'a': [0.91.01.11.2],'b': [0.80.91.01.1],'c': [4.95.05.15.2],}  
Clearly, a is very similar to b, but both are different from c. There are two kinds of comparisons you may want to do:
1. Pairwise: Is similar to b? Is a similar to c? Is b similar to c?
2. Combined: Are ab and c drawn from the same group? (This is generally a better question)

The former can be achieved using independent t-tests as follows:
>>> data = {'a': [0.9, 1.0, 1.1, 1.2], 'b': [0.8, 0.9, 1.0, 1.1], 'c': [4.9, 5.0, 5.1, 5.2]}
>>> from itertools import combinations
>>> from scipy.stats import ttest_ind
>>> for k1, k2 in combinations(data.keys(), 2):
... t, p = ttest_ind(data[k1], data[k2])
... print(f"{k1} with {k2} has p={p}")
...
a with b has p=0.3153335962012298
a with c has p=9.458950025885854e-09
b with c has p=8.159638048425986e-09

This provides the relevant p-values, and implies that that a and c are different, b and c are different, but a and b may be similar. (Check "What a p-value tells you about statistical significance". A p-value less than 0.05 is statistically significant in difference.)

The latter can be achieved using the one-way ANOVA as follows:
  1. >>> from scipy.stats import f_oneway  
  2. >>> t, p =  f_oneway(*data.values())  
  3. >>> p  
  4. 7.959305946160327e-12  
The p-value indicates that ab, and c are unlikely to be from the same population.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...