正規分布のグラフィクス

MINAKA Nobuhiro — Nov 4, 2013, 2:25 PM

## 正規分布に関連する関数(dnorm, pnorm, qnorm, rnorm)
## Copyright (c) 2004, 2013 by MINAKA Nobuhiro. All rights reserved. 

# —————————————————————————————————
# 平均0, 標準偏差0.8の正規分布の確率密度関数(dnorm)
# —————————————————————————————————
x <- seq(-3, 3, 0.05)
plot(x,dnorm(x, mean=0, sd=0.4), type="n", xlab="x", ylab="density")
curve(dnorm(x, mean=0, sd=0.8), type="l", add=T, main=paste("normal distribution:  mean=0, standard deviation=0.8"))

plot of chunk unnamed-chunk-1


# —————————————————————————————————
# 正規分布の確率分布関数(pnorm)とその逆関数(qnorm)
# —————————————————————————————————
x <- seq(-3, 3, 0.05)
plot(x,dnorm(x, mean=0, sd=0.4), type="n", xlab="x", ylab="density")
curve(dnorm(x, mean=0, sd=0.8), type="l", add=T, main=paste("normal distribution:  mean=0, standard deviation=0.8"))
curve(pnorm(x, mean=0, sd=0.8), type="l", lty=3, add=T)

plot of chunk unnamed-chunk-1


# —————————————————————————————————
#5%点の表示
# —————————————————————————————————
x <- seq(-3, 3, 0.05)
plot(x,dnorm(x, mean=0, sd=0.4), type="n", xlab="x", ylab="density")
curve(dnorm(x, mean=0, sd=0.8), type="l", add=T, main=paste("normal distribution:  mean=0, standard deviation=0.8"))
curve(pnorm(x, mean=0, sd=0.8), type="l", lty=3, add=T)

abline(h=0.05)
lower.alpha5 <- qnorm(0.05, mean=0, sd=0.8)
lower.alpha5
[1] -1.316
abline(v=lower.alpha5)
points(lower.alpha5, 0.05, cex=3.0, pch="*")

abline(h=0.95)
upper.alpha5 <- qnorm(0.05, mean=0, sd=0.8, lower.tail = FALSE)
upper.alpha5
[1] 1.316
abline(v=upper.alpha5)
points(upper.alpha5, 0.95, cex=3.0, pch="*")

plot of chunk unnamed-chunk-1


# —————————————————————————————————
# 1%点の表示
# —————————————————————————————————
x <- seq(-3, 3, 0.05)
plot(x,dnorm(x, mean=0, sd=0.4), type="n", xlab="x", ylab="density")
curve(dnorm(x, mean=0, sd=0.8), type="l", add=T, main=paste("normal distribution:  mean=0, standard deviation=0.8"))
curve(pnorm(x, mean=0, sd=0.8), type="l", lty=3, add=T)

abline(h=0.05)
abline(v=lower.alpha5)
points(lower.alpha5, 0.05, cex=3.0, pch="*")

abline(h=0.95)
abline(v=upper.alpha5)
points(upper.alpha5, 0.95, cex=3.0, pch="*")

abline(h=0.01, lty=2)
lower.alpha1 <- qnorm(0.01, mean=0, sd=0.8)
lower.alpha1
[1] -1.861
abline(v=lower.alpha1, lty=2)
points(lower.alpha1, 0.01, cex=3.0, pch="*")

abline(h=0.99, lty=2)
upper.alpha1 <- qnorm(0.01, mean=0, sd=0.8, lower.tail = FALSE)
upper.alpha1
[1] 1.861
abline(v=upper.alpha1, lty=2)
points(upper.alpha1, 0.99, cex=3.0, pch="*")

plot of chunk unnamed-chunk-1


# —————————————————————————————————
# 正規乱数(rnorm)の生成とヒストグラム表示
# —————————————————————————————————
random.norm <- rnorm(10, mean=0, sd=0.8)
hist(random.norm, freq=F)

plot of chunk unnamed-chunk-1


random.norm <- rnorm(1000, mean=0, sd=0.8)
hist(random.norm, freq=F)

plot of chunk unnamed-chunk-1


random.norm <- rnorm(1000000, mean=0, sd=0.8)
hist(random.norm, freq=F)
curve(dnorm(x, mean=0, sd=0.8), add=T)

plot of chunk unnamed-chunk-1


# —————————————————————————————————
# 正規分布のパラメーター(1)――平均μを変える
# —————————————————————————————————
x <- seq(-4, 4, 0.01)
plot(x, dnorm(x, mean=0, sd=0.5), type="n", xlab="x", ylab="density")
title("Normal Distribution\nmean=0 -> 2.0")
for (i in 1:5) curve(dnorm(x, mean=0.5*(i-1), sd=0.5), type="l", lty=i, add=T)

plot of chunk unnamed-chunk-1


# —————————————————————————————————
#正規分布のパラメーター(2)――分散σ^2を変える
# —————————————————————————————————
x <- seq(-4, 4, 0.01)
plot(x, dnorm(x, mean=0, sd=0.5), type="n", xlab="x", ylab="density")
title("Normal Distribution\nsd=0.5 -> 2.5")
for (i in 1:5) curve(dnorm(x, mean=0, sd=0.5*i), type="l", lty=i, add=T)

plot of chunk unnamed-chunk-1


# —————————————————————————————————
# 標準正規分布(平均0,分散1)への線形変換
# —————————————————————————————————
mean1 <- 1.0
sd2 <- 2.0
plot(x, dnorm(x, mean=0, sd=1), type="n", xlab="x", ylab="density")
x <- rnorm(10000, mean=mean1, sd=sd2)
hist(x, freq=F, density=25, angle=135, add=T)
curve(dnorm(x, mean=mean1, sd=sd2), type="l", lty=2, lwd=2, add=T)

hist((x - mean1)/sd2, freq=F, density=25, angle=45, add=T)
curve(dnorm(x, mean=0, sd=1), type="l", lty=1, lwd=2, add=T)

plot of chunk unnamed-chunk-1


## Original Script: 8 December 2004 by MINAKA Nobuhiro
## Last Modified: 4 November 2013 by MINAKA Nobuhiro