Home page for accesible maths

Style control - access keys in brackets

Font (2 3) - + Letter spacing (4 5) - + Word spacing (6 7) - + Line spacing (8 9) - +

Appendix A R Commands for Distributions

Many probability calculations are too complicated to find exact answers e.g. values of the standard Normal cdf Φ(z). Instead, numerical approximations are used. The most accurate and convenient way to do this is by computer, and this chapter details the commands used for this purpose in R.

Historically, numerical values were supplied to users as tables. These are little used nowadays, but one important exception is in exams, where you will be told the values needed for the questions. These will be in the form of R output (see past papers for examples e.g. B3 in the 2011 exam), so it will be important to understand the basics of the R commands below.

Function groups

There are four groups of R functions for probability distributions, represented by four letters which start the function names:

  • p for probability giving the cdf FX(x) (as it represents the probability 𝖯(Xx).)

  • q for quantile giving the inverse cdf FX-1(x).

  • d for density giving the pdf (or pmf for discrete random variables.)

  • r for random giving random numbers from this distribution.

Shorthand names

To complete the function name, a shorthand version of the distribution name is added. For the Normal distribution, for example, this is norm, giving functions pnorm, qnorm, dnorm and rnorm.

Each group of functions operates in a similar way for any distribution, and will explained in turn below.

The following table lists the shorthand names of many common distributions implemented by R. It also gives the parameter names used in R, which will be needed shortly, and the corresponding parameter notation from earlier in the notes.

Distribution Shorthand Parameter 1 Parameter 2
Bernoulli Use Binomial with size 1
Discrete Binomial binom size n prob θ
Geometric geom prob θ
Poisson pois lambda λ
Uniform unif min a max b
Exponential exp rate β
Gamma gamma shape α rate β
Normal norm mean μ sd σ
Beta beta shape1 α1 shape2 α2
Cauchy cauchy
Continuous Weibull weibull shape α scale β
Chi-squared chisq df λ
F f df1 λ1 df2 λ2
Log-normal lnorm meanlog ξ sdlog σ
Student’s t t df λ
(with μ=0)

Probability functions

For a probability function such as pnorm, you must specify a value of x for which FX(x) is required, and also the parameters of the distribution, as given in the table.

Example 1.0.1.

Suppose XN(10,52). Find 𝖯(X<1) and 𝖯(X>5).

pnorm(1,mean=10,sd=5)
1-pnorm(5,mean=10,sd=5)

To save time you can leave out the names of the parameters.

pnorm(1,10,5)
1-pnorm(5,10,5)

But this can be dangerous! For example, it is easy to give the variance 52 instead of the standard deviation 5.

Quantile functions

For a quantile function such as qnorm, you must specify a value of x between 0 and 1 for which FX-1(x) is required, and also the parameters of the distribution.

Example 1.0.2.

Suppose XN(0,32) and Y𝖤𝗑𝗉(2). Find Fx-1(1/3) and the median of Y.

qnorm(1/3,mean=0,sd=3)
qexp(0.5,rate=2)

Density functions

For a density function such as dnorm, you must specify the value x for which the density fX(x) is required and also the parameters of the distribution. For a discrete distribution, the mass function pX(x) is found instead.

Example

Example 1.0.3.

Suppose XN(9,12) and Y𝖦𝖾𝗈𝗆(0.9). Find fX(11) and 𝖯(Y=0).

dnorm(11,mean=9,sd=1) dgeom(0,prob=0.9)

Random functions

For a random function such as rnorm, you must specify how many random numbers you want, n, and also the parameters of the distribution.

Example 1.0.4.

Suppose XN(0,22). From distribution X generate

  1. (a)

    a single random number,

  2. (b)

    the mean of 1000 independent random numbers.

rnorm(1,mean=0,sd=2)
x=rnorm(1000,mean=0,sd=2)
mean(x)

Advanced tricks

As noted above, to save time you can leave out the names of the parameters. Instead the parameters are entered without names in the order given in the table.

You can also leave out parameters entirely. Default values are used instead which usually correspond to the standard version of the distribution.

Example 1.0.5.

Suppose Z is a standard Normal random variable i.e. ZN(0,1). Find 𝖯(Z<2) and the lower quartile of Z.

pnorm(2)
pnorm(2,mean=0,sd=1) ##Same answer
qnorm(0.25)
qnorm(0.25,mean=0,sd=1) ##Same answer

To find the default values of arguments, and other information about these functions, use the R help system. For example ?dnorm shows a help page including the line

dnorm(x, mean = 0, sd = 1, log = FALSE)

This tells you that the default values are mean zero and standard deviation one, corresponding to the standard normal N(0,1).

Another way to save time is to use the fact that the distribution functions allow their arguments to be vectors.

Example 1.0.6.
  1. (a)

    Suppose X𝖡𝖾𝗍𝖺(2,2). Find all quartiles.

  2. (b)

    Generate random numbers from XiN(2i,12) for 1i10.

qbeta(0:4/4,2,2)
rnorm(10,2*1:10,1)