O'Reilly logo

R Cookbook by Paul Teetor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Probability

Introduction

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 these function names:

FunctionPurpose
dnormNormal density
pnormNormal distribution function
qnormNormal quantile function
rnormNormal random variates

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

Table 8-1. Discrete distributions

Discrete distributionR nameParameters
Binomialbinomn = number of trials; p = probability of success for one trial
Geometricgeomp = probability of success for one trial
Hypergeometrichyperm = number of white balls in urn; n = number of black balls in urn; k = number of balls drawn from urn
Negative binomial (NegBinomial)nbinomsize = number of successful trials; either prob = probability of successful trial or mu = mean
Poissonpoislambda = mean

Table 8-2. Continuous distributions

Continuous distributionR nameParameters
Betabetashape1; shape2
Cauchycauchylocation; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required