我一直在使用 R 包 optimx 进行统计模型估计。有谁知道如何获得 optimx 结果的 Hessian 矩阵?
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?
我一直在使用 R 包 optimx 进行统计模型估计。我的代码工作正常,我得到了所有 optimx 汇总结果(参数估计、函数值、收敛代码等)。但是,我还需要这些 optimx 结果的 Hessian 矩阵。我在 optimx 文档、R 站点和讨论论坛中寻求帮助均未成功。有人能帮我吗?先感谢您!!!
这里有一个例子(不是统计模型,但足以说明我的意思):
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)
全部在帮助页面上。请参阅 "Value" 标题下的 ?optimx
(在 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"
仅获取所用两种方法(Nelder-Mead 和 BFGS)的 Hessians
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
仅获取 Nelder-Mead 优化的 Hessian:
attr(ans1, "details")["Nelder-Mead" ,"nhatend"]
[[1]]
[,1] [,2]
[1,] 802.4220 -400.1041
[2,] -400.1041 200.0000
我一直在使用 R 包 optimx 进行统计模型估计。我的代码工作正常,我得到了所有 optimx 汇总结果(参数估计、函数值、收敛代码等)。但是,我还需要这些 optimx 结果的 Hessian 矩阵。我在 optimx 文档、R 站点和讨论论坛中寻求帮助均未成功。有人能帮我吗?先感谢您!!!
这里有一个例子(不是统计模型,但足以说明我的意思):
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)
全部在帮助页面上。请参阅 "Value" 标题下的 ?optimx
(在 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"
仅获取所用两种方法(Nelder-Mead 和 BFGS)的 Hessians
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
仅获取 Nelder-Mead 优化的 Hessian:
attr(ans1, "details")["Nelder-Mead" ,"nhatend"]
[[1]]
[,1] [,2]
[1,] 802.4220 -400.1041
[2,] -400.1041 200.0000