norm是正态分布,前面加r表示生成随机正态分布的序列,其中rnorm(10)表示产生10个数;给定正太分布的均值和方差,
Density(d), distribution function§, quantile function(q) and random® generation for the normal distribution with mean equal to mean and standard deviation equal to sd.
- rnorm生成随机正态分布序列
- pnorm可以输出正态分布的分布函数
- dnorm可以输出正态分布的概率密度
- qnorm给定分位数的正太分布
使用格式如下:
dnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)
x, q
分位数向量vector of quantiles.
p
概率向量vector of probabilities.
n
表示产生几个数,length(n) > 1
mean
向量均值 vector of means
sd
向量的标准变异
vector of standard deviations
log, log.p
逻辑值 logical; 为真时概率取对数 if TRUE, probabilities p are given as log§.
lower.tail
逻辑值logical; 为真取小部分概率 if TRUE (default), probabilities are P[X ≤ x] otherwise, P[X > x].
如果没有设置mean和sd的话,他们的默认值分别为0和1
还有其他随机产生方式runif,rgamma:其总体随机数符合分别符合均匀分布uniform,gamma分布,而不是正态分布
require(graphics)
# 概率密度计算公式和原理
dnorm(0) == 1/sqrt(2*pi)
dnorm(1) == exp(-1/2)/sqrt(2*pi)
dnorm(1) == 1/sqrt(2*pi*exp(1))
# 绘制概率密度曲线
## Using "log = TRUE" for an extended range :
par(mfrow = c(2,1))
plot(function(x) dnorm(x, log = TRUE), -60, 50,
main = "log { Normal density }")
curve(log(dnorm(x)), add = TRUE, col = "red", lwd = 2)
mtext("dnorm(x, log=TRUE)", adj = 0)
mtext("log(dnorm(x))", col = "red", adj = 1)
# 绘制分布函数
plot(function(x) pnorm(x, log.p = TRUE), -50, 10,
main = "log { Normal Cumulative }")
curve(log(pnorm(x)), add = TRUE, col = "red", lwd = 2)
mtext("pnorm(x, log=TRUE)", adj = 0)
mtext("log(pnorm(x))", col = "red", adj = 1)
## if you want the so-called 'error function'
erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1
## (see Abramowitz and Stegun 29.2.29)
## and the so-called 'complementary error function'
erfc <- function(x) 2 * pnorm(x * sqrt(2), lower = FALSE)
## and the inverses
erfinv <- function (x) qnorm((1 + x)/2)/sqrt(2)
erfcinv <- function (x) qnorm(x/2, lower = FALSE)/sqrt(2)
参考资料:
R中help(rnorm)