#==========
Julia の修行をするときに,いろいろなプログラムを書き換えるのは有効な方法だ。
以下のプログラムを Julia に翻訳してみる。
一般化 Wilcoxon 検定
http://aoki2.si.gunma-u.ac.jp/R/Gen-Wil.html
ファイル名: genwil.jl 関数名: genwil
翻訳するときに書いたメモ
==========#
using Rmath
function genwil(group, event, time)
n = length(group)
o = sortperm(group)
time = time[o]
group = group[o]
event = event[o]
index, (na, nb) = table(group)
W = 0
for i = 1:na
for j = na + 1:n
W += getU(time[i], event[i], time[j], event[j])
end
end
VarW = 0
for i = 1:n
temp = 0
for j = 1:n
temp += getU(time[i], event[i], time[j], event[j])
end
VarW += temp ^ 2
end
VarW = na * nb * VarW / n / (n - 1)
Z = W / sqrt(VarW)
P = pnorm(abs(Z), false) * 2
println("W = $W, V(W) = $VarW, Z = $Z, p value =$P")
Dict(:W => W, :V => VarW, :Z => Z, :pvalue => P)
end
function getU(tx, sx, ty, sy)
if (tx < ty && sx == 1 && sy == 1) || (tx <= ty && sx == 1 && sy == 0)
return -1
elseif (tx > ty && sx == 1 && sy == 1) || (tx >= ty && sx == 0 && sy == 1)
return 1
else
return 0
end
end
function table(x) # indices が少ないとき
indices = sort(unique(x))
counts = zeros(Int, length(indices))
for i in indexin(x, indices)
counts[i] += 1
end
return indices, counts
end
group = [1, 1, 2, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 1];
event = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0];
time = [2, 20, 5, 1, 3, 17, 2, 3, 15, 14, 12, 13, 11, 11, 10, 8, 8, 3, 7, 3, 6, 2, 5, 4, 2, 3, 1, 3, 2, 1];
genwil(group, event, time)
# W = 63, V(W) = 1320.3126436781608, Z = 1.7338126143696353, p value =0.08295133672723795