- Statistical Distributions in R
April 13, 2017
ddist()
: gives the density of the distributionrdist()
: generates random numbers out of the distributionqdist()
: gives the quantile of the distributionpdist()
: gives the cumulative distribution function (CDF)dbinom()
str(dbinom) # binomial probability mass func
## function (x, size, prob, log = FALSE)
dbinom(5, 10, 0.5) # Pr[X = 5] = ?
## [1] 0.2460938
pbinom()
str(pbinom) # binomial CDF
## function (q, size, prob, lower.tail = TRUE, log.p = FALSE)
pbinom(5, 10, 0.5) # Pr[X <= 5] = ?
## [1] 0.6230469
qnorm()
(note that this is the inverse of pnorm()
)str(qbinom) # binomial quantile func
## function (p, size, prob, lower.tail = TRUE, log.p = FALSE)
qbinom(0.75, 10, 0.5) # get the value of ? s.t. Pr[X <= ?] = 0.75
## [1] 6
str(rbinom) # binomial random number generator
## function (n, size, prob)
rbinom(20, 10, 0.5) # 20 ind samples from binomial(10, 0.5)
## [1] 4 4 5 4 6 8 7 5 6 3 4 4 6 6 4 4 6 4 5 7
str(dnorm) # normal pdf
## function (x, mean = 0, sd = 1, log = FALSE)
dnorm(x = 0, mean = 0, sd = 1)
## [1] 0.3989423
str(pnorm) # normal CDF
## function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
pnorm(0, mean = 0, sd = 1) # Pr[X <= 0] = ?
## [1] 0.5
str(qnorm) # normal quantile func
## function (p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(0.975, mean = 0, sd = 1) # PR[X <= ?] = 0.975
## [1] 1.959964
str(rnorm) # generate random number from normal dist
## function (n, mean = 0, sd = 1)
rnorm(10, mean = 0, sd = 1)
## [1] 0.28732831 1.28870120 0.95492333 -1.05616106 0.71571889 ## [6] -0.06861534 0.25125574 1.13770718 0.31197057 1.73683798
Let's try plotting a normal curve (more on plotting later)
x <- seq(from = -3, to = 3, by = 0.05) y <- dnorm(x, mean = 0, sd = 1) plot(x, y, type = "l")