Counting: Factorials, Permutations and Combinations

factorial(x = 3)        # 3 times 2 times 1 = 6
## [1] 6
factorial(4)            # 24
## [1] 24
factorial(c(3, 4))      # 6 24
## [1]  6 24
choose(n = 6:9, k = 4:6)                  # 15  21  28 126; note the recycling of the k vector
## [1]  15  21  28 126
choose(n = 6, k = 4)                      # 15; 6 combination 4
## [1] 15
factorial(6)/(factorial(4)*factorial(2))  # 15; as expected
## [1] 15
# install.packages('gtools')
library(gtools)
combos <- combinations(n = 6, r = 4)  # Generates a matrix of all 4-number combinations of 1:6
nrow(combos)                  # 15; 6 combination 4
## [1] 15
permos <- permutations(n = 6, r = 4)  # Generates a matrix of all 4-number permutations of 1:6
nrow(permos)                  # 360; 6 permutation 4
## [1] 360
factorial(6)/factorial(6 - 4) # 360; 6 permutation 4
## [1] 360

Binomial Distribution

x <- seq(from = 0, to = 10, by = 1)     # Same as x <- seq(from = 0, to = 10, length.out = 11)
fx <- dbinom(x, size = 10, prob = 0.2)  # pmf of x successes from 10 trials with probability of success 0.2
plot(x, fx, type="h")             # Graph of the probability distribution

Note that the dbinom() probability mass function works on vectors of successes.

dbinom(0, 10, 0.2) + dbinom(1, 10, 0.2) + dbinom(2, 10, 0.2)    # 0.6777995; sum of PMF numbers
## [1] 0.6777995
pbinom(2, 10, 0.2)                                              # 0.6777995; CDF
## [1] 0.6777995
qbinom(pbinom(2, 10, 0.2), 10, 0.2)                             # 2; backing out the number of successes from the CDF
## [1] 2
rbinom(3, 10, 0.2)  # 3 random numbers from the Binomial distribution with 10 trials and 0.2 probability of success
## [1] 1 1 0

Normal Distribution

y <- seq(-5, 5, 1)
dnorm(y, mean = 0, sd = 1, log = FALSE)          # probability densities for the values in y
##  [1] 1.486720e-06 1.338302e-04 4.431848e-03 5.399097e-02 2.419707e-01
##  [6] 3.989423e-01 2.419707e-01 5.399097e-02 4.431848e-03 1.338302e-04
## [11] 1.486720e-06
pnorm(y, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)   # cumulative probability densities
##  [1] 2.866516e-07 3.167124e-05 1.349898e-03 2.275013e-02 1.586553e-01
##  [6] 5.000000e-01 8.413447e-01 9.772499e-01 9.986501e-01 9.999683e-01
## [11] 9.999997e-01
p <- pnorm(y, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)   # Should be y
##  [1] -5 -4 -3 -2 -1  0  1  2  3  4  5
rnorm(n = 10, mean = 0, sd = 1)   # 10 random numbers from a normal distribution with mean 0 and standard deviation 1
##  [1]  0.8007175  0.9216414  0.3065547  0.7403139 -1.4701575  2.8234975
##  [7] -0.9910256 -0.3234005 -0.7679784  1.5478907

To plot continuous distributions, use the curve() function.

curve(dnorm(x, mean = 0, sd = 1), -5, 5)
curve(dnorm(x, mean = 0, sd = 2), -5, 5, add = TRUE)
abline(v = 0)   # Add a vertical line at x = 0