[R] error in function: nls (urgent)
Douglas Bates
bates at stat.wisc.edu
Wed Nov 19 21:43:25 CET 2008
On Tue, Nov 18, 2008 at 6:19 AM, tedzzx <zengzhenxing at gmail.com> wrote:
> Hi,all:
> I am running a nonlinear regression and there is a problem.
Yes. The problem is that there is a singular gradient matrix at the
initial parameter estimates. You will need to modify your function or
your initial parameter estimates.
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list