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
Use a grid search to get the starting values in which case you will likely be close enough that you won't run into problems even without derivatives: attach(fldgd) grid <- expand.grid(Vr = seq(0,.3,.1), Vm = seq(.45, 1, .05), alpha = seq(1,2,.25), lamda = seq(1,2,.25)) ss <- function(p) sum((Moisture - vanGen(Suction, p[1], p[2], p[3], p[4]))^2) idx <- which.min(apply(grid, 1, ss)) startval <- grid[idx,] nls(Moisture ~ vanGen(Suction, Vr, Vm, alpha, lamda), start = startval) On 9/26/05, Tony Meissner <tony.meissner at bigpond.com> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
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
attr(,"gradient")
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