There is a function in R called combn()
, but it doesn't calculate the number of combinations. Rather, it shows all the different possible combinations. To count the number of combinations when choosing k objects from a set of n objects we can use the choose()
function. There is no function in base R for calculating the number of permutations. However, permutations can be calculated manually using the factorial()
function. The union and intersection of two sets can be calculated by using the union()
and intersect()
functions, respectively.
> combn(x, m) # List of Combinations
> choose(n, k) # Number of Combinations
> choose(n, k) * factorial(k) # Number of Permutations
> union(x, y)
> intersect(x, y)
There are several different functions for calculations related to the binomial probability distribution. The function dbinom()
gives you the probability of a binomial random variable taking a specific value. There are three arguments in this function: the value, x, the number of trials, size, and the probability of success, prob. The function pbinom()
is similar to the previous function, except that it gives you teh cumulative probability. That is, it gives you the probability of getting x or fewer successes. The function qbinom()
is the inverse of the pbinom() function. So it gives you the value corresonding to the given cumulative probability. The rbinom()
function can be used to generate random values from a binomial distribution.
> dbinom(x, size, prob) # Probability of x Successes
> pbinom(x, size, prob) # Cumulative Probability
> pbinom(x, size, prob, lower.tail = FALSE) # Cumulative Probability (Upper Tail)
> qbinom(q, size, prob) # Inverse Cumulative Probability
> rbinom(r, size, prob) # Generate Random Numbers
The functions for the Poisson distribution, and all probability distributions in R, are similar to the ones for the binomial distribution. However, the arguments are going to be different. Instead of the arguments "size" and "prob", there is one argument for the poisson distribution in R, "lambda". When using the Poisson distribution, we are interested in calculating the number of occurences in a period of time or space. The argument "lambda" is the average number of occurences. The function dpois()
will give you the probability of having x number of occurences. The function ppois()
calculates the probability of having x or fewer occurences. Setting the argument lower.tail equal to FALSE gives you the probability of x or more occurences.
> dpois(x, lambda) # Probability of x
> ppois(x, lambda) # Cumulative Probability
> ppois(x, lambda, lower.tail = FALSE) # Cumulative Probability (Upper Tail)
> qpois(q, lambda) # Inverse Cumulative Probability
> rpois(r, lambda) # Generate Random Numbers
The functions for the uniform distribution all have the term "unif" in them. In each of the functions there are arguments for the minimum and maximum value, labeled "min" and "max". The dunif()
function gives you the value of the probability density function at the value x. Note that this is not the probability of x as this is a continuous probability distribution. The function punif()
gives you the cumuluative probability, or the probability of getting a value less than or equal to x. Setting the argument lower.tail equal to FALSE gives you the probability of getting a value greater than or equal to x. The function qunif()
gives you the value correspondinging to the given cumulative probability, q.
> dunif(x, min, max) # Probability of x Successes
> punif(x, min, max) # Cumulative Probability
> punif(x, min, max, lower.tail = FALSE) # Cumulative Probability (Upper Tail)
> qunif(q, min, max) # Inverse Cumulative Probability
> runif(r, min, max) # Generate Random Numbers
The functions for calculating probabilities for the normal distribution all have the term "norm" in them. The arguments "mean" and "sd" are the mean and standard deviation of the distribution. The default values for these arguments are 0 and 1, which correspond to the standard normal distribution. In other words, omitting these arguments means you're calculating probabilities for the standard normal distribution. The function dnorm()
calculates the values of the probability density function at x. Again, this is not the probability of getting a value of x. Rather, it is the height of the normal curve at x. Probabilities are calculated for a continuous distribution by taking the area under the curve, which can be done use the pnorm()
function.
> dnorm(x, mean, sd) # Probability of x Successes
> pnorm(x, mean, sd) # Cumulative Probability
> pnorm(x, mean, sd, lower.tail = FALSE) # Cumulative Probability (Upper Tail)
> qnorm(q, mean, sd) # Inverse Cumulative Probability
> rnorm(r, mean, sd) # Generate Random Numbers
The functions for the Poisson distribution, and all probability distributions in R, are similar to the ones for the binomial distribution. However, the arguments are going to be different. The function dpois()
will give you the probability
> dexp(x, rate) # Probability of x Successes
> pexp(x, rate) # Cumulative Probability
> pexp(x, rate, lower.tail = FALSE) # Cumulative Probability (Upper Tail)
> qexp(q, rate) # Inverse Cumulative Probability
> rexp(r, rate) # Generate Random Numbers