### 正規分布のグラフィクス

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

``````## 正規分布に関連する関数（dnorm, pnorm, qnorm, rnorm）

# —————————————————————————————————
# 平均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"))
``````

``````
# —————————————————————————————————
# 正規分布の確率分布関数（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)
``````

``````
# —————————————————————————————————
#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="*")
``````

``````
# —————————————————————————————————
# 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="*")
``````

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

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

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

``````
# —————————————————————————————————
# 正規分布のパラメーター（１）――平均μを変える
# —————————————————————————————————
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)
``````

``````
# —————————————————————————————————
#正規分布のパラメーター（２）――分散σ^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)
``````

``````
# —————————————————————————————————
# 標準正規分布（平均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)
``````