R language - the difference between dnorm-pnorm-qnorm-rnorm

The difference between dnorm, pnorm, qnorm and rnorm in R language

foreword

dnorm, pnorm, qnorm, rnorm are normal distribution functions commonly used in R language. Norm refers to normal distribution (also called Gaussian distribution ( normal distribution )), and there are other different distribution operations in R language that are similar. pqdr here refers to different functions respectively. The following will introduce in detail the application of these different functions in normal distribution and how this command is used in R.

dnorm

d - refers to the probability density function

Equivalent to the dependent variable:
f ( x ∣ µ , σ ) = 1 2 π e − x 2 2 f(x|\mu, \sigma)=\frac{1}{\sqrt{2\pi}}e^ {-\frac{x^{2}}{2}}f(xμ,s )=2 p.m 1e2x2

dnorm is essentially the value of the probability density function of the normal distribution. In human terms, it returns the value of the above function. Let's demonstrate it in the code below:

# 输出在标准正态分布下(mean = 0, standard deviation = 1) 0 的z-sore
dnorm(0, mean=0, sd=1) # 0.3989423
# 因为是标准正态分布所以mean和sd是可以省略的
dnorm(0) # 0.3989423
# 如果是一个非标准正态分布如下:
dnorm(2, mean=5, sd=3) # 0.08065691

pnorm

p - refers to the probability density integral function (integral from infinitesimal to x) (Probability density integral function)

x refers to a z-score. The technical term sounds fantasy, but it is actually the area (probability ratio) to the left of x under the normal distribution curve. We know which score the z-score is calculated on

# 标准正态分布
pnorm(0) # 0.5 (50%)
pnorm(2) # 0.9772499
# 非标准正态分布
pnorm(2, mean=5, sd=3) # 0.1586553
# 也可以求x右边的概率
pnorm(2, mean=5, sd=3, lower.tail=FALSE) # 0.81586553
# pnorm也能用来求置信区间
pnorm(3) - pnorm(1) # 0.1573054

The above figure can be written in R as follows

pnorm(2) # 0.9772499

qnorm

q - refers to the quantile function

Simply put, it is the inverse function of pnorm. Calculate z-score by percentage. I know that quantiles can be used to calculate z-score, for example:

# 在标准正态分布中求z-score
qnorm(0.5) # 0
qnorm(0.96) # 1.750686
qnorm(0.99) # 2.326348

rnorm

r - refers to the random function (random function) (often used in probabilistic simulations)

It is used to generate a set of random numbers conforming to the normal distribution, for example:

# 设置随机数种子
set.seed(1)
# 生成5个符合标准正态分布的随机数
rnorm(5) # -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078
# 生成10个mean=70, sd=5的正态分布随机数
rnorm(10, mean=70, sd=5) # 65.89766 72.43715 73.69162 72.87891 68.47306 77.55891 71.94922 66.89380 58.92650 75.62465

The various other distributions generated in R language also start with d, p, q, r, the principle is similar to the normal distribution

references

http://www.360doc.com/content/18/0913/18/19913717_786412696.shtml

https://www.runoob.com/r/r-basic-operators.html

Guess you like

Origin blog.csdn.net/Kevin_Carpricron/article/details/124069960