1

Im running an optimisation routine using optim in R and im telling the programme what i want returned. for example, if i put return(op1$par), it will return all 4 of my variable values. Thats fine, and if i run return(op1), I obviously get all the information from the optimisation routine (par, value, convergence etc). However, in this format, the par values arent accessible in the output, it simply details that there are 4 values.

Now what i need is to the get the parameter values and the convergence information at the same time. R wont let me call this return(op1$par, op1$convergence) so im looking for the best way to get these two entities in one run?

I should specify that im writing this to a file for 1000s of iterations and not just looking to call it up once on screen.

Cheers

user1642166
  • 39
  • 1
  • 8
  • What do you mean that the par values are not accessible in the output if you return the `optim` result? Can't you assign `a <- fn(b)`, and then call `a$par`? – Blue Magister Sep 07 '12 at 20:45
  • Have you seen this? http://stackoverflow.com/questions/1826519/function-returning-more-than-one-value?rq=1 – Roman Luštrik Sep 08 '12 at 06:43

2 Answers2

2

Try something like this:

return(c(Parameters=op1$par, Convergence=op1$convergence))

The names Parameters and Convergence are only for identifying what are the parameters and what is the convergence, since this result will be a vector.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0

By design, a function can return only one object (or else assignments like a <- fn(b) would get confusing; which thing do you assign?). But that object can be a vector, or a list (which is what optim does). So wrap your arguments in something like

return(c(par=op1$par, convergence=op1$convergence))

or more generally (for objects of different types),

return(list(par=op1$par, convergence=op1$convergence))
Blue Magister
  • 13,044
  • 5
  • 38
  • 56