1.Matlab rank sum test
matlab中的rank sum test基本没有什么坑,直接拿来用就行,两组数据不一样长都可以。
p = ranksum(x,y) returns the p-value of a two-sided Wilcoxon rank sum test. ranksum tests the null hypothesis that data in x and y are samples from continuous distributions with equal medians, against the alternative that they are not. The test assumes that the two samples are independent. x and y can have different lengths.
This test is equivalent to a Mann-Whitney U-test.
p = ranksum(x,y)
[p,h] = ranksum(x,y) also returns a logical value indicating the test decision. The result h = 1 indicates a rejection of the null hypothesis, and h = 0 indicates a failure to reject the null hypothesis at the 5% significance level.
[p,h] = ranksum(x,y)
[p,h,stats] = ranksum(x,y) also returns the structure stats with information about the test statistic.
[p,h,stats] = ranksum(x,y)
[___] = ranksum(x,y,Name,Value) returns any of the output arguments in the previous syntaxes, for a rank sum test with additional options specified by one or more Name,Value pair arguments.
[___] = ranksum(x,y,Name,Value)
2.Python rank sum test
https://stackoverflow.com/questions/60462532/how-to-use-1-side-wilcoxon-rank-sum-test-python/60465812#60465812
讲道理Python同样得到和Matlab一样的结果,只是有一点需要注意。
首先需要导入包
from scipy import stats
之后使用
scipy.stats.mannwhitneyu(x,y,alternative="two-sided")
即可保证两者一致。
import numpy as np
from rpy2.robjects import FloatVector
from rpy2.robjects.packages import importr
stats = importr('stats')
x = np.random.poisson(1,size=20)
y = np.random.poisson(3,size=20)
test = stats.wilcox_test(FloatVector(x),FloatVector(y),alternative='less')
d = { key : test.rx2(key)[0] for key in ['statistic','p.value','alternative'] }
d
References
Reference
https://docs.scipy.org/doc/scipy/reference/stats.html
https://ww2.mathworks.cn/help/stats/ranksum.html
https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test