#==========
Julia の修行をするときに,いろいろなプログラムを書き換えるのは有効な方法だ。
以下のプログラムを Julia に翻訳してみる。
一対比較データの双対尺度法(西里)
http://aoki2.si.gunma-u.ac.jp/R/pc.dual.html
ファイル名: pcdual.jl 関数名: pcdual, plotpcdual
翻訳するときに書いたメモ
考えて見れば当たり前なんだが,annotate! にもドットを付けることができるのだ。
その際,必要ならば text にもドットを付けることを忘れずに。
==========#
using Rmath, Combinatorics, LinearAlgebra, Printf, Plots
function pcdual(F, onetwo = true)
N, C = size(F)
if onetwo
F[F .== 2] .= - 1
end
reshape(F, 14, :)
n = floor(Int, 1 + sqrt(1 + 8 * C) / 2)
x = combinations(1:n, 2)
nc = binomial(8, 2)
A = zeros(nc, n)
for (i, (j, k)) in enumerate(x)
A[i, j] = 1
A[i, k] = -1
end
E = F * A
Hn = transpose(E) * E ./ (N * n * (n - 1) ^ 2)
values, vectors = eigen(Hn, sortby=x-> -x)
ne = size(Hn, 1) - 1
eta2 = values[1:ne]
eta = sqrt.(eta2)
contribution = eta2 ./ sum(values[1:ne]) * 100
cumcont = cumsum(contribution)
W = vectors[:, 1:ne]
colscore = W .* sqrt(n)
colscore2 = colscore .* transpose(eta)
rowscore2 = E * (W ./ (sqrt(n) * (n - 1)))
rowscore = rowscore2 ./ transpose(eta)
printpcdual(N, n, ne, eta2, eta, contribution, cumcont,
rowscore, colscore, rowscore2, colscore2)
Dict(:eta2 => eta2, :eta => eta, :contribution => contribution,
:cumcont => cumcont, :rowscore => rowscore,
:colscore => colscore, :rowscoreweighted => rowscore2,
:colscoreweighted => colscore2)
end
function printrow(name, x)
@printf("%9s", name)
for y in x
@printf("%8.3f", y)
end
println()
end
function printheader(ne)
@printf("%9s", "")
for i = 1:ne
@printf("%8s", "Axis-" * string(i))
end
println()
end
function printpcdual(N, n, ne, eta2, eta, contribution, cumcont,
rowscore, colscore, rowscore2, colscore2)
printheader(ne)
printrow("eta sq.", eta2)
printrow("cor.", eta)
printrow("cont", contribution)
printrow("cum.cont.", cumcont)
println("\nrow score")
printheader(ne)
for i = 1:N
printrow("Row-" * string(i), rowscore[i, :])
end
println("\ncolumn score")
printheader(ne)
for i = 1:n
printrow("Col-" * string(i), colscore[i, :])
end
println("\nweighted row score")
printheader(ne)
for i = 1:N
printrow("Row-" * string(i), rowscore2[i, :])
end
println("\nweighted column score")
printheader(ne)
for i = 1:n
printrow("Col-" * string(i), colscore2[i, :])
end
end
function plotpcdual(results; weighted=true, ax1=1, ax2=2)
pyplot()
if weighted
row, col = results[:rowscore], results[:colscore]
else
row, col = results[:rowscoreweighted], results[:colscoreweighted]
end
rownames = [" R" * string(i) for i = 1:size(row, 1)]
colnames = [" C" * string(i) for i = 1:size(col, 1)]
plt = scatter(row[:, ax1], row[:, ax2], grid=false, tick_direction=:out,
color=:red, xlabel="Axis-"*string(ax1), ylabel="Axis-"*string(ax2),
aspect_ratio=1, size=(600, 600), label="")
annotate!.(row[:, ax1], row[:, ax2], text.(rownames, 8, :red, :left));
scatter!(col[:, ax1], col[:, ax2], grid=false, tick_direction=:out,
color=:blue, label="")
annotate!.(col[:, ax1], col[:, ax2], text.(colnames, 8, :blue, :left))
display(plt)
end
F = [ 1 1 2 1 1 2 1 2 2 2 2 2 2 2 1 1 2 1 1 1 2 1 1 2 1 2 1 2
2 2 2 1 2 1 2 1 2 1 2 1 2 2 1 1 1 2 1 1 1 2 2 2 2 1 2 2
1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 2 1 1 1 2 1 2 2 2 2 1 1
2 1 2 1 1 1 2 1 1 1 1 1 2 2 1 2 2 2 1 1 1 2 2 2 2 2 2 2
2 2 2 1 2 1 2 2 2 1 2 2 2 2 1 2 1 2 1 1 1 1 2 2 2 1 2 2
1 1 1 1 1 1 1 2 2 1 2 2 2 2 1 2 2 2 1 1 1 1 2 2 2 2 2 1
1 1 1 1 1 2 1 1 2 1 1 2 1 2 1 1 2 1 1 1 2 1 2 2 2 2 2 1
1 1 1 1 1 2 1 1 2 1 2 2 1 2 1 2 2 1 1 2 2 1 2 2 1 2 1 1
1 2 2 1 1 2 1 2 2 1 1 2 2 1 1 1 2 1 1 1 2 1 2 2 2 2 2 1
1 2 1 1 2 2 2 2 2 1 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 1 1 2
1 2 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
2 2 2 2 1 2 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 2 1
1 2 1 1 2 1 2 2 2 2 2 2 2 1 1 1 1 1 1 2 1 2 2 2 2 1 1 2
2 2 2 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 2 1 2 1 1 ];
results = pcdual(F)
#==
Axis-1 Axis-2 Axis-3 Axis-4 Axis-5 Axis-6 Axis-7
eta sq. 0.141 0.110 0.065 0.055 0.028 0.013 0.006
cor. 0.376 0.331 0.255 0.235 0.169 0.114 0.075
cont 33.719 26.241 15.591 13.176 6.800 3.122 1.352
cum.cont. 33.719 59.960 75.550 88.726 95.526 98.648 100.000
row score
Axis-1 Axis-2 Axis-3 Axis-4 Axis-5 Axis-6 Axis-7
Row-1 1.101 -0.417 -0.436 -0.156 1.989 0.483 1.119
Row-2 0.334 1.473 1.417 -0.449 -0.520 0.458 -0.510
Row-3 1.211 -1.160 0.105 0.158 -1.527 -0.377 -0.818
Row-4 0.418 0.507 2.145 0.783 -0.898 -1.179 0.386
Row-5 0.669 1.481 0.872 -0.332 0.255 1.894 0.030
Row-6 1.498 0.195 0.345 1.129 0.984 -0.226 -0.539
Row-7 1.422 -0.968 0.693 0.294 -0.299 -0.354 0.098
Row-8 1.086 -0.927 -0.518 1.193 -0.501 2.117 -1.057
Row-9 1.528 -0.366 0.095 -1.011 -0.122 -0.241 2.161
Row-10 0.808 1.149 -1.173 -0.618 -1.593 0.316 1.124
Row-11 1.296 0.613 -0.546 -0.939 1.037 -1.691 -1.601
Row-12 -0.120 -0.651 1.513 -1.906 0.797 0.856 -0.689
Row-13 0.678 1.453 -1.257 -0.585 -0.387 -0.071 -0.987
Row-14 -0.024 -1.289 -0.098 -1.995 -0.922 0.160 -0.555
column score
Axis-1 Axis-2 Axis-3 Axis-4 Axis-5 Axis-6 Axis-7
Col-1 1.042 -0.412 -0.488 1.244 0.373 -1.364 -1.400
Col-2 -0.923 -0.949 1.504 -0.140 -1.563 -0.037 -0.722
Col-3 0.506 0.361 -1.017 -2.246 -0.468 -0.411 -0.382
Col-4 0.796 0.248 1.388 -0.443 1.567 1.240 -0.435
Col-5 -1.950 -0.720 -0.721 -0.077 1.403 -0.293 0.317
Col-6 -0.389 1.078 -1.113 0.972 -0.775 1.644 -0.449
Col-7 1.091 -1.380 -0.361 0.326 -0.396 0.429 1.824
Col-8 -0.173 1.776 0.807 0.365 -0.140 -1.209 1.246
weighted row score
Axis-1 Axis-2 Axis-3 Axis-4 Axis-5 Axis-6 Axis-7
Row-1 0.414 -0.138 -0.111 -0.037 0.335 0.055 0.084
Row-2 0.126 0.488 0.362 -0.105 -0.088 0.052 -0.038
Row-3 0.455 -0.384 0.027 0.037 -0.258 -0.043 -0.062
Row-4 0.157 0.168 0.548 0.184 -0.151 -0.135 0.029
Row-5 0.251 0.491 0.223 -0.078 0.043 0.216 0.002
Row-6 0.563 0.065 0.088 0.265 0.166 -0.026 -0.041
Row-7 0.534 -0.321 0.177 0.069 -0.050 -0.040 0.007
Row-8 0.408 -0.307 -0.132 0.280 -0.084 0.242 -0.079
Row-9 0.574 -0.121 0.024 -0.237 -0.021 -0.028 0.162
Row-10 0.304 0.381 -0.300 -0.145 -0.269 0.036 0.085
Row-11 0.487 0.203 -0.139 -0.220 0.175 -0.193 -0.120
Row-12 -0.045 -0.216 0.386 -0.448 0.134 0.098 -0.052
Row-13 0.255 0.482 -0.321 -0.137 -0.065 -0.008 -0.074
Row-14 -0.009 -0.427 -0.025 -0.468 -0.155 0.018 -0.042
weighted column score
Axis-1 Axis-2 Axis-3 Axis-4 Axis-5 Axis-6 Axis-7
Col-1 0.391 -0.137 -0.125 0.292 0.063 -0.156 -0.105
Col-2 -0.347 -0.315 0.384 -0.033 -0.264 -0.004 -0.054
Col-3 0.190 0.119 -0.260 -0.527 -0.079 -0.047 -0.029
Col-4 0.299 0.082 0.355 -0.104 0.264 0.142 -0.033
Col-5 -0.732 -0.239 -0.184 -0.018 0.237 -0.033 0.024
Col-6 -0.146 0.357 -0.284 0.228 -0.131 0.188 -0.034
Col-7 0.410 -0.457 -0.092 0.077 -0.067 0.049 0.137
Col-8 -0.065 0.588 0.206 0.086 -0.024 -0.138 0.094
==#
plotpcdual(results)
※コメント投稿者のブログIDはブログ作成者のみに通知されます