[R] fitting an exp model

Rolf Turner rolf.turner at xtra.co.nz
Thu Jan 19 20:56:45 CET 2012


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



More information about the R-help mailing list