1
votes

The function dmultinom (x, size = NULL, prob, log = FALSE) estimate probabilities of a Multinomial distribution. However, it does not run with size =1.

Theoretically, when setting size=1 the Multinomial distribution should be equivalent to the Categorical distribution.

Does anybody know why the error message?

FYI, Categorical distribution can be modelled by dist.Categorical {LaplacesDemon}.

Examples:

dmultinom(c(1,2,1),size = 1,prob = c(0.3,0.5,0.4))

Error in dmultinom(c(1, 2, 1), size = 1, prob = c(0.3, 0.5, 0.4)) : size != sum(x)

dcat(c(1,2,1),p = c(0.3,0.5,0.4))

[1] 0.3 0.5 0.3

Thanks

1

1 Answers

2
votes

LaplacesDemon::dcat and stats::dmultinom do two different things. If you have multiple observations dcat takes a vector of category values, whereas dmultinom takes a single vector response, so you have to construct a matrix of responses and use apply (or something).

library(LaplacesDemon)
probs <- c(0.3,0.5,0.2)
dcat(c(1,2,1), p = probs) ## ans: 0.3 0.5 0.3
x=matrix(c(1,0,0,
           0,1,0,
           1,0,0),
         nrow=3,byrow=TRUE)
apply(x,1,dmultinom,size=1, prob=probs)

(I modified your example because your original probabilities, c(0.3,0.5,0.4), don't add up to 1 - neither function gives you a warning, but dmultinom automatically rescales the probabilities to sum to 1)

If I try dmultinom(c(1,2,1),p=probs, size=1) I get

size != sum(x)

that is, dmultinom is interpreting c(1,2,1) as "one sample from group 1, two samples from group 2, 1 from group 3", which isn't consistent with a total sample size of 1 ...