Vijayan Padmanabhan
2011-Aug-04 16:03 UTC
[R] random value generation with added constraint.
Hi I am looking at generating a random dataset of say 100 values fitting in a normal distribution of a given mean and SD, I am aware of rnorm function. However i am trying to build into this function one added constraint that all the random value generated should also obey the constraint that they only take values between say X to X+25 How do i do this in R? Any help would be highly appreciated,. Thanks Vijayan Padmanabhan [[alternative HTML version deleted]]
On 04/08/2011 12:03 PM, Vijayan Padmanabhan wrote:> Hi > I am looking at generating a random dataset of say 100 values fitting in a > normal distribution of a given mean and SD, I am aware of rnorm > function. However i am trying to build into this function one added > constraint that all the random value generated should also obey the > constraint that they only take values between say X to X+25 > How do i do this in R?The easiest way is to use the inverse-CDF method to generate values. For example: mu <- 50 sd <- 10 X <- 30 lower <- pnorm(X, mean=mu, sd=sd) upper <- pnorm(X+25, mean=mu, sd=sd) U <- runif(1000, lower, upper) Y <- qnorm(U, mean=mu, sd=sd) This will fail if you go too far out in the tail (e.g. trying mu=0, sd=1, X=30); for that you need to be more careful, and work with log probabilities, etc. Duncan Murdoch> Any help would be highly appreciated,. > Thanks > Vijayan Padmanabhan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi, If I understand correctly, you can just simply keep generating norm values till you don't get the 100 appropriate one. I am pretty new to R, but here you go with a function that does this: rnorm25 <- function(mean,sd,X){ i = 0; ret = c() while ( i < 100 ){ r = rnorm(1,mean,sd) if ( r >= X & r <= X + 25 ){ ret = c(ret,r) i = i + 1 } } print(ret) } # test rnorm25(10,20,17) Cheers, zoltanctoth On Thu, Aug 4, 2011 at 9:03 AM, Vijayan Padmanabhan <padmanabhan.vijayan at gmail.com> wrote:> Hi > I am looking at generating a random dataset of say 100 values fitting in a > normal distribution of a given mean and SD, I am aware of rnorm > function. However i am trying to build into this function one added > constraint that all the random value generated should also obey the > constraint that they only take values between say X to X+25 > How do i do this in R? > Any help would be highly appreciated,. > Thanks > Vijayan Padmanabhan > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >