[R] Help!!

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jan 29 16:41:04 CET 2003

```Duncan Murdoch <murdoch at stats.uwo.ca> writes:

> On Tue, 28 Jan 2003 16:01:06 +0000, you wrote in message
> <F171eRaNgEpTX5V3Gyk00000356 at hotmail.com>:
>
> >
> >Dear R ers, if some can tel me how I can generate a sample from a given
> >density. I have a complex  2D density function en I want to genearte
> >a sample from it? Any package?
>
> That's generally a hard problem, and the answer depends a lot on the
> particular characteristics of your distribution.  The easiest general
> purpose method if you can calculate the density is probably MCMC, in
> particular random walk Metropolis; see the book Markov Chain Monte
> Carlo in Practice for details.  This will give you a Markov chain that
> (hopefully) converges to your target distribution.  If you only want a
> small sample, it's quite inefficient, but for a large sample (e.g. for
> estimating moments) it can be quite good.
>
> Support in R for MCMC is pretty much non-existent, but it's easy to
> write the chains yourself.

MCMC has big difficulty in generating *independent* samples. An
alternative might be rejection methods. This essentially requires that
you can find an "easy" density that can be scaled to be a majorant for
the target density (which it might take a bit of calculus to achieve).
Lets call the majorant g. Then draw X at random from this distribution
(with density proportional to g) and an additional uniform variable U
and return X if U < f(X)/g(X), else reject X and retry.

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

```