2

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?

pmangg
  • 35
  • 1
  • 3
  • 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 Nov 10 '11 at 19:59

1 Answers1

3

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)
    }
)
Owen
  • 38,836
  • 14
  • 95
  • 125
  • Thanks for the idea, I ended up putting the dimension of the multidimensional array as a parameter to my function, and perform the reshape to that inside the likelihood function. – pmangg Nov 11 '11 at 00:25