For example, I recently needed to simulate 1.5 million independent draws from a Type 1 Extreme Value distribution, which is known on Wikipedia as the Gumbel distribution. I tried using rgumbel(), but no such function exists in the standard packages. I also conducted a quick search of R help files... to no avail.

What's an R user to do? Write a function!

Fortunately, I remembered this really handy theorem that transforming any continuous random variable by its CDF F() always produces a random variable that is uniformly distributed on the [0,1] interval. As a practical matter, this means that you can simulate n random draws from a distribution by:

- Computing n random draws from a Uniform distribution on [0,1]
- Applying the inverse function of the CDF to the uniformly draw random variables.

rt1ev = function(n){ ## Function that returns n

U = runif(n) ## independent draws from T1EV

t1ev = -log(-log(U))

t1ev

}

Because IO economists are fond of using the T1EV distribution and I am studying IO now, I can see myself using this function repeatedly.

Here's how it works. Suppose you want a vector x to have 100 draws from a T1EV distribution. After loading my code, all you need to do to use this function is type:

x = rt1ev(100)

Not only is this a useful function if you want to simulate from a Gumbel distribution, but it is a great illustration of why knowing how to write functions in R can make your life easier.

## No comments:

## Post a Comment