Hi I have a data set with two variables "q" and "depth" as follows: q<-c(tapply(weight[Soil=="Jy"], Depth[Soil=="Jy"], mean)). This commns returns 7 "q" values: 0.687900000 0.845550000 0.405416667 0.152766667 0.033100000 0.031400000 0.005183333 The "depth" values are produced using this command whish "depth" shows teh soil depth: depth <--c(5,15,25,35,45,55,65). This return these values for "depth": [1] -5 -15 -25 -35 -45 -55 -65 I have uploaded the plot file in below: http://www.nabble.com/file/p22118160/plot.doc plot.doc Now I want to fit the following decaying exponential model to our data presented in the plot file such that depth is X and q is Y. nreg<-nls(q~a*exp(-b*depth), start=list(a=1, b=0.5)). This command returns the following error: Error in nls(q ~ a * exp(-b * depth), start = list(a = 1, b = 0.5)) : singular gradient However, if I use positive values of depth i.e. [1] 5 15 25 35 45 55 65 then I recieve the following error: Error in numericDeriv(form[[3]], names(ind), env) : Missing value or an infinity produced when evaluating the model Notice that though in the plot depth is drawn on Y axis but in the regression model "depth" in independent. Plot shows the real world of what happne to the crop root growth. Thank you. Saeed Ahmadi -- View this message in context: http://www.nabble.com/An-error-in-fitting-a-non-linear-regression-tp22118160p22118160.html Sent from the R help mailing list archive at Nabble.com.
Hi Saeed, one approach is to try out several initial value combinations for a and b. It often helps to find initial values of the same order of magnitude and of the same sign as the final estimates. To get such initial values, you could linearize the model: lm(log(q) ~ I(-depth)) and supply the estimated coefficients from the linear regression as starting values: nreg <- nls(q ~ a*exp(-b*depth), start = list(a = 0.76168, b = -0.08484)) summary(nreg) Christian
Hi r-help-bounces at r-project.org napsal dne 20.02.2009 11:21:26:> > Hi > > I have a data set with two variables "q" and "depth" as follows: > > q<-c(tapply(weight[Soil=="Jy"], Depth[Soil=="Jy"], mean)). This commns > returns 7 "q" values: > 0.687900000 0.845550000 0.405416667 0.152766667 0.033100000 0.031400000 > 0.005183333 > > The "depth" values are produced using this command whish "depth" showsteh> soil depth: > depth <--c(5,15,25,35,45,55,65). This return these values for "depth": > [1] -5 -15 -25 -35 -45 -55 -65 > > I have uploaded the plot file in below: > > > > http://www.nabble.com/file/p22118160/plot.doc plot.doc > > Now I want to fit the following decaying exponential model to our data > presented in the plot file such that depth is X and q is Y. > nreg<-nls(q~a*exp(-b*depth), start=list(a=1, b=0.5)). This commandreturns> the following error: > Error in nls(q ~ a * exp(-b * depth), start = list(a = 1, b = 0.5)) : > singular gradientWith b=0.05 it will converge, however the model seems to be wrong. depth <--c(5,15,25,35,45,55,65) y<-c(0.687900000, 0.845550000, 0.405416667, 0.152766667, 0.033100000, 0.031400000,0.005183333) plot(depth,y) nreg<-nls(y~a*exp(-b*depth), start=list(a=1, b=0.05)) lines(depth, predict(nreg)) Regards Petr> > However, if I use positive values of depth i.e. > [1] 5 15 25 35 45 55 65 > > then I recieve the following error: > Error in numericDeriv(form[[3]], names(ind), env) : > Missing value or an infinity produced when evaluating the model > > Notice that though in the plot depth is drawn on Y axis but in the > regression model "depth" in independent. Plot shows the real world ofwhat> happne to the crop root growth. > > Thank you. > Saeed Ahmadi > > -- > View this message in context:http://www.nabble.com/An-error-in-fitting-a-non-> linear-regression-tp22118160p22118160.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi, Thank you for the reply and suggestions. I have two questions? 1) If I want to use log, it seems that I have to take log from both sides of the model which will lead to lm(log(q)~log(-depth)). What is tehdifference between this syntax and lm(log(q) ~ I(-depth))? 2) How can I calculate the R-squared of a fitted non linear model? Regards Saeed Christian Ritz-3 wrote:> > Hi Saeed, > > one approach is to try out several initial value combinations for a and b. > > It often helps to find initial values of the same order of magnitude and > of the same sign > as the final estimates. > > To get such initial values, you could linearize the model: > > lm(log(q) ~ I(-depth)) > > > and supply the estimated coefficients from the linear regression as > starting values: > > nreg <- nls(q ~ a*exp(-b*depth), start = list(a = 0.76168, b = -0.08484)) > summary(nreg) > > > Christian > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://www.nabble.com/An-error-in-fitting-a-non-linear-regression-tp22118160p22179525.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 24.02.2009 11:31:22:> > Hi, > > Thank you for the reply and suggestions. > > I have two questions? > 1) If I want to use log, it seems that I have to take log from bothsides of> the model which will lead to lm(log(q)~log(-depth)). What istehdifference> between this syntax and lm(log(q) ~ I(-depth))?If you have y = a*exp(-b*x) then log of this equation is log(y) = log(a) - b * x at least I was told that by my teacher back at school some decades ago that log(exp(x)) = x. You can prove it by log(exp(whatever)) interactively in R Regards Petr> > 2) How can I calculate the R-squared of a fitted non linear model? > > Regards > Saeed > > > Christian Ritz-3 wrote: > > > > Hi Saeed, > > > > one approach is to try out several initial value combinations for aand b.> > > > It often helps to find initial values of the same order of magnitudeand> > of the same sign > > as the final estimates. > > > > To get such initial values, you could linearize the model: > > > > lm(log(q) ~ I(-depth)) > > > > > > and supply the estimated coefficients from the linear regression as > > starting values: > > > > nreg <- nls(q ~ a*exp(-b*depth), start = list(a = 0.76168, b =-0.08484))> > summary(nreg) > > > > > > Christian > > > > ______________________________________________ > > R-help at 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. > > > > > > -- > View this message in context:http://www.nabble.com/An-error-in-fitting-a-non-> linear-regression-tp22118160p22179525.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi Peter, You are totally right and it was a miscalculating and misunderstanding from me. Regarding the R-squared calculation of non linear model (question 2), is there any way to do that? Regards Saeed Petr Pikal wrote:> > Hi > > r-help-bounces at r-project.org napsal dne 24.02.2009 11:31:22: > >> >> Hi, >> >> Thank you for the reply and suggestions. >> >> I have two questions? >> 1) If I want to use log, it seems that I have to take log from both > sides of >> the model which will lead to lm(log(q)~log(-depth)). What is > tehdifference >> between this syntax and lm(log(q) ~ I(-depth))? > > > If you have > > y = a*exp(-b*x) then log of this equation is > > log(y) = log(a) - b * x > > at least I was told that by my teacher back at school some decades ago > that log(exp(x)) = x. > > You can prove it by > > log(exp(whatever)) > > interactively in R > > Regards > Petr > >> >> 2) How can I calculate the R-squared of a fitted non linear model? >> >> Regards >> Saeed >> >> >> Christian Ritz-3 wrote: >> > >> > Hi Saeed, >> > >> > one approach is to try out several initial value combinations for a > and b. >> > >> > It often helps to find initial values of the same order of magnitude > and >> > of the same sign >> > as the final estimates. >> > >> > To get such initial values, you could linearize the model: >> > >> > lm(log(q) ~ I(-depth)) >> > >> > >> > and supply the estimated coefficients from the linear regression as >> > starting values: >> > >> > nreg <- nls(q ~ a*exp(-b*depth), start = list(a = 0.76168, b = > -0.08484)) >> > summary(nreg) >> > >> > >> > Christian >> > >> > ______________________________________________ >> > R-help at 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. >> > >> > >> >> -- >> View this message in context: > http://www.nabble.com/An-error-in-fitting-a-non- >> linear-regression-tp22118160p22179525.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at 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. > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://www.nabble.com/An-error-in-fitting-a-non-linear-regression-tp22118160p22180225.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 24.02.2009 12:41:50:> > Hi Peter, > > You are totally right and it was a miscalculating and misunderstandingfrom> me. > Regarding the R-squared calculation of non linear model (question 2), is > there any way to do that?I am not an expert statistician, Douglas Bates is so see his answer to similar question few years ago http://tolstoy.newcastle.edu.au/R/help/02b/0482.html Regards Petr> > Regards > Saeed > > Petr Pikal wrote: > > > > Hi > > > > r-help-bounces at r-project.org napsal dne 24.02.2009 11:31:22: > > > >> > >> Hi, > >> > >> Thank you for the reply and suggestions. > >> > >> I have two questions? > >> 1) If I want to use log, it seems that I have to take log from both > > sides of > >> the model which will lead to lm(log(q)~log(-depth)). What is > > tehdifference > >> between this syntax and lm(log(q) ~ I(-depth))? > > > > > > If you have > > > > y = a*exp(-b*x) then log of this equation is > > > > log(y) = log(a) - b * x > > > > at least I was told that by my teacher back at school some decades ago> > that log(exp(x)) = x. > > > > You can prove it by > > > > log(exp(whatever)) > > > > interactively in R > > > > Regards > > Petr > > > >> > >> 2) How can I calculate the R-squared of a fitted non linear model? > >> > >> Regards > >> Saeed > >> > >> > >> Christian Ritz-3 wrote: > >> > > >> > Hi Saeed, > >> > > >> > one approach is to try out several initial value combinations for a> > and b. > >> > > >> > It often helps to find initial values of the same order ofmagnitude> > and > >> > of the same sign > >> > as the final estimates. > >> > > >> > To get such initial values, you could linearize the model: > >> > > >> > lm(log(q) ~ I(-depth)) > >> > > >> > > >> > and supply the estimated coefficients from the linear regression as > >> > starting values: > >> > > >> > nreg <- nls(q ~ a*exp(-b*depth), start = list(a = 0.76168, b = > > -0.08484)) > >> > summary(nreg) > >> > > >> > > >> > Christian > >> > > >> > ______________________________________________ > >> > R-help at 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. > >> > > >> > > >> > >> -- > >> View this message in context: > > http://www.nabble.com/An-error-in-fitting-a-non- > >> linear-regression-tp22118160p22179525.html > >> Sent from the R help mailing list archive at Nabble.com. > >> > >> ______________________________________________ > >> R-help at 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. > > > > ______________________________________________ > > R-help at 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. > > > > > > -- > View this message in context:http://www.nabble.com/An-error-in-fitting-a-non-> linear-regression-tp22118160p22180225.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.