I am trying to use nlm() to minimize the SSE in a function. I'm having trouble with they syntax and getting nlm() to provide estimates for both variables. Eventually t_1, t_2, and t_3 will be values pulled from a data.frame but I just assigned them numbers until I could get the nlm() to work. I have attempted solutions from these threads there and there, with no luck:
t_1 <- 1.91
t_2 <- 3.23
t_3 <- 4.20
fun <- function(s,y){
(10 - s*(t_1-y + y*exp(-t_1/y)))^2+
(20 - s*(t_2-y + y*exp(-t_2/y)))^2+
(30 - s*(t_3-y + y*exp(-t_3/y)))^2
}
## Testing the function
fun(9.57,1.13)
[1] 0.9342627
I have tried multiple approaches for my nlm syntax. With 2 varaibles I believe I have to insert an array for p but when I've tried that it hasn't worked. None of these solutions below have worked:
# Attempt 1
p = array(c( 1,0), dim=c(2,1) )
ans <- nlm(fun, p)
# Attempt 2
ans <- nlm(fun, c( 0.1, 0.1))
# The first two return a "Error in f(x, ...) : argument "y" is missing, with no default"
# Attempt 3 returns a "invalid function value in 'nlm' optimizer" error
ans <- nlm(fun, c( 0.1, 0.1), y = c(1,1))
I'm sure there are several errors in my code but I'm not sure where. This task is more complex than I've attempted before as I'm relatively new to R.
If you look closely to the
nlmfunction. It asks only one argument. One solution is :Both
vectororarraywork however you can't give two arguments like you did.EDIT
In numerical optimization the initial point is really important. I advise you to use
optimfunction which is less sensitive of misspecification of initial point.One idea is to do like this, you make a grid of many initial points and you select the one which give you the best result :
I insist that with the example above the
optimfunction is realy more stable. I advise you to use it if you have no other constrains.You can check function parameters thanks to
?nlm.I hope it helps.
EDIT 2
I choose this initial point because it seems nearer to the optimal one.
You can obtain your two parameters like this : s is :
y is :
You also have the optimal value which is :
My second post, the edit is just their to alight the fact that optimisation with
nlmfunction is a bit tricky because you need ta carefully choose initial value.The
optimalso an optimisation function for R is more stable as in the example I give with many initialization points.expand.gridfunction is useful to obtain a grid like this :res
data.framegives you the minimum obtain with different initials values. You see that the first initials values give you no good result fornlmbut relatively stable one foroptim.tryfunction is just their to avoid the loop to break. Theifis to put NA at the right place.When there is
NAvalues it meens thatnlmdoesn't work well because of initialization. I advise you to chooseoptimif you don't need really precise optimisation because of its stability.To an extensive discussion on
optimvsnlm, you may have a look their. In your specific caseoptimseems to be a better choice. I don't know if we could generalise a bit.