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.
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. >
Doug Bates and I have exchanged ideas on the issue of singularities in nonlinear models a number of times over the years. Both perspectives are "right", and though I will characterize them as adversarial, they are really complementary. These views can be overly simplified as - if there's a singularity, something's wrong (DB) - if you can minimize the objective you should (JN) Thesse attitudes result in somewhat different output, and the JN attitude can often have multiple solutions (even an infinity of solutions in some cases), akin to collinearity in linear models. It is a minimization attitude rather than a statistical usefulness viewpoint. Sometimes in some problems, the singularity is local in the parameter space and a suitable solution can be found by different starting point(s). I've found crude minimizers, like the Nelder-Mead approach, often work better for these problems, but prepare to go have a coffee while it chugs along. However, NM only gives you a set of parameters, with no variability measures like nls. You may be able to use the results as initial info to nls to get more info, but if the singularity is at the solution, you need to think how to explain your results. John Nash>>>>>>>>>From: "Douglas Bates" <bates at stat.wisc.edu> Subject: Re: [R] error in function: nls (urgent) To: tedzzx <zengzhenxing at gmail.com> Cc: r-help at r-project.org Message-ID: <40e66e0b0811191243v192a97bbq215f0e719db95d2d at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 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