裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

Julia に翻訳--135 クラスカル・ウォリス検定,exact test

2021年03月28日 | ブログラミング

#==========
Julia の修行をするときに,いろいろなプログラムを書き換えるのは有効な方法だ。
以下のプログラムを Julia に翻訳してみる。

クラスカル・ウォリス検定(exact test)
http://aoki2.si.gunma-u.ac.jp/R/exact-kw.html

ファイル名: exactkw.jl  関数名: exactkw

翻訳するときに書いたメモ

global は不要

==========#

using Rmath, Printf

function exactkw(x, y = []; exact = true)
    function found()
        hh = sum((um * q) .^ 2 ./ rt)
        if hh >= stat_val || abs(hh - stat_val) <= EPSILON
            nprod = sum(perm_table[rt .+ 1]) - sum(perm_table[um .+ 1])
            nntrue += exp(nprod - nntrue2 * log_expmax)
            while nntrue >= EXPMAX
                nntrue /= EXPMAX
                nntrue2 += 1
            end
        end
        ntables += 1
    end

    function search(x, y)
        if y == 1
            found()
        elseif x == 1
            t = um[1, 1] - um[y, 1]
            if t >= 0
                um[1, 1] = t
                search(nc, y - 1)
                um[1, 1] += um[y, 1]
            end
        else
            search(x - 1, y)
            while um[y, 1] != 0 && um[1, x] != 0
                um[y, x] += 1
                um[y, 1] -= 1
                um[1, x] -= 1
                search(x - 1, y)
            end
            um[y, 1] += um[y, x]
            um[1, x] += um[y, x]
            um[y, x] = 0
        end
    end

    function exacttest()
        denom2 = 0
        denom = perm_table[n + 1] - sum(perm_table[ct .+ 1])
        while denom > log_expmax
            denom -= log_expmax
            denom2 += 1
        end
        denom = exp(denom)
        um[:, 1] = rt
        um[1, :] = ct
        search(nc, nr)
        @printf("正確な P 値 = % .10g\n", nntrue / denom * EXPMAX ^ (nntrue2 - denom2))
        @printf("査察した分割表の個数は % s 個\n", ntables)
    end

    function kwtest(u)
        return sum((u * q) .^ 2 ./ rt)
    end

    function asymptotic()
        chisq = (stat_val * 12 / (n * (n + 1)) - 3 * (n + 1)) / (1 - sum(ct .^ 3 .- ct) / (n ^ 3 - n))
        @printf("カイ二乗値 = % g, 自由度 = % i, P 値 = % g\n", chisq, nr - 1, pchisq(chisq, nr - 1, false))
    end

    if ndims(x) == 2
        t = x
    elseif length(y) == 0
        ni = map(length, x)
        y = rep(1:length(ni), ni)
        indexy, indexx, t = table(y, vcat(x...))
    else
        indexy, indexx, t = table(y, x)
    end
    EPSILON = 1e-10
    EXPMAX = 1e100
    log_expmax = log(EXPMAX)
    nr, nc = size(t)
    rt = sum(t, dims=2)
    ct = transpose(sum(t, dims=1))
    um = zeros(Int, nr, nc)
    n = sum(t)
    q = cumsum(vcat(0, ct[1:nc-1])) .+ (ct .+ 1) .* 0.5
    half = (n + 1) * 0.5
    stat_val = kwtest(t)
    asymptotic()
    if exact
        perm_table = cumsum(vcat(0, log.(1:(n + 1))))
        ntables = nntrue = nntrue2 = 0
        exacttest()
    end
end

function rep(x, n::Array{Int64,1})
    length(x) == length(n) || error("length(x) wasn't length(n)")
    vcat([repeat([x[i]], n[i]) for i = 1:length(x)]...)
end

function table(x, y) # 二次元
    indicesx = sort(unique(x))
    indicesy = sort(unique(y))
    counts = zeros(Int, length(indicesx), length(indicesy))
    for (i, j) in zip(indexin(x, indicesx), indexin(y, indicesy))
        counts[i, j] += 1
    end
    return indicesx, indicesy, counts
end

分割表を与える場合 Array{Int64,2}

x = [5 3 2 1

     4 3 5 2
     2 3 1 2]
exactkw(x)
# カイ二乗値 =  1.32485, 自由度 =  2, P 値 =  0.5156
# 正確な P 値 =  0.5268191237
# 査察した分割表の個数は 24871 個

データベクトルと factor ベクトルを与える場合

data = [

    3.42, 3.84, 3.96, 3.76,
    3.17, 3.63, 3.47, 3.44, 3.39,
    3.64, 3.72, 3.91
    ]
group = rep(1:3, [4, 5, 3]);
exactkw(data, group)
# カイ二乗値 =  5.54872, 自由度 =  2, P 値 =  0.0623895
# 正確な P 値 =  0.0538961039
# 査察した分割表の個数は 27720 個

二重配列で与える場合 Array{Array{Float64,1},1}

x = [[3.42, 3.84, 3.96, 3.76], [3.17, 3.63, 3.47, 3.44, 3.39], [3.64, 3.72, 3.91]]

exactkw(x)
# カイ二乗値 =  5.54872, 自由度 =  2, P 値 =  0.0623895
# 正確な P 値 =  0.0538961039
# 査察した分割表の個数は 27720 個

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« Julia に翻訳--134 マン・ホ... | トップ | Julia に翻訳--136 一元配置... »
最新の画像もっと見る

コメントを投稿

ブログラミング」カテゴリの最新記事