在隨機收集來自獨立來源的數(shù)據(jù)中,通常觀察到數(shù)據(jù)的分布是正常的。 這意味著,在繪制水平軸上的變量的值和垂直軸中的值的計數(shù)時,我們得到一個鐘形曲線。 曲線的中心代表數(shù)據(jù)集的平均值。 在圖中,百分之五十的值位于平均值的左側,另外五十分之一位于圖的右側。 統(tǒng)稱為正態(tài)分布。
R有四個內(nèi)置函數(shù)來生成正態(tài)分布。它們在下面描述 -
dnorm(x, mean, sd)
pnorm(x, mean, sd)
qnorm(p, mean, sd)
rnorm(n, mean, sd)
以下是上述函數(shù)中使用的參數(shù)的描述 -
1。該函數(shù)給出給定平均值和標準偏差在每個點的概率分布的高度。
setwd("F:/worksp/R")
# 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()
當我們執(zhí)行上述代碼時,會產(chǎn)生以下結果 -

該函數(shù)給出正態(tài)分布隨機數(shù)小于給定數(shù)值的概率。它也被稱為“累積分布函數(shù)”。
setwd("F:/worksp/R")
# 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()
當我們執(zhí)行上述代碼時,會產(chǎn)生以下結果 -

該函數(shù)采用概率值,并給出其累積值與概率值匹配的數(shù)字值。
setwd("F:/worksp/R")
# 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()
當我們執(zhí)行上述代碼時,會產(chǎn)生以下結果 -

該函數(shù)用于生成分布正常的隨機數(shù),它將樣本大小作為輸入,并生成許多隨機數(shù)。我們繪制直方圖以顯示生成數(shù)字的分布。
setwd("F:/worksp/R")
# 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 = "正態(tài)分布")
# Save the file.
dev.off()
當我們執(zhí)行上述代碼時,會產(chǎn)生以下結果 -
