# Re: [R] function in nls argument

From: Katharine Mullen <kate_at_few.vu.nl>
Date: Wed, 07 May 2008 23:36:25 +0200 (CEST)

The error message means that the gradient (first derivative of residual vector with respect to the parameter vector) is not possible to work with; calling the function qr on the gradient multiplied by the square root of the weight vector .swts (in your case all 1's) fails.

If you want concrete advice it would be helpful to provide the commented, minimal, self-contained, reproducible code that the posting guide requests. what are the values of ST04, SM08b, ch2no, and tower?

General comments: If your goal is to minimize sum( (observed - predicted)^2), the function you give nls to minimize (optim.fun in your case) should return the vector (observed - predicted), not the scalar sum( (observed - predicted)^2). You may want to see the nls.lm function in package minpack.lm for another way to deal with the problem.

On Wed, 7 May 2008, Fernando Moyano wrote:

> Greetings R users, maybe there is someone who can help
> me with this problem:
>
> I define a function "optim.fun" and want as output the
> sum of squared errors between predicted and measured
> values, as follows:
>
> optim.fun <- function (ST04, SM08b, ch2no, a, b, d, E)
> {
> predR <-
> (a*SM08b^I(2)+b*SM08b+d)*exp(E*((1/(283.15-227.13))-(1/(ST04+273.15-227.13))))
> abserr <- abs(ch2no-predR)
> qnum <- quantile(abserr, probs=0.95, na.rm=T)
>
> is.na(abserr) <- (abserr > qnum)
> errsq <- sum(abserr^2, na.rm=T)
> errsq
> }
>
> Then I want to optimize parameters a,b,d and E as to
> minimize the function output with the following:
>
> optim.model<-nls(nulldat ~ optim.fun(ST04, SM08b,
> ch2no, a, b, d, E), data=tower,
> start=list(a=-0.003,b=0.13,d=0.50, E=400), na.action =
> na.exclude )
>
> were nulldat=0
> At this point I get the following error message:
>
> Error in qr.default(.swts * attr(rhs, "gradient")) :
> NA/NaN/Inf in foreign function call (arg 1)
> Warning messages:
> 1: In if (na.rm) x <- x[!is.na(x)] else if
> (any(is.na(x))) stop("missing values and NaN's not
> allowed if 'na.rm' is FALSE") ... :
> the condition has length > 1 and only the first
> element will be used
> (this warning is repeated 12 times)
>
> Question: what does the error mean? What am I doing
> wrong?
> Thanks a bunch.
> Nano
> Jen, Germany
> Max Planck for Biogeochemistry
>
>
>
>
> ____________________________________________________________________________________
>
> [[elided Yahoo spam]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help