[R] names of arguments in function for optimize

Duncan Murdoch murdoch at stats.uwo.ca
Wed Nov 15 00:13:42 CET 2006


On 11/14/2006 6:00 PM, Steve Su wrote:
> Dear All,
> 
>  
> 
> I noticed that optimize in R 2.4.0 on Windows XP does not seem to work
> if I use "m" as an argument in my function for example:
> 
>  
> 
>> optimize(function(x,m) x^m,interval=c(0,1),m=2)
> 
> Error in f(arg, ...) : argument "m" is missing, with no default

Take a look at the header for optimize:

 > optimize
function (f, interval, lower = min(interval), upper = max(interval),
     maximum = FALSE, tol = .Machine$double.eps^0.25, ...)

Unfortunately, the ... comes after the other arguments, so partial name 
matching will be attempted.  By saying m=2 you've set the maximum arg to 2.

You can avoid this by specifying maximum explicitly, e.g.

 > optimize(function(x, m) x^m, interval=c(0,1), maximum=FALSE, m=2)
$minimum
[1] 6.610696e-05

$objective
[1] 4.37013e-09

Duncan Murdoch


> 
>  
> 
>  
> 
> But! If I change the argument to "a" then it works.
> 
>  
> 
>> optimize(function(x,a) x^a,interval=c(0,1),a=2)
> 
> $minimum
> 
> [1] 6.610696e-05
> 
>  
> 
> $objective
> 
> [1] 4.37013e-09
> 
>  
> 
> 
>  
> 
> Why?
> 
>  
> 
>  
> 
> Steve.
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list