# R语言：生成正态分布数据生成--rnorm,dnorm,pnorm,qnorm

norm是正态分布，前面加r表示生成随机正态分布的序列，其中rnorm(10)表示产生10个数；给定正太分布的均值和方差，

Density(d), distribution function(p), quantile function(q) and random(r) 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

p

n

mean

sd

vector of standard deviations

log, log.p

lower.tail

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

## 写在后面

https://mp.weixin.qq.com/s/5jQspEvH5_4Xmart22gjMA

http://blog.sciencenet.cn/blog-3334560-1150595.html

## 全部精选博文导读

GMT+8, 2019-3-20 23:26