Hi Chris, Curve fitting has nothing to do with statistics (even though it is used in statistics). To get an idea of this, try the following:
x <- ((-100):100)/100 cofs<-rnorm(4) #create coefficients y <- cofs[1] + cofs[2]*x + cofs[3]*x^2 +cofs[4]*x^3 y1 <- y +rnorm(201,0,0.1) #add noise mm <- lm(y1~poly(x,3,raw=TRUE)) #fit a polynomial of degree 3 y2 <- predict(mm,as.data.frame(x)) #compute the polynomial for every point of x plot(x,y,type="l");lines(x,y1,col="red");lines(x,y2,col="blue") cofs mm$coefficients For the exponential fit, there exist two options: you are trying to fit y = exp(a*x+b) one possibility is to fit log(y) = a*x+b by mm <- lm(log(y)~x) and the other (more "correct") one is to use any of the least squares packages. I believe that you better read a little bit about curve fitting before doing all this. Regards, Moshe. --- On Fri, 8/1/10, chrisli1223 <chri...@austwaterenv.com.au> wrote: > From: chrisli1223 <chri...@austwaterenv.com.au> > Subject: Re: [R] Polynomial equation > To: r-help@r-project.org > Received: Friday, 8 January, 2010, 2:14 PM > > Thank you very much for your help. Greatly appreciated! > > However, due to my limited stats background, I am unable to > find out the > equation of the trendline from the summary table. Besides, > how do I fit the > trendline on the graph? > > I intend to put the first column of data onto x axis and > the second column > onto y axis. Are they the x and y in your example? > > Many thanks, > Chris > > > Moshe Olshansky-2 wrote: > > > > Hi Chris, > > > > You can use lm with poly (look ?lm, ?poly). > > If x and y are your arrays of points and you wish to > fit a polynom of > > degree 4, say, enter: model <- > lm(y~poly(x,4,raw=TRUE) and then > > summary(model) > > The raw=TRUE causes poly to use 1,x,x^2,x^3,... > instead of orthogonal > > polynomials (which are "better" numerically but may be > not what you need). > > > > Regards, > > Moshe. > > > > --- On Fri, 8/1/10, chrisli1223 <chri...@austwaterenv.com.au> > wrote: > > > >> From: chrisli1223 <chri...@austwaterenv.com.au> > >> Subject: [R] Polynomial equation > >> To: r-help@r-project.org > >> Received: Friday, 8 January, 2010, 12:32 PM > >> > >> Hi all, > >> > >> I have got a dataset. In Excel, I can fit a > polynomial > >> trend line > >> beautifully. However, the equation that Excel > calculates > >> appear to be > >> incorrect. So I am thinking about using R. > >> > >> My questions are: > >> (1) How do I fit a polynomial trendline to a > graph? > >> (2) How do I calculate and display the equation > for that > >> trendline? > >> > >> Many thanks for your help. Greatly appreciated. > >> > >> Chris > >> -- > >> View this message in context: > >> http://n4.nabble.com/Polynomial-equation-tp1009398p1009398.html > >> Sent from the R help mailing list archive at > Nabble.com. > >> > >> ______________________________________________ > >> 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. > >> > > > > ______________________________________________ > > 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. > > > > > > -- > View this message in context: > http://n4.nabble.com/Polynomial-equation-tp1009398p1009438.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > ______________________________________________ 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.