2017-04-24 65 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