[R] How to solve "nlm() non-finite value supplied by 'nlm'" and how to constrain parameters to (0, 1)?
John Nash
John.Nash at uottawa.ca
Sun Apr 2 15:37:45 CEST 2017
nlm doesn't include bounds so you'd need to transform. nlminb does, as
do a number of codes in optimr (and more in R-forge version optimrx). The latter
packages have a common call which simplifies choosing the solver. My own Rvmmin is an all-R
implementation of the same method (but some details changed) as optim::BFGS but it includes
bounds constraints. Moreover, it is fairly easy to get inside and add some checks if needed.
If you can, I recommend supplying analytic gradients too, though that is often quite a
bit of work. If you do this, remember to check that the code is correct (numDeriv helps).
Since you have only 2 parameters, I think I'd give nmkb (it is from dfoptim or in optimrx).
It handles bounds, but you cannot start on the bound because it uses the transfinite transformation
to handle the bounds. The method is relatively slow and clunky, but for 2 parameters, you are not
going to wait too long.
Best, JN
On 2017-04-02 08:19 AM, mviljamaa wrote:
> So I'm getting:
>
> Error in nlm(neglikhood, p = c(0.1, 0.1), hessian = T, x = elinajat) :
> non-finite value supplied by 'nlm'
> In addition: There were 50 or more warnings (use warnings() to see the first 50)
>
> with the following (neglikelihood of 1-param. Weibull):
>
> neglikhood <- function(theta,x) {
> n <- length(x)
> -(n*log(theta[2])+n*log(theta[1])+(theta[1]-1)*sum(log(x))-theta[2]*sum(x^theta[1]))
> }
>
>> elinajat
> [1] 17.88 28.92 33.00 41.52 42.12 45.60 48.48 51.84 51.96 54.12 55.56 67.80
> [13] 68.64 68.64 68.88 84.12 93.12 98.64 105.12 105.84 127.92 128.04 173.40
>
> I read somewhere that I might need to constrain my parameters to the range (0,1). Is this correct?
>
> For example:
>
>> neglikhood(c(0.1,0.1),elinajat)
> [1] 195.3213
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list