Hi R-community, I have the code as follows,i Fitted model as follows lbeer<-log(beer_monthly) t<-seq(1956,1995.2,length=length(beer_monthly)) #beer_monthly contains 400+ entries t2=t^2 beer_fit_parabola=lm(lbeer~t+t2) Below is not working for me. Please help me in preparing the new data set for the below prediction predict(beer_fit_parabola,newdata=data.frame(t=seq(1995,1998,length=20),t2=seq(1995,1998,length=20)) #it is listing all 400+ entries ,but not 20 ahead prediction. Thanks In advance for your help -- View this message in context: http://r.789695.n4.nabble.com/Help-me-with-prediction-in-linear-model-tp2297313p2297313.html Sent from the R help mailing list archive at Nabble.com.
Hi: On Wed, Jul 21, 2010 at 8:46 AM, vijaysheegi <vijay.sheegi@gmail.com> wrote:> > Hi R-community, > I have the code as follows,i Fitted model as follows > lbeer<-log(beer_monthly) > t<-seq(1956,1995.2,length=length(beer_monthly)) #beer_monthly contains 400+ > entries >This is unnecessary:> t2=t^2 > beer_fit_parabola=lm(lbeer~t+t2) >Try beer_fit_parabola <- lm(lbeer ~ poly(t, 2)) instead. poly() is a safer function to use for polynomial regression. See ?poly for the reasons why.> > Below is not working for me. > Please help me in preparing the new data set for the below prediction > > > tpred <- data.frame(t = seq(1995, 1998, length = 20))predict(beer_fit_parabola, newdata = tpred) If that doesn't work, try putting lbeer and t in a data frame and use it as the data = argument of lm() [same model]. Then try predict() again with the same code as above. This shouldn't be necessary, though. Here's a quick made up example: x <- 1:10 y <- 3 + 2 * x - x^2 + rnorm(10) plot(x, y) mm <- lm(y ~ poly(x, 2)) predict(mm, newdata = data.frame(x = c(11, 12))) 1 2 -96.10648 -117.04534 If you look at summary(mm), you'll see why polynomial models are not very useful for prediction, particularly when extrapolating outside the range of the observed data. (Compare the parameter estimates to the function that produced y as a crude check.) A better choice might be a natural or restricted cubic spline to reduce the standard error of prediction. HTH, Dennis predict(beer_fit_parabola,newdata=data.frame(t=seq(1995,1998,length=20),t2=seq(1995,1998,length=20))> > #it is listing all 400+ entries ,but not 20 ahead prediction. > > Thanks In advance for your help > -- > View this message in context: > http://r.789695.n4.nabble.com/Help-me-with-prediction-in-linear-model-tp2297313p2297313.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. >[[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 21.07.2010 17:46:02:> > Hi R-community, > I have the code as follows,i Fitted model as follows > lbeer<-log(beer_monthly) > t<-seq(1956,1995.2,length=length(beer_monthly)) #beer_monthly contains400+> entriesnot needed> t2=t^2> beer_fit_parabola=lm(lbeer~t+t2)beer_fit_parabola=lm(lbeer~t+I(t^2)) you can use poly(t,2) but coefficients are not directly applicable in equation.> > Below is not working for me. > Please help me in preparing the new data set for the below prediction > > > >predict(beer_fit_parabola,newdata=data.frame(t=seq(1995,1998,length=20),t2=seq> (1995,1998,length=20))predict(beer_fit_parabola,newdata=data.frame(t=seq(1995,1998,length=20)) shall be enough Regards Petr> > #it is listing all 400+ entries ,but not 20 ahead prediction. > > Thanks In advance for your help > -- > View this message in context:http://r.789695.n4.nabble.com/Help-me-with-> prediction-in-linear-model-tp2297313p2297313.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.
Thanks Murphy and pikal, I need another help,for fitting first fourier transformation ,i used following thing .Please advise on this beer_monthl has 400+ records EXample:> head(beer_monthly)beer 1 93.2 2 96.0 3 95.2 4 77.1 5 70.9 6 64.8 time<-seq(1956,1995.2,length=length(beer_monthly)) sin.t<-sin(2*pi*time) cos.t<-cos(2*pi*time) beer_fit_fourier=lm(beer_monthly[,1]~poly(time,2)+sin.t+cos.t) #this is not working beer_fit_fourier=lm(beer_monthly[,1]~time+time2+sin.t+cos.t) #it is working #prediction is not workinng tpred_four <- data.frame(time = seq(1995, 1998, length = 20)) predict(beer_fit_fourier, newdata = tpred_four) Is there any way to fit first fourier frequency , Please assist. Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Help-me-with-prediction-in-linear-model-tp2297313p2300991.html Sent from the R help mailing list archive at Nabble.com.