jpm miao
2013-Nov-28 07:50 UTC
[R] Find the prediction or the fitted values for an lm model
Hi, I would like to fit my data with a 4th order polynomial. Now I have only 5 data point, I should have a polynomial that exactly pass the five point Then I would like to compute the "fitted" or "predict" value with a relatively large x dataset. How can I do it? BTW, I thought the model "prodfn" should pass by (0,0), but I just wonder why the const is unequal to zero x1<-c(0,3,4,5,8) y1<-c(0,1,4,7,8) prodfn<-lm(y1 ~ poly(x1, 4)) x<-seq(0,8,0.01) temp<-predict(prodfn,data.frame(x=x)) # This line does not work..> prodfnCall: lm(formula = y1 ~ poly(x1, 4)) Coefficients: (Intercept) poly(x1, 4)1 poly(x1, 4)2 poly(x1, 4)3 poly(x1, 4)4 4.000e+00 6.517e+00 -4.918e-16 -2.744e+00 -8.882e-16 [[alternative HTML version deleted]]
Achim Zeileis
2013-Nov-28 08:17 UTC
[R] Find the prediction or the fitted values for an lm model
On Thu, 28 Nov 2013, jpm miao wrote:> Hi, > > I would like to fit my data with a 4th order polynomial. Now I have only > 5 data point, I should have a polynomial that exactly pass the five point > > Then I would like to compute the "fitted" or "predict" value with a > relatively large x dataset. How can I do it? > > BTW, I thought the model "prodfn" should pass by (0,0), but I just > wonder why the const is unequal to zero > > x1<-c(0,3,4,5,8) > y1<-c(0,1,4,7,8) > prodfn<-lm(y1 ~ poly(x1, 4)) > > x<-seq(0,8,0.01) > > temp<-predict(prodfn,data.frame(x=x)) # This line does not work..You need to call the variable x1 because that is the name you used in the original data: plot(x, predict(prodfn,data.frame(x1=x)), type = "l") points(x1, y1)> >> prodfn > > Call: > lm(formula = y1 ~ poly(x1, 4)) > > Coefficients: > (Intercept) poly(x1, 4)1 poly(x1, 4)2 poly(x1, 4)3 poly(x1, 4)4 > 4.000e+00 6.517e+00 -4.918e-16 -2.744e+00 -8.882e-16 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Rolf Turner
2013-Nov-28 08:36 UTC
[R] Find the prediction or the fitted values for an lm model
See in-line below. On 11/28/13 20:50, jpm miao wrote:> Hi, > > I would like to fit my data with a 4th order polynomial. Now I have only > 5 data point, I should have a polynomial that exactly pass the five point > > Then I would like to compute the "fitted" or "predict" value with a > relatively large x dataset. How can I do it? > > BTW, I thought the model "prodfn" should pass by (0,0), but I just > wonder why the const is unequal to zeroBecause poly() produces orthonormalized polynomials, Look at poly(x1,4). It is not much like cbind(x1,x1^2,x1^3,x1^4), is it? cheers, Rolf Turner> > x1<-c(0,3,4,5,8) > y1<-c(0,1,4,7,8) > prodfn<-lm(y1 ~ poly(x1, 4)) > > x<-seq(0,8,0.01) > > temp<-predict(prodfn,data.frame(x=x)) # This line does not work.. > > >> prodfn > Call: > lm(formula = y1 ~ poly(x1, 4)) > > Coefficients: > (Intercept) poly(x1, 4)1 poly(x1, 4)2 poly(x1, 4)3 poly(x1, 4)4 > 4.000e+00 6.517e+00 -4.918e-16 -2.744e+00 -8.882e-16 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.