R dnorm() pnorm() qnorm() functions - Normal distribution function family

R 语言实例R Language Examples

R provides four core functions for the normal distribution: dnorm() density function, pnorm() cumulative probability, qnorm() quantile function, rnorm() random number generation.

They each answer different questions: What is the probability density of a certain value? What is the cumulative probability? What is the quantile corresponding to a certain probability?

The syntax of each function is as follows:

dnorm(x, mean = 0, sd = 1)   # 密度函数
pnorm(q, mean = 0, sd = 1)   # 累积分布函数 P(X <= q)
qnorm(p, mean = 0, sd = 1)   # 分位数函数

Parameter description:

  • x/qThe value to be calculated.

  • pProbability value (0 to 1).

  • meanMean, default 0.

  • sdStandard deviation, default 1.

Example

# Assume test scores N(70, 10)

# dnorm: probability density at score 70
print(paste("dnorm(70):", round(dnorm(70, mean = 70, sd = 10), 4)))

# pnorm: probability of score below 85
print(paste("P(score <= 85):", round(pnorm(85, mean = 70, sd = 10), 4)))

# pnorm: probability of score above 85
print(paste("P(score > 85):", round(1 - pnorm(85, mean = 70, sd = 10), 4)))

# qnorm: cutoff for top 10%
print(paste(Top 10% cutoff score:, round(qnorm(0.9, mean = 70, sd = 10), 2)))

Executing the above code outputs the following:

[1] "dnorm(70): 0.0399"
[1] "P(score <= 85): 0.9332"
[1] "P(score > 85): 0.0668"
[1] "前 10% 分数线: 82.82"

R 语言实例R Language Examples

Other extensions