我想要适应转化的帕累托分布,然后需要计算 Hessian矩阵的以下数据。
library(stats4)
library(MASS)
library(vcd) # for goodness of fit test
library(pracma) # for hessain matrix
library(numDeriv)
# Data from Exceedances of Wheaton River flood data.
x =c(1.7, 2.2, 14.4, 1.1, .4, 20.6, 5.3, .7, 1.9, 13, 12, 9.3, 1.4, 18.7, 8.5, 25.5, 11.6,
14.1, 22.1, 1.1, 2.5, 14.4, 1.7, 37.6 ,.6, 2.2, 39, .3, 15, 11, 7.3, 22.9, 1.7, .1, 1.1,
.6, 9, 1.7, 7, 20.1, .4, 2.8, 14.1, 9.9, 10.4, 10.7, 30, 3.6, 5.6, 30.8, 13.3, 4.2, 25.5,
3.4, 11.9, 21.5, 27.6, 36.4, 2.7, 64, 1.5, 2.5, 27.4, 1, 27.1, 20.2, 16.8, 5.3, 9.7, 27.5,
2.5, 27)
k=.35 # guessed vales
gamma=.1 # minimum vales of x ,p, 0
lambda=-.95 # guessed vale
theta=c(k,lambda)
fn=function(k,lambda)
{
n=length(x)
-n*log(k)-n*(k)*log(.1)+(k+1)*sum(log(x))-sum(log((1-lambda)+2*lambda*((.1/x)^(x))))
}
result=nlm(fn, p=c(1), theta, hessian=TRUE, print.level=2 ) # minimization
print(result)
result1=solve(result$hessian) # inverse of Hesssain approx
print(result1)
此代码的结果仅提供单个值,这也是不正确的,我需要2×2的粗糙矩阵。 提前谢谢。
答案 0 :(得分:1)
fn
应该有一个长度为2的参数,并且nlm
args需要修复。由于我们记录了k
的日志,因此我们添加了一个条件,以防止k
降至接近零或更低。
fn=function(p)
{
k <- p[1]
if (k < 1e-10) return(10^10) # optional: will eliminate the warnings
lambda <- p[2]
n=length(x)
-n*log(k)-n*(k)*log(.1)+(k+1)*sum(log(x))-sum(log((1-lambda)+2*lambda*((.1/x)^(x))))
}
result=nlm(fn, theta, hessian=TRUE, print.level=2 ) # minimization
result1=solve(result$hessian) # inverse of Hesssain approx
print(result1)
,并提供:
[,1] [,2]
[1,] 0.0008266354 0.0000000000
[2,] 0.0000000000 0.0009375602