Re: [R] generate random numbers

From: Ted Harding <ted.harding_at_wlandres.net>
Date: Thu, 31 Mar 2011 20:50:56 +0100 (BST)


On 31-Mar-11 19:23:33, Anna Lee wrote:
> Hey List,
> does anyone know how I can generate a vector of random numbers
> from a given distribution? Something like "rnorm" just for non
> normal distributions???
>
> Thanks a lot!
> Anna

SUppose we give your distribution the name "Dist".

The generic approach would start by defining a function for the inverse of its cumulative distribution. Call this qDist. Then

  qDist(runif(1000))

would generate 1000 values from the distribution "Dist".

As a ready-made example, qnorm is the inverse of pnorm, the cumulative distribution function of the Normal distribution. Then

  qnorm(runif(1000))

would act just like rnorm(1000), though the sequence of values would be different (a different algorithm) -- and also rnorm() would be more efficient (being specially written).

Depending on what your desired distribution is, you may find that an "rDist" has already been written for it. There are many distributions already in R for which the family of functions dDist, pDist, qDist and rDist are provided.

For more specific advice, please give us information about the specific distribution you want to sample from!

Ted.



E-Mail: (Ted Harding) <ted.harding_at_wlandres.net> Fax-to-email: +44 (0)870 094 0861
Date: 31-Mar-11                                       Time: 20:50:52
------------------------------ XFMail ------------------------------

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Mar 2011 - 19:53:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Mar 2011 - 22:50:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive