[R] Histogram for mixed random variables

Stephen D. Weigand weigand.stephen at charter.net
Fri May 6 15:04:40 CEST 2005


Dear Paul,

On May 5, 2005, at 6:43 AM, Paul Smith wrote:

> Dear All
>
> I would like to get the histogram for the following model with
> discrete and continuous random variables:
>
> * with probability 1/3, a random number is drawn from the continuous
> uniform distribution (min=0, max=1);
> * with probability 2/3, a random number is drawn from a different
> continuous uniform distribution (min=-1/2, max=1/2).
>
> Any help would be most welcome.
>
> Thanks in advance,
>
> Paul

My approach to generate the data would be

rMyfun <- function(n){
   x1 <- runif(n, 0, 1)
   x2 <- runif(n, -0.5, 0.5)
   ifelse(rbinom(n, 1, 1/3), x1, x2)
}

Hope this helps,

Stephen




More information about the R-help mailing list