Chapter 8. Probability

Probability theory is the foundation of statistics, and R has plenty of machinery for working with probability, probability distributions, and random variables. The recipes in this chapter show you how to calculate probabilities from quantiles, calculate quantiles from probabilities, generate random variables drawn from distributions, plot distributions, and so forth.

Names of Distributions

R has an abbreviated name for every probability distribution. This name is used to identify the functions associated with the distribution. For example, the name of the normal distribution is “norm,” which is the root of the function names listed in Table 8-1.

Table 8-1. Normal distribution functions
Function Purpose

dnorm

Normal density

pnorm

Normal distribution function

qnorm

Normal quantile function

rnorm

Normal random variates

Table 8-2 describes some common discrete distributions, and Table 8-3 describes several common continuous distributions.

Table 8-2. Common discrete distributions
Discrete distribution R name Parameters

Binomial

binom

n = number of trials; p = probability of success for one trial

Geometric

geom

p = probability of success for one trial

Hypergeometric

hyper

m = number of white balls in urn; n = number of black balls in urn; k = number of balls drawn from urn

Negative binomial (NegBinomial)

nbinom

size = number of successful trials; either prob = probability of successful trial or mu = mean

Poisson

pois

lambda = mean ...

Get R Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.