# [R] Problems with nls-function

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 8 17:59:15 CEST 2007

```On 8/8/07, Michael Petram <Michi.Pe at web.de> wrote:
> Dear all
>
> I have got some problems with a least-squares regression using the function nls.
>
> I want to estimate h, k and X of the following formula by using nls :
>
> exp(2*200*(q^2-4*h/k-0.25+(2/k-0.5+4*h^2/k^2)*log(abs((k*q^2+2*h*q-1)/(0.25*k-h-1)))))/((-k*q^2-2*h*q+1)*X)
>
> y as defined by c(0.009747 0.001949 0.000000 0.003899 0.000000 0.000000 0.005848 0.001949)
> q as defined by c(-0.7500 -0.6875 -0.5625 -0.4875 -0.4625 -0.4375 -0.4125 -0.3875)
>
> (length of  the real q and y is 46; too long to post them here)
>
> i tought the correct using of nls would be:
>
> Mic<-nls(y~"function", start = list(k=1.0,h=0.1,X=exp(10))
>
> But it doesn`t work. i tryed an easier formula like :
>
> Mic<-nls(y~h*exp(2*k*200*(q^2)), start=list(h=0.1,k=1,X=100000))
>
> The result was the same.
>
>
> Isn`t  "nls"  the function i should use to solve this regression problem? Which things did i make wrong?

X is not in the model so by including it in your starting values you
make it non-idenfiable.  Get rid of it.

Also use better starting values.  Here we use grid search to get them:

> g <- expand.grid(h = 1:100/100, k = 1:100/100)
> st <- g[which.min(apply(g, 1, function(x) x * exp(2*x*200*q^2))),]
> nls(y~h*exp(2*k*200*(q^2)), start = st)
Nonlinear regression model
model:  y ~ h * exp(2 * k * 200 * (q^2))
data:  parent.frame()
h         k
0.0003533 0.0139700
residual sum-of-squares: 5.039e-05

Number of iterations to convergence: 16
Achieved convergence tolerance: 8.883e-06

```