2017-04-24 61 views
0

我想在我的函数GGG中获得更精确(最多6位小数位)的x[1]x[2]是nlm()或优化()比R()更精确

使用optim,我得到了一些精度高达3位小数,但我想知道如何提高至少6位小数的精度?

可以使用optimizenlm达到这个目标吗?

GGG = function(Low, High, p1, p2) { 


f <- function(x) { 

y <- c(Low, High) - qcauchy(c(p1, p2), location=x[1], scale=x[2]) 

} 


## SOLVE: 
AA <- optim(c(1,1), function(x) sum(f(x)^2)) 

## return parameters: 
parms = unname(AA$par) 


return(parms)  ## Correct but up to 3 decimal places 

} 

## TEST: 
AAA <- GGG (Low = -3, High = 3, p1 = .025, p2 = .975) 


## CHECK: 
q <- qcauchy(c(.025, .975), AAA[1], AAA[2]) # What comes out of "q" MUST match "Low" and 
               # "High" up to 6 decimal places 

回答

1

优化功能有一个容差控制参数。这种替换您的Optim功能:

AA <- optim(c(1,1), function(x) sum(f(x)^2), control=list(reltol=(.Machine$double.eps))) 

返回:

> q 
[1] -3 3 
> AAA 
[1] 5.956798e-08 2.361051e-01 
+0

亲爱的THC,非常感谢你。请问你是否可以修改你的回复[** HERE **](http://stackoverflow.com/questions/43286436/using-optimize-to-find-the-shortest-interval-that-takes-95-曲线区域)所以函数[** THERE **](http://stackoverflow.com/questions/43286436/using-optimize-to-find-the-shortest-interval-that-takes-95曲线区域)可以在所有情况下工作而无需手动***调整间隔? – rnorouzian