# [R] question about formulating a nls optimization

Douglas Bates bates at stat.wisc.edu
Fri Jul 18 21:15:51 CEST 2003

```Adi Humbert <adrian_humbert at yahoo.com> writes:

> I'm migrating a project from Matlab to R, and I'm
> facing a relatively complicated problem for nls.  My
> objective function is below:
>
> >> objFun <- function(yEx,xEx,tEx,gamma,theta,kappa){
>      yTh <- pdfDY(xEx,tEx,gamma,theta,kappa)
>      sum(log(yEx/yTh)^2)
>    }
>
> The equation is yTh=P(xEx,tEx) + noise.
> I collect my data in:
>
> >> data <- data.frame(xEx,tEx,yEx)
>
> And I do the nls:
> aux <- nls( ~ objFun(yEx,xEx,tEx,gamma,theta,kappa),
>            data=data,
>    start=list(gamma=0.085, theta=8.62*10^(-5),
> kappa=4.45*10^(-3)), trace=TRUE)
>
> I get the following error:
> 179.8911 :  8.50e-02 8.62e-05 4.45e-03
> Error in sum(rr[-(1:npar)]^2) : subscript out of
> bounds

Could you run traceback() at this point so we can see where the error
message is originating?  I would hope that npar is 3 and my guess is
that the error message is originating in a call to

conv = function()
{
rr <- qr.qty( QR, resid ) # rotated residual vector
sqrt( sum( rr[1:npar]^2 ) / sum( rr[ -(1:npar) ]^2 ) )
},

How many rows are there in your data frame?

> One problem that I identified is that the algorithm
> does not change the values of the parameters at all.
> It calculates the objective function OK, but the
> parameters are not updated.

> The function pdfDY is a Fourier integral with
> parameters xEx, tEx, gamma, theta, kappa, that I
> calculate with a Filon algorithm.  The calculation of
> pdfDY is correct.

```