n中的nlm()或optimize()比R中的optim()更精确

时间:2017-04-24 23:15:07

标签: r precision

我想在我的函数x[1]中对x[2]GGG进行更精确(最多6个小数位)的估算。

使用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 个答案:

答案 0 :(得分:1)

optim函数具有公差控制参数。用以下代码替换你的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