Ram

These errors often show up with starting values that are poor and wander 
into 'illegal' territory (divide by zero, exp(large number), 
log(negative or zero), etc. Consider putting constraints on the 
parameters (?nls will guide you) and working harder to get starting 
values by plotting the data, looking at subsets, literature, etc. I 
modified your setup to use the 'port' algorithm where constraints are 
allowed and got a better result.  It appears as though your starting 
values are poor.

mytest <- nls(rl ~ c/r*log(1+exp(r*(myday-tt))), data = mydata,trace=T,
   start = list(c = 2.0, r = 0.05, tt = 0.6),algorithm = "port",
   lower=c(.001,.001,.001),upper=c(5,1,2))

  iter       RSS          c       r         tt
   0:     20858.152:  2.00000 0.0500000 0.600000
   1:     8338.1365:  1.66585 0.0633944  2.00000
   2:     612.00623: 0.713178 0.0849172  2.00000
   3:     15.206335: 0.120166 0.0696765  2.00000
   4:     3.3908882: 0.149522 0.0529488 0.00100000
   5:     2.4569410: 0.109027 0.0397051 0.00100000
   6:     2.4547394: 0.107493 0.0388038 0.00100000
   7:     2.4547363: 0.107512 0.0388200 0.00100000
   8:     2.4547363: 0.107512 0.0388199 0.00100000  (constraint!)

The first column is the residual sum of squares (objective function) to 
be minimized. The last three are c, r, and tt. It appears tt wants to be 
a (-) number since it hits the constraint. When I used -40 as the lower 
bound on tt, the algorithm hit -30.9 and stopped because it wouldn't 
converge. This tells the tale. The model doesn't appear to represent 
your data well (not uncommon) and you have too many parameters for the 
information in your data. (Look at the data plot). Remember Occam's Razor.

Eliminating tt,

mytest2 <- nls(rl ~ cp/rp*log(1+exp(rp*(myday))), data = mydata,trace=T,
   start = list(cp = 2.0, rp = 0.05),algorithm = "port",
   lower=c(.001,.001),upper=c(5,1))

Converged nicely, away from the constraints

  iter        RSS       c         r
   0:     21768.060:  2.00000 0.0500000
   1:     7852.9156:  1.55056 0.0669735
   2:     11.579059: 0.111530 0.0637943
   3:     2.7905618: 0.142624 0.0521830
   4:     2.4561315: 0.111674 0.0405712
   5:     2.4547433: 0.107484 0.0387969
   6:     2.4547362: 0.107510 0.0388206
   7:     2.4547362: 0.107510 0.0388203

Simpler is better, no. Though, your data are noisy. Kudos for 
replication though. Let us know how it works out.

David Stevens

On 1/31/2012 7:11 AM, 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
>
>
> Regards,
> Ram Kumar Basnet
> 	[[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> 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.

-- 
David K Stevens, P.E., Ph.D., Professor
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
david.stevens@usu.edu




	[[alternative HTML version deleted]]

