在來(lái)自獨(dú)立源的數(shù)據(jù)的隨機(jī)集合中,通常觀察到數(shù)據(jù)的分布是正常的。 這意味著,在繪制水平軸上的變量值和垂直軸上的值的計(jì)數(shù)的圖形時(shí),我們得到鐘形曲線(xiàn)。 曲線(xiàn)的中心表示數(shù)據(jù)集的平均值。 在圖中,50%的值位于平均值的左側(cè),另外50%位于圖表的右側(cè)。 這在統(tǒng)計(jì)學(xué)中被稱(chēng)為正態(tài)分布。
R語(yǔ)言有四個(gè)內(nèi)置函數(shù)來(lái)產(chǎn)生正態(tài)分布。 它們描述如下。
dnorm(x, mean, sd)
pnorm(x, mean, sd)
qnorm(p, mean, sd)
rnorm(n, mean, sd)
以下是在上述功能中使用的參數(shù)的描述 -
該函數(shù)給出給定平均值和標(biāo)準(zhǔn)偏差在每個(gè)點(diǎn)的概率分布的高度。
# 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()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
該函數(shù)給出正態(tài)分布隨機(jī)數(shù)的概率小于給定數(shù)的值。 它也被稱(chēng)為“累積分布函數(shù)”。
# 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()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
該函數(shù)采用概率值,并給出累積值與概率值匹配的數(shù)字。
# 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 = 3)
# Give the chart file a name.
png(file = "qnorm.png")
# Plot the graph.
plot(x,y)
# Save the file.
dev.off()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
此函數(shù)用于生成分布正常的隨機(jī)數(shù)。 它將樣本大小作為輸入,并生成許多隨機(jī)數(shù)。 我們繪制一個(gè)直方圖來(lái)顯示生成的數(shù)字的分布。
# 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()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
更多建議: