Search code examples
roptimizationminimum

Minimum of a multidimensional function


I have a function R^5 -> R, and I am interested in its minimum. There are plenty of functions in R like optim, optimize or fminbnd in the R package pracma. But they just accept one argument and I don't understand the help page.

mindisturbed <- function(a,d1,d2,d3,p){
  sum((data^(- a) * (d1 + d2*cos(log(data)*2*pi/p) + d3 *
                            sin(log(data)*2*pi/p)) - log(j))^2)
}

The "data" and the "j" variable are in my global settings. These are vectors with length k. The arguments of the function are all numeric numbers with length 1. The function is an residual square sum.

So do anyone know how to minimize this function in depend of all its arguments?


Solution

  • Assuming data and j are vectors of the same length try the following. You may or may not need better starting values.

    1) Use optim like this

    st <- c(a = 1, d1 = 1, d2 = 1, d3 = 1, p = 1)
    f <- function(x) mindisturbed(x[1], x[2], x[3], x[4], x[5])
    optim(st, f)
    

    2) or nls with default algorithm where st is from (1)

    fo <- log(j) ~ data^(- a) * (d1 + d2*cos(log(data)*2*pi/p) + d3 *
                            sin(log(data)*2*pi/p))
    nls(fo, start = st)
    

    3) or nls with plinear algorithm. In that case the RHS of the formula is a matrix with column names d1, d2 and d3 such that first column multiplies d1, second d2 and third d3. Only the nonlinear parameters, i.e. a and p, are specified in start.

    fo2 <- log(j) ~ data^(-a) * cbind(d1 = 1, 
                                      d2 = cos(log(data)*2*pi/p), 
                                      d3 = sin(log(data)*2*pi/p))
    nls(fo2, start = c(a = 0.1, p = 0.1), algorithm = "plinear")
    

    Note

    The question did not include data and j but we can use these to try it out.

    set.seed(123)
    n <- 100
    data <- runif(n, 1, 2)
    j <- 1:n
    
    o <- order(data)
    j <- j[o]
    data <- data[o]