R/rMultinom.R

Defines functions rMultinom

Documented in rMultinom

rMultinom <- function(p){
    if(any(p < 0))
        stop("non-positive probability")
    if(!isTRUE(all.equal(rowSums(p),rep(1, nrow(p)))))
        stop("The sums of probabilities have to be 1.")
    k <- ncol(p)
    n <- nrow(p)
    csm <- ifelse(outer(1:k,1:(k - 1),FUN=`<=`), 1, 0)
    cp <- p %*% csm
    U <- runif(n)
    rowSums(sweep(cp, 1, U, `<=`)) + 1
}

Try the miscF package in your browser

Any scripts or data that you put into this service are public.

miscF documentation built on April 14, 2020, 7:01 p.m.