We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Wan • 6 years ago

Good read ! R is full of quirks and features.

I have doubts about your explanation of the optim function.
You said

This directly answers one question: What happens if method is not set? In this case, it will return the first element, which is why the default is in fact "Nelder-Mead".

which suggests the reason match.arg returns the first choice is because method is provided NULL.

But as you mentioned in your post, the method argument of optim is not null (See formals(optim)$method).

I believe what actually happens is when match.arg is called with only one argument (eg. match.arg(method)), choice are obtained from the formal argument which in this case is identical to method. When arg and choice are identical, match.arg will return the first element of choice. (See line 13 -14 in match.arg source code)

Let me know your thoughts on this.

Thanks!
Shu

alistaire47 • 5 years ago

No, there's no NULL; "not set" here was intended to mean the user not setting it, not the default not being defined in the function signature. When the user does not explicitly specify an argument for the parameter in this case, the default vector is passed through, triggering the behavior you correctly describe.

ruser • 5 years ago

Extremely enlightening blog post, thank you! I'm glad I found this when I, too, was wondering about the inner life of match.arg.