2
votes

I'm using optim/nlm to do a maximum likelihood estimation, and my parameters are in a multidimensional array.

The likelihood evaluates fine, i.e. given a data x, and multidimensional array of parameters theta, likelihood(theta,x) gives a real number.

However, using optim/nlm, with a starting value that has the same dimension as the theta had evaluated just fine, I'm getting the following error:

Error in theta[1, 1, 1] : incorrect number of dimensions

when evaluation the likelihood. It turns out that optim/nlm flattens my multidimensional array to a 1D array. Is there anyway I can use optim/nlm with a multidimensional array of parameters?

1
The specific solution will depend on the code you use. As far as I know the drop=FALSE parameter for [ cannot be applied globally.IRTFM

1 Answers

3
votes

I do not believe this is possible with optim itself. My advice would be to restore the shape yourself, e.g.

optim(
    matrix(1:4, 2, 2),
    function(par) {
        par = matrix(par, 2, 2) # Reshape
        sum((par - matrix(5:8, 2, 2))**2)
    }
)