Search code examples
roptimizationstatisticsdata-sciencemathematical-optimization

Error 'argument "param" is missing, with no default' - optim() function in R


I'm trying to use Pythagoras’ theorem to calculate the minimum value of time by creating functions in R that output T and (dT/dX1) as a function of X1, and use the optim() to numerically find the value of X1 that minimises T.

Time_1 <- function(param){((sqrt(param^2 + 225))/10) + ((sqrt(((25-param)^2) + 100))/2)} #Function to define T

D_Time <- function(param){(param / (10*(sqrt(param ^ 2 + 225)))) +
((param- 25) / (2*sqrt((25 - param) ^ 2 + 100)))} #Function to define (dT/dX1)

start_guess <- 1#start value
mle_param <- optim(par=start_guess, fn = Time_1(),gr = D_Time(), method = 'Brent')
Error in D_Time() : argument "param" is missing, with no default

I know something wrong with the fn=?, gr=?, but don't know how to fix this.


Solution

  • As mentioned by user 'jogo' replace fn and gr parameters with the name of respective functions without the parenthesis.

    Moreover, as you already figured out, the Brent method needs the lower and upper parameters to be filled in:

    lower, upper ... , or bounds in which to search for method "Brent"

    mle_param <- optim(par = start_guess,
                       fn = Time_1,
                       gr = D_Time,
                       method = 'Brent',
                       lower = 1,
                       upper = 25)