*上面的程序可以大幅减化(首数是样本量,末数是可能的秩和,求给定首数时末数对应的概率)
clear
cap pr drop rankext
pr rankext
args n e
if wordcount("`*'")!=2|int(`e')!=`e'|int(`n')!=`n'|`e'<0|`n'<0|`e'>`n'*(`n'+1)/2 {
n di as error "Two and only two nonnegative integers, of which"
n di as error "the last is smaller than the accumlated sum by the first, are allowed"
exit
}
qui{
clear
set ob `=2^`n''
forv i=1/`n'{
g v`i'=`i'*mod(int((_n-1)/2^(`n'-`i')),2)
}
egen exp=rowtotal(v*)
collapse (count)t=v1,by(exp)
egen p=pc(t),prop
su p if exp<=`e'
n di
n di in yellow "exact P-value for the ranksum of `e' given the sample size of `n' = " r(sum)
clear
}
end
|