正規分布から10の数字を生成
rnorm(n = 10)
## [1] 0.8671481 0.9339415 0.0449156 -0.7248498 1.9200008 0.4115597
## [7] -0.8773931 -0.5340204 -0.3173870 -1.6146635
正規分布から平均100、標準偏差が20の10個の数字を生成
rnorm(n = 10, mean = 100, sd = 20)
## [1] 131.45904 92.83415 146.04697 106.63027 101.00036 91.38774 117.70816
## [8] 110.20902 81.02325 88.73396
正規分布の確率密度(特定の値の確率)を計算する場合は、dnorm関数を利用する
randNorm10 <- rnorm(10)
randNorm10
## [1] 0.29652936 -1.19249285 0.60470460 0.29389700 0.16226688
## [6] -1.49646527 -0.74674176 1.98382586 1.45477979 -0.09921651
dnorm(randNorm10)
## [1] 0.38178282 0.19593778 0.33228164 0.38207962 0.39372452 0.13020532
## [7] 0.30187261 0.05575874 0.13846597 0.39698353
dnorm(c(-1, 0 , 1))
## [1] 0.2419707 0.3989423 0.2419707
作図
# 正規分布からデータを生成
randNorm <- rnorm(30000)
# 分布を計算
randDensity <- dnorm(randNorm)
# ggplot2をロード
require(ggplot2)
## Loading required package: ggplot2
# 図を作成
ggplot(data.frame(x = randNorm, y = randDensity)) + aes(x = x, y = y) + geom_point() + labs(x = "Random Normal Variables", y = "Density")
