Search code examples
rminimum

Function of loss in R


I have function of loss given for a k-elements vector by the following formula:

Formula

Where p is a positive parameter. I have to write a function in R which returns the value of u which minimizes this function. How can I do that?


Solution

  • first define the function you want to minimize:

    my_function <- function(u) {
        ...
    }
    

    Then you can call the optim() function:

    result <- optim(starting_point_for_u, my_function, ...)
    # parameter value minimizing the function
    result$par
    # minimal value of function
    result$value