Sampling
Often, we would be interested in creating a representative dataset, for some analysis or design of experiments, by sampling from a population. This is particularly the case for Bayesian inference, as we will see in the later chapters, where samples are drawn from posterior distribution for inference. Therefore, it would be useful to learn how to sample N points from some well-known distributions in this chapter.
Before we use any particular sampling methods, readers should note that R, like any other computer program, uses pseudo random number generators for sampling. It is useful to supply a starting seed number to get reproducible results. This can be done using the set.seed(n)
command with an integer n
as the seed.
Random uniform sampling from an interval
To generate n random numbers (numeric) that are uniformly distributed in the interval [a, b], one can use the runif()
function:
>runif(5,1,10) #generates 5 random numbers between 1 and 10 [1] 7.416 9.846 3.093 2.656...