R-normal-distribution
提供:Dev Guides
R-正規分布
独立したソースからのデータのランダムな収集では、通常、データの分布は正常であることが観察されています。 つまり、水平軸の変数の値と垂直軸の値のカウントを使用してグラフをプロットすると、釣鐘型の曲線が得られます。 曲線の中心は、データセットの平均を表します。 グラフでは、値の50%が平均の左側にあり、残りの50%がグラフの右側にあります。 これは、統計では正規分布と呼ばれます。
Rには正規分布を生成する組み込み関数が4つあります。 以下に説明します。
dnorm(x, mean, sd)
pnorm(x, mean, sd)
qnorm(p, mean, sd)
rnorm(n, mean, sd)
以下は、上記の機能で使用されるパラメータの説明です-
- x は数値のベクトルです。
- p は確率のベクトルです。
- n は観測数(サンプルサイズ)です。
- mean は、サンプルデータの平均値です。 デフォルト値はゼロです。
- sd は標準偏差です。 デフォルト値は1です。
dnorm()
この関数は、与えられた平均と標準偏差の各点での確率分布の高さを与えます。
# Create a sequence of numbers between -10 and 10 incrementing by 0.1.
x <- seq(-10, 10, by = .1)
# Choose the mean as 2.5 and standard deviation as 0.5.
y <- dnorm(x, mean = 2.5, sd = 0.5)
# Give the chart file a name.
png(file = "dnorm.png")
plot(x,y)
# Save the file.
dev.off()
上記のコードを実行すると、次の結果が生成されます-
pnorm()
この関数は、正規分布された乱数が特定の数値よりも小さくなる確率を与えます。 「累積分布関数」とも呼ばれます。
# Create a sequence of numbers between -10 and 10 incrementing by 0.2.
x <- seq(-10,10,by = .2)
# Choose the mean as 2.5 and standard deviation as 2.
y <- pnorm(x, mean = 2.5, sd = 2)
# Give the chart file a name.
png(file = "pnorm.png")
# Plot the graph.
plot(x,y)
# Save the file.
dev.off()
上記のコードを実行すると、次の結果が生成されます-
qnorm()
この関数は、確率値を受け取り、累積値が確率値と一致する数値を提供します。
# Create a sequence of probability values incrementing by 0.02.
x <- seq(0, 1, by = 0.02)
# Choose the mean as 2 and standard deviation as 3.
y <- qnorm(x, mean = 2, sd = 1)
# Give the chart file a name.
png(file = "qnorm.png")
# Plot the graph.
plot(x,y)
# Save the file.
dev.off()
上記のコードを実行すると、次の結果が生成されます-
rnorm()
この関数は、分布が正規の乱数を生成するために使用されます。 サンプルサイズを入力として受け取り、その数の乱数を生成します。 ヒストグラムを作成して、生成された数値の分布を示します。
# Create a sample of 50 numbers which are normally distributed.
y <- rnorm(50)
# Give the chart file a name.
png(file = "rnorm.png")
# Plot the histogram for this sample.
hist(y, main = "Normal DIstribution")
# Save the file.
dev.off()
上記のコードを実行すると、次の結果が生成されます-