Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multinomial density function working with non-integer data?

Does anyone know why dmultinom function in R works with non-integers and what does it return in that case? See below:

> dmultinom(c(0.7,0.1,0.1,0.1) ,prob = c(0.25,0.25,0.25,0.25))
[1] 0.25

> dmultinom(c(0.25,0.25,0.25,0.25) ,prob = c(0.25,0.25,0.25,0.25))
[1] 1

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the source code of dmultinom

function (x, size = NULL, prob, log = FALSE) 
{
    K <- length(prob)
    if (length(x) != K) 
        stop("x[] and prob[] must be equal length vectors.")
    if (any(!is.finite(prob)) || any(prob < 0) || (s <- sum(prob)) == 
        0) 
        stop("probabilities must be finite, non-negative and not all 0")
    prob <- prob/s
    x <- as.integer(x + 0.5)
    if (any(x < 0)) 
        stop("'x' must be non-negative")
    N <- sum(x)
    if (is.null(size)) 
        size <- N
    else if (size != N) 
        stop("size != sum(x), i.e. one is wrong")
    i0 <- prob == 0
    if (any(i0)) {
        if (any(x[i0] != 0)) 
            return(if (log) -Inf else 0)
        if (all(i0)) 
            return(if (log) 0 else 1)
        x <- x[!i0]
        prob <- prob[!i0]
    }
    r <- lgamma(size + 1) + sum(x * log(prob) - lgamma(x + 1))
    if (log) 
        r
    else exp(r)
}

As you can see, it rounds the x to the closest integer in x <- as.integer(x + 0.5).
Therefore, in your case it is equivalent to:

dmultinom(c(0,0,0,0) ,prob = c(0.25,0.25,0.25,0.25))

[1] 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading