TABLE OF CONTENTS


simSurvival/generaterandom [ Functions ]

NAME

    generaterandom --- Generate random numbers

FUNCTION

Generate random numbers from any of the following distributions:

     fixed         not random, fixed at a certain value
     weibull       Weibull, parametrized by rate and shape
     gamma         Gamma, parametrized by mean and variance
     normal        Normal, parametrized by mean and variance
     lognormal     Lognormal, parametrized by mean and variance
     normmix       Mixture of normals, parametrized by means, variances and weights
     lognormmix    Mixture of lognormals, parametrized by means, variances and weights
     unifmix       Mixture of uniforms, parametrized by weights and bounds

SYNOPSIS

522 generaterandom <- function(n, type, params)

INPUTS

     n         number of variates to generate 
     type      string, one of the types above
     params    a list with parameters, which are different for each type. See the code
               for each type

OUTPUTS

     out       n random numbers with the specified distribution

SOURCE

525 {
526     if(!(type%in%c("fixed", "weibull", "gamma", "normal", "lognormal", "normmix",
527             "lognormmix", "unifmix"))) stop("Invalid distribution type")
528     # fixed at params$value
529     if(type == "fixed"){
530         if(!("value"%in%names(params))) stop("Parameter value not specified for type fixed")
531         return(rep(params$value, n))
532     }
533     # weibull with rate params$lambda0 and shape params$gamweib
534     if(type == "weibull"){
535         if(!all(c("lambda0", "gamweib")%in%names(params))) 
536                 stop("Parameters lambda0, gamweib not specified for type weibull")
537         lambda0 <- params$lambda0
538         gamweib <- params$gamweib
539         return(rweibull(n, shape = gamweib, scale = lambda0^(-1 / gamweib)))
540     }
541     # gamma with mean params$mu and variance params$sigma2
542     if(type == "gamma"){
543         if(!all(c("mu", "sigma2")%in%names(params)))
544                 stop("Parameters mu, sigma2 not specified for type gamma")
545         mu <- params$mu
546         sigma2 <- params$sigma2
547         return(rgamma(n, shape = mu^2 / sigma2, scale = sigma2 / mu))
548     }
549     # normal with mean params$mu and variance params$sigma2
550     if(type == "normal"){
551         if(!all(c("mu", "sigma2")%in%names(params))) 
552                 stop("Parameters mu, sigma2 not specified for type normal")
553         mu <- params$mu
554         sigma2 <- params$sigma2
555         return(rnorm(n, mean = mu, sd = sqrt(sigma2)))
556     }
557     # lognormal with mean params$mu and variance params$sigma2
558     if(type == "lognormal"){
559         if(!all(c("mu", "sigma2")%in%names(params)))
560                 stop("Parameters mu, sigma2 not specified for type lognormal")
561         mu <- params$mu
562         sigma2 <- params$sigma2
563         sigma2prime <- log(1 + sigma2 / mu^2)        
564         muprime <- log(mu) - 1 / 2 * sigma2prime
565         return(rlnorm(n, meanlog = muprime, sdlog = sqrt(sigma2prime)))
566     }
567     # normal mixture with weights params$w, means params$mu and variances params$sigma2
568     if(type == "normmix"){
569         if(!all(c("mu", "sigma2", "w")%in%names(params)))
570                 stop("Parameters mu, sigma2, w not specified for type normmix")
571         mu <- params$mu
572         sigma2 <- params$sigma2
573         w <- params$w / sum(params$w)
574         if(length(w) == 1) w = rep(w, length(mu))
575         if(length(mu) != length(sigma2) | length(mu) != length(w) |
576                 length(mu) < 2) stop("Bad parameter lengths for type normmix")
577         out <- MYmvrnorm(n, mu, mdiag(sigma2)) 
578         return(t(out)[findInterval(runif(n), cumsum(w)) + 1 + 0:(n - 1) * length(w)])
579     }
580     # lognormal mixture with weights params$w, means params$mu and variances params$sigma2
581     if(type == "lognormmix"){
582         if(!all(c("mu", "sigma2", "w")%in%names(params)))
583                 stop("Parameters mu, sigma2, w not specified for type lognormmix")
584         mu <- params$mu
585         sigma2 <- params$sigma2
586         w <- params$w / sum(params$w)
587         if(length(w) == 1) w = rep(w, length(mu))
588         if(length(mu) != length(sigma2) | length(mu) != length(w) |
589                 length(mu) < 2) stop("Bad parameter lengths for type lognormmix")
590         sigma2prime <- log(1 + sigma2 / mu^2)        
591         muprime <- log(mu) - 1 / 2 * sigma2prime
592         out <- MYmvrnorm(n, muprime, mdiag(sigma2prime)) 
593         return(exp(t(out)[findInterval(runif(n), cumsum(w)) + 1 + 0:(n - 1) * length(w)]))   
594     }
595     # uniform mixture with weights params$w, bounds params$bounds
596     if(type == "unifmix"){
597         if(!all(c("w", "bounds")%in%names(params)))
598                 stop("Parameters w, bounds not specified for type unifmix")
599         w <- params$w / sum(params$w)
600         bounds <- matrix(params$bounds, ncol = 2, byrow = TRUE)
601         out <- rep(0, n)
602         for(i in 1:n){
603             which <- sum(runif(1) > cumsum(w)) + 1
604             out[i] <- runif(1, bounds[which, 1], bounds[which, 2])
605         }
606         return(out)
607     }
608 }