[R] problem in fitting model in NLS function

Rolf Turner rolf.turner at xtra.co.nz
Thu Feb 2 01:34:47 CET 2012


I am no expert on nls() but since I haven't seen any replies to your
post, I'll chip in:

(1) I am mystified as to why nls() is giving that error about a 
"singular" (???)
gradient.

(2) That being said, I think your parameterisation of the objective function
is a bit flaky.  I would use

         a*log(1+exp(b*x - tau)) # Using "x" instead of the Windoze-ese 
"myday".

This is equivalent to your (c,r,tt) parameterisation with

         c = a*b
         r  = b
         tt = tau/b

(3) However I still get that error message from nls() with this new 
parameterisation.

(4) I tried the optimize() function and *that* seems to work without 
complaint.

With my parameterisation  the Nelder-Mead (default) and the BFGS methods 
give
very similar results with a minumum sum of squares equal to 4.909448 and 
4.904986
respectively.

With your parameterisation the Nelder-Mead method gives a minimum sum of 
squares
equal to 4.972705 --- not as good, and the BFGS method (which is more 
like what nls()
uses) gives 219.79 --- right out to lunch.

The plots of the fitted curves for the two fits with my parameterisation 
are visually
indistinguishable and are visually indistiguishable from a straight line 
fit.  (Which raises
the question --- why are you using such a complicated model?

The Nelder-Mead curve from your parameterisation is "close" to those from my
parameterisation, but is definitely different.  The BFGS curve from your 
parameterisation
is off the plot region.

Summary:

     * I have no idea why nls() is throwing an error.
     * Your parameterisation is bad.
     * A better parameterisation can be readily fitted to your data 
using optimize().
     * The model is probably too complicated and inappropriate for these 
data.

HTH

     cheers,

         Rolf Turner

On 01/02/12 03:11, ram basnet wrote:
> Dear R users,
>
> I am struggling to fit expo-linear equation to my data using "nls" function. I am always getting error message as i highlighted below in yellow color:
>
>
>   Theexpo-linear equation which i am interested to fit my data:  
>      response_variable =  (c/r)*log(1+exp(r*(Day-tt))), where "Day" is time-variable
>
> my response variable
>
> rl<- c(2,1.5,1.8,2,2,2.5,2.6,1.5,2.4,1.7,2.3,2.4,2.2,2.6,
>                   2.8,2,2.5,1.8,2.4,2.4,2.3,2.6,3,2,2.6,1.8,2.5,2.5,
>                   2.3,2.7,3,2.2,2.6,1.8,2.5,2.5,2.3,2.7,3,2.2)
> myday<- rep(c(3,5,7,9,10), each = 8) # creating my predictor time-variable
> mydata<- data.frame(rl,myday) # data object
>
> # fitting model equation in "nls" function, when i assigned initial value for tt = 0.6,
>
> CASE-I:
>
>> mytest<- nls(rl ~ (c/r)*log(1+exp(r*(myday-tt))), data = mydata,
> + na.action = na.omit,
> + start = list(c = 2.0, r = 0.05, tt = 0.6),algorithm = "plinear")
> Error in numericDeriv(form[[3L]], names(ind), env) :
>    Missing value or an infinity produced when evaluating the model
>   
> CASE - II:
> When i assigned initial value for tt = 1:
>
>> mytest<- nls(rl ~ (c/r)*log(1+exp(r*(myday-tt))), data = mydata,
> + na.action = na.omit,
> + start = list(c = 2.0, r = 0.5, tt = 1),algorithm = "plinear")
> Error in nls(rl ~ (c/r) * log(1 + exp(r * (myday - tt))), data = mydata,  :
>    singular gradient
>   
> I am getting the yellow-color highlighted error message (see above). Truely speaking, i have not so much experienced with fitting specific model equation in R-package.
> I have following queries:
>   
> 1. Does any one can explain me what is going wrong here ?
>   
> 2. Importantly, how can i write above equation into "nls" functions ? 
>   
> I will be very thankful to you, if any one can help me.
> I am looking for your cooperations.
>
> Thanks



More information about the R-help mailing list