R語言----(標準正態&二項)分佈

標準分佈也即是:正態分佈。 在來自獨立源的數據的隨機集合中,通常觀察到數據的分佈是正態的。
在圖中,曲線的中心表示數據集的平均值。 50%的值位於平均值的左側,另外50%位於圖表的右側。

正態分佈

開始前先附一篇下述四個函數的統計學講解,個人感覺較爲詳細http://www.360doc.com/content/18/0913/18/19913717_786412696.shtml

R語言有四個內置函數來產生正態分佈:

dnorm(x, mean, sd)
pnorm(x, mean, sd)
qnorm(p, mean, sd)
rnorm(n, mean, sd)

以下是在上述功能中使用的參數的描述:

  • x是數字的向量。
  • p是概率的向量。
  • n是觀察的數量(樣本大小)。
  • mean是樣本數據的平均值。 它的默認值爲零。
  • sd是標準偏差。 它的默認值爲1。

dnorm()

該函數給出給定平均值和標準偏差在每個點的概率分佈的高度。
翻譯一下就是:返回值是正態分佈概率密度函數值,比如dnorm(z)則表示:標準正態分佈密度函數f(x)在x=z處的函數值。

輸入:

# 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()

該函數給出正態分佈隨機數的概率小於給定數的值。 它也被稱爲“累積分佈函數”。
翻譯一下就是:返回值是正態分佈的分佈函數值,比如pnorm(z)等價於P[X ≤ z]

輸入:

# 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()

該函數採用概率值,並給出累積值與概率值匹配的數字。
翻譯一下就是:qnorm是正態分佈累積分佈函數的反函數,也就是說它可以視爲pnorm的反函數,這裏的q指的是quantile,即分位數。
使用qnorm這個函數可以回答這個問題:正態分佈中的第p個分位數的Z-score是多少?

輸入:

# 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()

輸出:

在這裏插入圖片描述

二項分佈

二項分佈模型處理在一系列實驗中僅發現兩個可能結果的事件的成功概率。 例如,擲硬幣總是給出正面或反面(兩個結果)。

R語言有四個內置函數來生成二項分佈:

dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)

以下是所使用的參數的描述:

  • x是數字的向量。
  • p是概率向量。
  • n是觀察的數量。
  • size是試驗的數量。
  • prob是每個試驗成功的概率。

dbinom()

該函數給出每個點的概率密度分佈。翻譯一下就是:返回值是二項分佈概率密度函數值,比如dbinom(z)則表示:二項分佈密度函數f(x)在x=z處的函數值。

輸入:

# Create a sample of 50 numbers which are incremented by 1.
x <- seq(0,50,by = 1)

# Create the binomial distribution.
y <- dbinom(x,50,0.5)

# Give the chart file a name.
png(file = "dbinom.png")

# Plot the graph for this sample.
plot(x,y)

# Save the file.
dev.off()

輸出:
在這裏插入圖片描述

pbinom()

此函數給出事件的累積概率。 它是表示概率的單個值。
翻譯一下就是:返回值是二項分佈的分佈函數值,比如pbinom(z)等價於P[X ≤ z]

例:從一枚硬幣的51次投擲中,得到26個或更少的正面朝上的概率

輸入:

# Probability of getting 26 or less heads from a 51 tosses of a coin.
x <- pbinom(26,51,0.5)

print(x)

輸出:

[1] 0.610116

qbinom()

該函數採用概率值,並給出累積值與概率值匹配的數字。
翻譯一下就是:qbinom可以視爲pbinom的反函數,這裏的q指的是quantile,即分位數。
使用qbinom這個函數可以回答這個問題:二項分佈中的第p個分位數的Z-score是多少?

例:當一枚硬幣被擲51次時,有多少個硬幣正面朝上的概率爲0.25

輸入:

# How many heads will have a probability of 0.25 will come out when a coin is tossed 51 times.
x <- qbinom(0.25,51,1/2)

print(x)

輸出:

[1] 23

rbinom()

該函數從給定樣本產生給定概率的所需數量的二項分佈的隨機值。

輸入:

# Find 8 random values from a sample of 150 with probability of 0.4.
x <- rbinom(8,150,.4)

print(x)

輸出:

58 61 59 66 55 60 61 67
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章