2

I'm looking to put a limit on the output parameters from optim(). It is needlessly converging thousands of phases of out of phase for my sinusoidal function (where 'designL' is my independent variable, and 'ratio' is my dependent variable data, dfm is my dataframe):

lo_0 = 2e-6
kc_0 = 80000
min.RSS <- function(data, par) {
  with(data, sum( (sin(par[2] *(par[1] + designL))^2 - ratio)^2) )
}
resultt <- optim(par = c(lo_0, kc_0), min.RSS, data = dfm)

I want to limit the lo_0 (phase shift) from 0:2e-5. I found some documentaition on this, but it doesn't go into much description on how to achieve: https://ubuntuforums.org/showthread.php?t=1420061

2
  • 1
    Your question can be improved by providing an example data set. It is not clear what is designL2 and ind_1 are. optim does have the option to provide upper and lower limits depending on the method you use. Feb 6, 2018 at 14:29
  • you should probably post your answer as an answer (doesn't matter that it's similar to mine), rather than as part of the question ...
    – Ben Bolker
    Feb 6, 2018 at 20:35

2 Answers 2

6

Probably

resultt <- optim(par = c(lo_0, kc_0), min.RSS, data = dfm[ind_1,],
     method="L-BFGS-B", lower=c(0,-Inf), upper=c(2e-5,Inf))

I strongly suggest that in addition you use the argument control=list(parscale=c(lo_0,kc_0)); optim() expects parameters to be similarly scaled and (when using finite-difference approximations to compute derivatives) to be of order 1.

1
  • The control argument made all the difference. Thank you Ben, you are a life saver!
    – Entropy
    Feb 6, 2018 at 14:53
0

I have edited in the fix to show here below. Note: I used finite values for the upper and lower bounds because the infinite parameters were giving me an error.

lo_0 = 2e-6
kc_0 = 80000
min.RSS <- function(data, par) {
  with(data, sum( (sin(par[2] *(par[1] + designL))^2 - ratio)^2) )
}
resultt <- optim(par = c(lo_0, kc_0), min.RSS,   
     data = dfm, method="L-BFGS-B", 
     lower=c(0,50000), upper=c(2e-5,100000), 
     control=list(parscale=c(lo_0,kc_0)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.