#=
Julia の修行をするときに,いろいろなプログラムを書き換えるのは有効な方法だ。
以下のプログラムを Julia に翻訳してみる。
正規確率紙に累積相対度数をプロットする
http://aoki2.si.gunma-u.ac.jp/R/npp.html
# 正規確率紙に累積相対度数をプロットする
npp <- function(y, # 度数ベクトル
x=NULL, # 階級代表値
plt=TRUE, # データ点をプロットする
xlab=NULL, # 横軸ラベル
ylab=NULL, # 縦軸ラベル
main=NULL) # 図のタイトル
{
if (length(y) < 3) { # 階級数が2以下のときには正規確率紙のみを出力する
y <- 1:11
plt <- FALSE
}
y <- cumsum(y)/sum(y) # 累積相対度数
if (is.null(x)) {
x = seq(along=y)
}
if (is.null(xlab)) xlab <- "観察値"
if (is.null(ylab)) ylab <- "累積相対度数"
if (is.null(main)) main <- "正規確率紙"
probs <- c(0.01, 0.1, 1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99, 99.9, 99.99)/100
plot(c(x[1], x[length(x)-1]), qnorm(c(probs[1], probs[17])), type="n", xaxt="n", yaxt="n",
xlab=xlab, ylab=ylab, main=main)
abline(h=qnorm(probs), v=x, col="grey") # 格子線を引く
if (plt) { # データ点をプロットする
points(x, qnorm(y), type="b")
text(x, qnorm(y), round(y, digit=3)*100, pos=1)
}
axis(1, at=x) # 横軸を描く
axis(2, at=qnorm(probs), labels=probs*100) # 縦軸を描く
}
ファイル名: npp.jl 関数名:npp
翻訳するときに書いたメモ
scatter! と annotate! の位置関係で図が表示できないというトラブルがあったが,バグか?
annotate! も,書き方によっては引数にベクトルが使えないので,for loopで書かざるを得ない?
=#
using Distributions
using Plots
using Plots.PlotMeasures
function npp(y; x = [], plt = true, leftmargin=2mm,
xlab = "観察値", ylab = "累積確率", main = "正規確率紙")
pyplot()
if length(y) < 3
y = 1:11
plt = false
end
y = cumsum(y)/sum(y)
x = length(x) == 0 ? collect(1:length(y)) : x
probability = [0.01, 0.1, 1, 5, 10, 20, 30, 40, 50,
60, 70, 80, 90, 95, 99, 99.9, 99.99]/100;
qnp = quantile(Normal(), probability);
delta = (x[2] - x[1]) / 2
p = plot([x[1], x[1], x[end-1]], [qnp[end], qnp[1], qnp[1]],
xlims = (x[1]-delta, x[end-1]+delta),
linecolor=:gray,
linestyle=:dot,
tickdirection=:out,
grid=false,
label="",
xlabel=xlab,
ylabel=ylab,
title=main,
leftmargin=leftmargin,
size=(600, 400))
yticks!(quantile(Normal(), probability), string.(round.(probability, digits=4)))
hline!(quantile(Normal(), probability), linecolor = :gray, linestyle=:dot, label="")
vline!(x, linecolor=:gray, linestyle=:dot, label="")
if plt
qny = quantile(Normal(), y)
scatter!(x[1:end-1], qny[1:end-1], markercolor=:black, label="")
stry = string.(round.(y, digits=3))
for i = 1:length(x)-1
annotate!(x[i], qny[i]-0.3, text(stry[i], 8))
end
end
display(p)
end
npp([1, 2, 3, 4, 3, 2, 1])
npp([1, 2, 3, 4, 3, 2, 1], x=collect(160:5:190))
npp(collect(1:21), plt=false)