rnorouzian
rnorouzian

Reputation: 7517

are nlm() or optimize() more precise than optim() in R

I am trying to get more precise (up to 6 decimal places) estimates of x[1] and x[2] in my function called GGG.

Using optim, I get some precision up to 3 decimal places, but am wondering how I can improve precision up to at least 6 decimal places?

Can optimize and nlm be used for this goal?

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

Upvotes: 1

Views: 822

Answers (1)

thc
thc

Reputation: 9705

The optim function has a tolerance control parameter. Replace your optim function with this:

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

Returns:

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

Upvotes: 2

Related Questions