Simulation

Distributions in R

  1. Density function (p.d.f.): dDistribution(x,...)
  2. Probability function (c.d.f.): pDistribution(q,...)
  3. Quantile function (inverse c.d.f.): qDistribution(p,...)
  4. Random function (random variate generator): rDistribution(n,...)

where

Note that

Distributions in package:stats
Distribution Density Probability Quantile Random
beta
binom
birthday × ×
cauchy
chisq
exp
f
gamma
geom
hyper
lnorm
logis
multinom × ×
nbinom
norm
pois
signrank
t
tukey × ×
unif
weibull
wilcox

Many add-on packages have other distributions available, for instance, package mvtnorm has multivariate normal distribution and multivariate t distribution. Users can write their own distribution functions and are recommended to comply with the above convention (using initials d, p, q, and r for function names; and using x, p, q, and n as the first argument).

Sampling

sample(x, size, replace = FALSE, prob = NULL)
where

Examples

  1. Random permutation: sample(x)
  2. Bootstrap sample: bx <- sample(x, replace=T)
  3. Double bootstrap sample: with bx above, sample(bx, replace=T) gives a double bootstrap sample
  4. Sampling with unequal probabilities: sample(0:2,20,T,c(.2,.5,.3))
  5. Bootstrap multivariate data (assume data matrix X): X[sample(nrow(X),rep=T),]

2008-09-26