Simulations are often used in advanced scientific modeling to make predictions and even to simulate the experimental design of an experiment. So this chapter will show you how to perform basic sample simulations and how to use simulations to answer statistical problems. You will also learn how to use R to generate random numbers, and how to simulate random variables from several common probability distributions. Then, we will show you how simulations can be used to evaluate integrals.
In this chapter, we will cover the following topics:
We already showed you the basics of generating random variables from common probability distributions in Chapter 2, Statistical Methods with R. For example, to simulate four numbers from a normal distribution with a mean of 10 and standard deviation of 3, we use the rnorm()
function with the mean
and sd
arguments set to 10
and 3
, respectively:
> rnorm(4, mean=10, sd=3) [1] 9.546714 8.600795 14.344557 11.669767
Similarly, we can simulate 10 numbers from a Poisson distribution with a lambda of 3 with rpois(10, lambda=3)
. A table showing the function names for the most common distributions is available in Chapter 2, Statistical Methods with R.
We can also select random variables from a predetermined vector using the sample()
function. This function allows you to randomly select a specified number of elements with the size
argument from a vector, with or without replacing the values taken with the replace
argument set to TRUE
or FALSE
, respectively. For example, if we wanted to obtain five random numbers from 1 to 100 without any duplicates, we would use the sample()
function with the replace
argument set to FALSE
. Let's take a look at the following example:
> sample(1:100, size=5, replace=FALSE) [1] 83 79 3 11 55
Now let's say that we wanted to simulate data from throwing a dice six times. We could use the sample()
function with size equal to 6, and set replace
to TRUE
because it is possible to draw the same number more than once, as follows:
> sample(1:6, size=6, replace=TRUE) [1] 6 2 1 4 5 2
We can also use the sample function with character vectors as we saw in Chapter 3, Linear Models.
> fruits <- c("apple", "orange", "strawberry", "lemon", "clementine") > sample(fruits, size=2, replace=T) [1] "orange" "apple"
3.147.59.198