#==========
Julia の修行をするときに,いろいろなプログラムを書き換えるのは有効な方法だ。
以下のプログラムを Julia に翻訳してみる。
Deming 法による回帰直線
http://aoki2.si.gunma-u.ac.jp/R/Deming.html
ファイル名: deming.jl 関数名: deming
翻訳するときに書いたメモ
sxx, sxy, sxy, syy = cov(hcat(x, y)) * (length(x)-1)
が,スマートなのか疑問?
==========#
using Statistics, Printf, Plots
function deming(x, y; n=1, a=1, sig=0.95, col1=:black, col2=:red,
xlab="", ylab="", legend=:best, hist=false)
intercept, slope = deming0(x, y, a)
if n == 1
@printf("Intercept: %.7g\n", intercept)
@printf(" Slope: %.7g\n", slope)
results = Dict(:intercept => intercept, :slope => slope)
else
interceptvector = zeros(n)
slopevector = zeros(n)
suffix0 = collect(1:length(x))
for i = 1:n
suffix = rand(suffix0, length(x))
interceptvector[i], slopevector[i] = deming0(x[suffix], y[suffix], a)
end
alpha = (1-sig)/2
LCLintercept, UCLintercept = quantile!(interceptvector, [alpha, 1-alpha])
LCLslope, UCLslope = quantile!(slopevector, [alpha, 1-alpha])
@printf(" Estimate %6.3f%% %6.3f%%\n", alpha, 1-alpha)
@printf("Intercept: %8.7g [%8.7g, %8.7g]\n", intercept, LCLintercept, UCLintercept)
@printf("Slope: %8.7g [%8.7g, %8.7g]\n", slope, LCLslope, UCLslope)
results = Dict(:intercept => intercept, :slope => slope,
:LCLintercept => LCLintercept, :UCLintercept => UCLintercept,
:LCLslope => LCLslope, :UCLslope => UCLslope)
!hist || simurationresults(interceptvector, slopevector)
end
plotdeming(x, y, intercept, slope, col1, col2, xlab, ylab, legend)
return results
end
function deming0(x, y, a)
sxx, sxy, sxy, syy = cov(hcat(x, y)) * (length(x)-1)
if sxy != 0
Slope = (syy-a*sxx+sqrt((syy-a*sxx)^2+4*a*sxy^2))/(2*sxy)
Intercept = mean(y)-Slope*mean(x)
else
Slope = Intercept = NaN
end
return Intercept, Slope
end
function simurationresults(interceptvector, slopevector)
plthist1 = histogram(interceptvector, grid=false, tick_direction=:out,
alpha=0.2, label="intercept")
plthist2 = histogram(slopevector, grid=false, tick_direction=:out,
alpha=0.2, label="slope")
plthist0 = plot(plthist1, plthist2)
display(plthist0)
end
function plotdeming(x, y, intercept, slope, col1, col2, xlab, ylab, legend)
abline(intercept, slope, col, label) =
plot!([minx, maxx],
[slope * minx + intercept, slope * maxx + intercept],
color=col, label=label, legend=legend)
function lm(x, y)
b = cov(x, y)/var(x)
return mean(y) - b * mean(x), b
end
minx, maxx = extrema(x)
margin = (maxx - minx) * 0.05
minx -= margin
maxx += margin
pyplot()
plt = scatter(x, y, grid=false, tick_direction=:out,
xlabel=xlab, ylabel=ylab, color=col1, label="")
abline(intercept, slope, col1, "Deming")
a, b = lm(x, y)
abline(a, b, col2, "linear regression")
display(plt)
end
x = [594, 422, 516, 432, 300, 452, 498, 281, 445, 717, 507, 582,
656, 512, 330, 477, 449, 634, 430, 637, 577, 391, 550, 347, 544,
426, 518, 493, 508, 442, 418, 519, 443, 479, 521, 450, 624, 616,
338, 544, 383, 526, 706, 488, 589, 425, 655, 527, 519, 562];
y = [616, 401, 424, 396, 416, 335, 447, 322, 507, 680, 588, 614,
667, 445, 403, 431, 634, 440, 395, 676, 616, 517, 521, 457, 561,
371, 556, 437, 491, 379, 434, 473, 443, 524, 479, 406, 643, 533,
480, 519, 422, 423, 651, 406, 531, 617, 720, 468, 478, 608];
deming(x, y)
# Intercept: 0.8898059
# Slope: 0.9983003
# Dict{Symbol,Float64} with 2 entries:
# :intercept => 0.889806
# :slope => 0.9983
deming(x, y, n=1000, hist=true)
# Estimate 0.025% 0.975%
# Intercept: 0.8898059 [-154.9576, 113.4234]
# Slope: 0.9983003 [0.7733284, 1.287288]
# Dict{Symbol,Float64} with 6 entries:
# :UCLintercept => 113.423
# :UCLslope => 1.28729
# :intercept => 0.889806
# :slope => 0.9983
# :LCLslope => 0.773328
# :LCLintercept => -154.958