# [R] nls and na/Nan/Inf error

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Sep 26 09:28:24 CEST 2005

```This works if you omit the deriv() step.

Use R's options(error=dump.frames) and debugger().  This gives

Browse[1]> rhs
[1] 0.4330000 0.4272571 0.3994105 0.3594037 0.3270730 0.3104752 0.3000927
[8] 0.2928445 0.2874249 0.2831787
Vr        Vm       alpha        lamda
[1,] 0.00000000 1.0000000  0.00000000          NaN
[2,] 0.02815158 0.9718484 -0.04069202  0.001183749
[3,] 0.16465431 0.8353457 -0.17769291 -0.035591190
[4,] 0.36076599 0.6392340 -0.24085444 -0.100064577
[5,] 0.51925014 0.4807499 -0.21793994 -0.136056450
[6,] 0.60061200 0.3993880 -0.19071160 -0.145267481
[7,] 0.65150658 0.3484934 -0.17020938 -0.147113828
[8,] 0.68703698 0.3129630 -0.15471851 -0.146388612
[9,] 0.71360324 0.2863968 -0.14263118 -0.144660967
[10,] 0.73441811 0.2655819 -0.13290951 -0.142543261

and note the NaN.  Now think about your formula for x = 0: it does not
actually depend on lamda.  The analytical derivative ends up with a
calculation as 0/0.

On Mon, 26 Sep 2005, Tony Meissner wrote:

> I am trying to it a particular nonlinear model common in Soil Science to
> moisture release data from soil.  I have written the function as shown
> below according to the logist example in Ch8 of Pinheiro & Bates.  I am
> getting the following error (R version 2.1.1)
>
> *Error in qr(attr(rhs, "gradient")) : NA/NaN/Inf in foreign function
> call (arg 1)*
>
> Below is the function and data.
>
> /# the van genuchten moisture release function
> vanGen <- function(x, Vr, Vm, alpha, lamda) {
>   if (Vr < 0) Vr <- 0
>   Vr + (Vm - Vr)/((1+(alpha*x)^lamda)^(1-1/lamda))
>   }
> vanGen <- deriv(~Vr + (Vm - Vr)/((1+(alpha*x)^lamda)^(1-1/lamda)),
>   c("Vr", "Vm", "alpha", "lamda"), function(x, Vr, Vm, alpha, lamda) {} )/
>
>
> the call in R
>
> /> fm1fld.nls <- nls(Moisture ~ vanGen(Suction, Vr,Vm,alpha,lamda),
> + data=fldgd, start=c(Vr=0.229, Vm=0.433, alpha=0.2, lamda=1.5))
>
> /and the data:/
>
> /*   Suction Moisture
> 1        0    0.433
> 2        1    0.421
> 3        4    0.400
> 4       10    0.379
> 5       20    0.366
> 6       30    0.362
> 7       40    0.358
> 8       50    0.353
> 9       60    0.351
> 10      70    0.349
> */
> /can anyone offer any suggestions.  The parameters Vr, Vm >= 0,  alpha >
> 0, and
> lamda > 1

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```