rmathematical-optimizationhessian

I have been working on a statistical model estimation using the R package optimx. Does anyone know how to get the Hessian matrix of optimx results?


I have been working on a statistical model estimation using the R package optimx. My code works fine and I get all optimx summary results (parameter estimates, function values, convergence codes, etc.). However, I also need the Hessian matrix of these optimx results. I have unsuccessfully looked for help in the optimx documentation, R sites, and discussion forums. Can someone help me? Thank you in advance!!!

Here is an example (not a statistical model, but enough to show what I mean):

library(optimx) 

fr <- function(x) {
    ## Rosenbrock Banana function 
    x1 <- x[1] 
    x2 <- x[2] 
    100 * (x2 - x1 * x1)^2 + (1 - x1)^2 
    }

grr <- function(x) { 
    ## Gradient of ’fr’ 
    x1 <- x[1] 
    x2 <- x[2] 
    c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 200 * (x2 - x1 * x1)) 
    } 

ans1<-optimx(c(-1.2,1), fr, grr)

Solution

  • It's all on the help page. See ?optimx under the "Value" heading (after xtimes).

    The attribute "details" to the returned answer object contains information, if computed, on the gradient (ngatend) and Hessian matrix (nhatend) at the supposed optimum, along with the eigenvalues of the Hessian (hev), as well as the message, if any, returned by the computation for each method, which is included for each row of the details. If the returned object from optimx() is ans, this is accessed via the construct attr(ans, "details")

    This object is a matrix based on a list so that if ans is the output of optimx then attr(ans, "details")[1, ] gives the first row and attr(ans,"details")["Nelder-Mead", ] gives the Nelder-Mead row. There is one row for each method that has been successful or that has been forcibly saved by save.failures=TRUE.

    attr(ans1, "details")["Nelder-Mead", ]
    $method
    [1] "Nelder-Mead"
    
    $ngatend
    [1]  0.006260098 -0.002869164
    
    $nhatend
              [,1]      [,2]
    [1,]  802.4220 -400.1041
    [2,] -400.1041  200.0000
    
    $hev
    [1] 1002.0216761    0.4003383
    
    $message
    [1] "none"
    

    Get just the Hessians for both methods used (Nelder-Mead and BFGS)

    attr(ans1, "details")[ ,"nhatend"]
    $`Nelder-Mead`
              [,1]      [,2]
    [1,]  802.4220 -400.1041
    [2,] -400.1041  200.0000
    
    $BFGS
         [,1] [,2]
    [1,]  802 -400
    [2,] -400  200
    

    Get the Hessian for only the Nelder-Mead optimization:

    attr(ans1, "details")["Nelder-Mead" ,"nhatend"]
    [[1]]
              [,1]      [,2]
    [1,]  802.4220 -400.1041
    [2,] -400.1041  200.0000