2

我需要近似Birnbaum-Saunders distr的样本参数。这里是我的代码:R中的Newtonraphson代码导致不同的结果

x =c(6.7508, 1.9345, 4.9612, 22.0232, 0.2665, 66.7933, 5.5582, 60.2324, 72.5214, 1.4188, 4.6318, 61.8093, 11.3845, 1.1587, 22.8475, 8.3223, 2.6085, 24.0875, 4.6762, 8.2369) 
l.der1 = function(theta,x) { 
gamma <- theta[1] 
beta <- theta[2] 
n <- length(x) 
ausdruck1=sum((sqrt(x/beta)-sqrt(beta/x))^2) 
ausdruck2=sqrt(x/beta)+sqrt(beta/x) 
matrix(c(-n/gamma+ausdruck1/gamma^3, sum((1/(2*x*sqrt(beta/x))-x/(2*beta^2*sqrt(x/beta)))/ausdruck2)-1/(2*gamma^2)*sum(1/x-x/beta^2)),2, 1) 
} 

l.der2 = function(theta,x) { 
gamma <- theta[1] 
beta <- theta[2] 
n <- length(x) 
ausdruck1=sum((sqrt(x/beta)-sqrt(beta/x))^2) 
ausdruck2=sqrt(x/beta)+sqrt(beta/x) 
ausdruck3=(1/gamma^3)*sum(1/x-x/beta^2) 
matrix(c(n/gamma^2-(3*ausdruck1)/gamma^4,ausdruck3,ausdruck3,sum((2-beta/x+x/beta)/(2*beta^2*ausdruck2^2))-(1/(2*gamma^2))*sum(2*x/beta^3)),2, 2, byrow=T) 
} 

newtonraphson = function(theta,l.der1,l.der2,x,col=2,epsilon=10^(-6)) { 
I <- l.der2(theta,x) 
thetastar <- theta - solve(I) %*% l.der1(theta,x) 
repeat {theta=thetastar 
    thetastar <- theta - solve(I) %*% l.der1(theta,x) 
    if (((thetastar[1]-theta[1])^2)/thetastar[1]^2 < 10^(-6) && ((thetastar[2]-theta[2])^2)/thetastar[2]^2 < 10^(-6)) #calculating relative convergence 
    return(thetastar) 
} 
} 

theta = c(1,4) #starting point 
theta= newtonraphson(theta,l.der1,l.der2,x=x) 
theta 

的问题是,虽然收敛的状况似乎得到满足,我的近似值不同,在我看来,显著,这取决于我选择为出发点,这THETA。因此,我不知道选择一个甚至略有不同的起点,我会得到哪些结果。

为什么该方法如此不稳定的任何想法?

回答

4

我不会重新发明这种问题的车轮,并使用自定义算法。我将在一个实现Newton-raphson算法的多R包中使用一些已经构建的函数。

例如,这里使用rootSolve包:

library(rootSolve) 
theta <- c(1,4) 
multiroot(l.der1,theta,jacfunc=l.der2,x=x) 
$root 
[1] 1.87116 6.83414 

$f.root 
      [,1] 
[1,] 2.168992e-08 
[2,] 6.425832e-09 

$iter 
[1] 8 

$estim.precis 
[1] 1.405788e-08 

我得到theta2 <- c(1,3)相同的结果。

+0

谢谢。事情是,当我收敛到10 ^( - 6)时,我特别给了一个任务来停止。我认为问题在于“重复”循环。 – Lola

+0

你能再请看我的重复循环吗?我真的需要知道为什么它会失败,否则我会一直犯同样的错误。我很难看到有什么问题...... – Lola