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.
使用格式如下:
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)
为鼓励读者交流、快速解决科研困难,我们建立了“宏基因组”专业讨论群,目前己有国内外2400+ 一线科研人员加入。参与讨论,获得专业解答,欢迎分享此文至朋友圈,并扫码加主编好友带你入群,务必备注“姓名-单位-研究方向-职称/年级”。技术问题寻求帮助,首先阅读《如何优雅的提问》学习解决问题思路,仍末解决群内讨论,问题不私聊,帮助同行。
学习扩增子、宏基因组科研思路和分析实战,关注“宏基因组”
点击阅读原文,跳转最新文章目录阅读
https://mp.weixin.qq.com/s/5jQspEvH5_4Xmart22gjMA