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]]
can someone tell me if there is an easier way to do this in R - create a design matrix? thanks. —> design <- model.matrix(~ -1+factor(c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5))) # Creates an appropriate design matrix can someone tell me if there is an easier way to do this matrix? thanks. [[alternative HTML version deleted]]
Which part? This might help: rep(1:5, each=9) On Tue, Jan 31, 2012 at 1:22 PM, Daniel Negusse <daniel.negusse at my.mcphs.edu> wrote:> ?can someone tell me if there is an easier way to do this in R - create a design matrix? thanks. > ?> design <- model.matrix(~ -1+factor(c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5))) # Creates an appropriate design matrix > can someone tell me if there is an easier way to do this matrix? > thanks.-- Sarah Goslee http://www.functionaldiversity.org
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]]
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