# [R] dmultinomial

Ben Bolker bbolker at gmail.com
Thu Nov 18 21:57:54 CET 2010

```Alexander Shenkin <ashenkin <at> ufl.edu> writes:

>
> Hello All,
>
> I'm trying to run a maximum likelihood analysis using dmultinomial (i'm
> avoiding dmultinom as I'd like to run it with vectors for the ML stuff).
>  However, I'm having a hard time getting even the simplest example
> running.  Any help would be greatly appreciated.
>
> > library(mc2d)
> > dmultinomial(x=c(0,0,1),prob=c(1,1,1),size=1,log=TRUE)
> Error in if (ncol(x) != K) stop("x[] and prob[] must be equal length
> vectors or equal col matrix.") :
>   argument is of length zero
>
> # Once I get the simple stuff above running, I'd like to be able to do
> the following:
>
> > testmat=rmultinomial(4, 1, prob)
> > testmat
>      [,1] [,2] [,3]
> [1,]    1    0    0
> [2,]    0    0    1
> [3,]    0    0    1
> [4,]    0    0    1
> > dmultinomial(x=testmat,prob=prob,size=1,log=TRUE)
> Error in if (!is.null(size) && nrow(size) != n) size <- matrix(t(size),  :
>   missing value where TRUE/FALSE needed
>
> thanks,
> allie
>
>

Hi Allie :-)

You might be able to vectorize for yourself using
apply, mapply ... ?

prob <- c(1,1,1)
dmultinom(c(0,0,1),prob=c(1,1,1),size=1,log=TRUE)
set.seed(1001)
testmat <- rmultinom(n=4,size=1,prob)
## note values are returned as COLUMNS
apply(testmat,2,dmultinom,prob=prob,size=1,log=TRUE)

I finally figured out how to install mc2d (after misreading
it as "m2cd" about 25 times -- argh !!) and am having the
same sorts of troubles.  I am out of my time quota for figuring
this out ... sorry ...

Ben

```