Hello there, I am trying to fit an exponential model using nls to some data. #data t <- c(0,15,30,60,90,120,240,360,480) var <- c(0.36,9.72,15.50,23.50,31.44,40.66,59.81,73.11,81.65) df <- data.frame(t, var) # model # var ~ a+b*(1-exp(-k*t)) # I'm looking for values of a,b and k # formula # mod <- nls(formula = var ~ a+b *(1-exp((-k)*t)), start=list(a=0, b=10, k=0), trace=T) # It fails and I get the following error - Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates I was trying different methods and looking for some solutions but nothing is working... Any suggestions how I can fix this? I appreciated any help Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/fitting-an-exp-model-tp4310752p4310752.html Sent from the R help mailing list archive at Nabble.com.
arivald <rzepus.m <at> gmail.com> writes:> I am trying to fit an exponential model using nls to some data. > > #data > t <- c(0,15,30,60,90,120,240,360,480) > var <- c(0.36,9.72,15.50,23.50,31.44,40.66,59.81,73.11,81.65) > df <- data.frame(t, var) > > # model > # var ~ a+b*(1-exp(-k*t)) > > # I'm looking for values of a,b and k > > # formula > # mod <- nls(formula = var ~ a+b *(1-exp((-k)*t)), start=list(a=0, b=10, > k=0), trace=T) > > # It fails and I get the following error - > Error in nlsModel(formula, mf, start, wts) : > singular gradient matrix at initial parameter estimates >Try different starting values, especially with k>0 ? mod2 <- nls(formula = var ~ a+b *(1-exp((-k)*t)), start=list(a=0, b=10,k=0.01), trace=TRUE) Alternately you can try a self-starting model (see ?SSasymp), although the parameterization is a little different. mod3 <- nls(formula = var ~ SSasymp(t,b,a,lrc), trace=TRUE) coef(mod2) coef(mod3) with(as.list(coef(mod3)),c(a=a,b=b-a,k=exp(lrc)))
On 20/01/12 06:38, arivald wrote:> Hello there, > > I am trying to fit an exponential model using nls to some data. > > #data > t<- c(0,15,30,60,90,120,240,360,480) > var<- c(0.36,9.72,15.50,23.50,31.44,40.66,59.81,73.11,81.65) > df<- data.frame(t, var) > > # model > # var ~ a+b*(1-exp(-k*t)) > > # I'm looking for values of a,b and k > > # formula > # mod<- nls(formula = var ~ a+b *(1-exp((-k)*t)), start=list(a=0, b=10, > k=0), trace=T) > > # It fails and I get the following error - > Error in nlsModel(formula, mf, start, wts) : > singular gradient matrix at initial parameter estimates > > I was trying different methods and looking for some solutions but nothing is > working... > > Any suggestions how I can fix this? I appreciated any help > Thanks in advance.Try different starting values. Yours are out to lunch. Look at: plot(function(t){10*(1-exp(-t))},xlim=c(0,500),xlab="t",ylab="var") This goes to hell in a handcart at about t=6 and looks nothing like plot(t,var) I found that mod <- nls(formula = var ~ a + b *(1-exp((-k)*t)), start=list(a=0,b=80, k=0.01)) works OK. Then doing ccc <- coef(mod) plot(function(t){ccc[1] +ccc[2]*(1-exp(-ccc[3]*t))},xlim=c(0,500),xlab="t",ylab="var") points(t,var) indicates a fairly reasonable fit. One wonders however about the validity of the model considering the lack of fit at t=0. R has great and easy-to-use graphics capabilities. Use them! cheers, Rolf Turner