# [R] error in function: nls (urgent)

From: tedzzx <zengzhenxing_at_gmail.com>
Date: Tue, 18 Nov 2008 04:19:09 -0800 (PST)

Hi,all:
I am running a nonlinear regression and there is a problem. There is a data frame: data

p s x t

```1  875.0 12392.5 11600 0.06967213
2  615.0 12332.5 12000 0.06967213
3  595.0 12332.5 12000 0.06967213
4  592.5 12337.0 12000 0.06967213
5  650.0 12430.0 12000 0.06967213
6  715.0 12477.5 12000 0.06967213
```

.
.
.
.

str(data):
'data.frame': 234 obs. of 4 variables:
``` \$ p: num  875 615 595 592 650 ...
\$ s: num  12392 12332 12332 12337 12430 ...
\$ x: num  11600 12000 12000 12000 12000 12000 12200 12200 12200 12200 ...
\$ t: num  0.0697 0.0697 0.0697 0.0697 0.0697 ...

```

My code to estimate a0,a1,a2:

bs<-function(s,x,t,a0,a1,a2){

```	v=exp(a0+a1*(x/s)+a2*(x/s)^2)
d1=(log(s/x)+(v^2)*t/2)/(v*sqrt(t))
d2=(log(s/x)-(v^2)*t/2)/(v*sqrt(t))
s*pnorm(d1)-x*pnorm(d2)
```

}
res<-nls(p~bs(s,x,t,a0,a1,a2),data=data,start=list(a0=-100,a1=100,a2=-100))

It returns the error:
Error in nlsModel(formula, mf, start, wts) :   singular gradient matrix at initial parameter estimates

By the way, bs function actually based on the Black-sholes option pricing model. There is a specific function in Rmetrics, can I call the function in Rmetrics package? Something like this:

bs<-function(s,x,t,a0,a1,a2,...){

```	v=exp(a0+a1*(x/s)+a2*(x/s)^2)
GBSOption(TypeFlag=c("c"),S=s,X=x,Time=t,r=0,b=0,sigma=v)@price
```
}

When I use this one, it returns the error similar to the upper one. Please help me, many thanks.

Ted

```--
View this message in context: http://www.nabble.com/error-in-function%3A-nls--%28urgent%29-tp20558499p20558499.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help