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))) ______________________________________________ R-help@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.