R Formula and Review Sheet
R Formula and Review Sheet
DISTRIBUTIONS
Functions related to distributions belong to the (automatically loaded) stats package and have the form:
d<distribution>(x, <parameters>) returns the value of the PDF (f (x) or P (X = x)
p<distribution>(x, <parameters>) returns the value of the CDF (F (x)) or P (X ≤ x))
p<distribution>(x, <parameters>, lower=FALSE) returns 1 − F (x) or P (X > x)
q<distribution>(p, <parameters>) returns the min value of x for which P (X ≤ x) ≥ p (p-th quantile)
q<distribution>(p, <parameters>, lower=FALSE) returns the min value of x for which P (X > x) ≥ p
r<distribution>(n, <parameters>) returns an iid random sample of size n from the distribution
Continuous Distributions:
The parameters for Continuous Distributions are:
(a) Uniform Distribution (U (a, b)) unif(<>,a,b), dunif(<>, a, b), punif(<>, a, b), …
(b) Exponential Distribution (Exp(λ)) exp(<>, lambda)
(c) Gamma Distribution (Gamma(α, λ)) gamma(<>, alpha, lambda)
(d) Chi-square Distribution (χ2df ) chisq(<>,df)
(e) Beta Distribution (Beta(α, β)) beta(<>,alpha,beta)
(f) Normal Distribution (N (µ, σ )) 2
norm(<>,mu,sigma)
(g) Log-normal Distribution (Lognormal(µ, σ 2 )) lnorm(<>,mu,sigma)
(h) Standard Normal Distribution (N(0,1)) norm(<>)
(i) Student’s t-distribution (t(ν) ) t(<>, nu)
(j) F-Distribution (F (d1 , d2 )) f(<>, d1, d2)
Discrete Distributions:
The parameters for Discrete Distributions are:
(a) Binomial Distribution (Bin(n, p)) binom(<>, n, p)
(b) Poisson Distribution (P(λ)) pois(<>, lambda)
(c) Geometric Distribution (Geom(p)) geom(<>, p)
(d) Negative Binomial Distribution (NB(k, p)) nbinom(<>, k, p)
(e) Hypergeometric Distribution (HG(N, K, n)) hyper(<>, N, K, n)
(f) Uniform Distribution:
R does not provide built-in functions to deal with the discrete uniform distribution. Here are some alternatives:
1. Frequency Table:
Example: Frequency table for an iid sample X with Xi ∼ Bin(100, 0.2). The bottom row is the number of occurrences of
the corresponding value in the top row in the sample.
##
## 4 5 6 7 8 9 10 11 12 13 14 15 16 19
## 3 2 5 13 7 12 11 13 13 12 5 1 1 2
2. Bar chart:
Example: Bar chart for the PDF of Bin(50, 0.4):
x <- 0:50
barplot(dbinom(x, 50, 0.4), names = x)
0.06
0.00
0 4 8 13 18 23 28 33 38 43 48
3. Plots:
Example: Plot of the PDF of N (10, 22 ):
0.15
0.00
0 5 10 15 20 25 30
x−values