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 -- Der Inhalt dieser E-Mail ist vertraulich. Sollte Ihnen die E-Mail irrt?mlich zugesandt worden sein, bitte ich Sie, mich unverz?glich zu benachrichtigen und die E-Mail zu l?schen. This e-mail is confidential. If you have received it in error, please notify me immediately and delete it from your system.
There are several non-normal distributions built in that you could just use their generationg functions. There are packages (most with dist somewhere in the name) that allow for functions on standard distributions including combining together distributions. If your distribution is not one of the above, but has a well defined inverse cumulative distribution then you can just generate random uniforms (runif) and put them into the inverse. If none of the above works then there are still many tools available such as rejection sampling or metropolis-hastings sampling. The logspline package can approximate a distribution from a dataset and generate values from the approximation. We would need to know more about the actual distribution that you are interested in to be of more specific help. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Anna Lee > Sent: Thursday, March 31, 2011 1:24 PM > To: r-help at r-project.org > Subject: [R] generate random numbers > > 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 > > -- > > > > Der Inhalt dieser E-Mail ist vertraulich. Sollte Ihnen die E-Mail > irrt?mlich zugesandt worden sein, bitte ich Sie, mich unverz?glich zu > benachrichtigen und die E-Mail zu l?schen. > > This e-mail is confidential. If you have received it in error, please > notify me immediately and delete it from your system. > > ______________________________________________ > 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.
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! > AnnaSUppose 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 ------------------------------
On 01/04/11 08:50, Ted Harding wrote:> 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. >I can point to one general implementation which might be helpful, and even the function names are the same. In the version of DistributionUtils on R-Forge you will find functions pDist and qDist which should give the distribution function and quantile function of any continuous unimodal distribution. Provisos: there may be problems with distributions with very heavy tails, and generally the routines could be slow. David Scott -- _________________________________________________________________ David Scott Department of Statistics The University of Auckland, PB 92019 Auckland 1142, NEW ZEALAND Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055 Email: d.scott at auckland.ac.nz, Fax: +64 9 373 7018 Director of Consulting, Department of Statistics