Incidentally, if all you wanted to do was plot fitted values, the predict method is kinda overkill, as it's just the fitted line from the model. But I assume you wanted to plot CI's/PI's also, as the example illustrated.
-- Bert On Thu, Oct 26, 2023 at 1:56 PM Bert Gunter <bgunter.4...@gmail.com> wrote: > > from ?predict.lm: > > "predict.lm produces a vector of predictions or a matrix of > predictions and bounds with column names fit, lwr, and upr if interval > is set. " > > ergo: > predict(model, dfuture, interval = "prediction")[,"fit"] ## or [,1] > as it's the first column in the returned matrix > > is your vector of predicted values that you can plot against > dfuture$date however you would like, e.g. with different colors, > symbols, or whatever. Exactly how you do this depends on what graphics > package you are using. The example in ?predict.lm shows you how to do > it with R's base graphics and overlaying prediction and confidence > intervals. > > Cheers, > Bert > > On Thu, Oct 26, 2023 at 11:27 AM varin sacha via R-help > <r-help@r-project.org> wrote: > > > > Dear R-Experts, > > > > Here below my R code working but I don't know how to complete/finish my R > > code to get the final plot with the extrapolation for the10 more years. > > > > Indeed, I try to extrapolate my data with a linear fit over the next 10 > > years. So I create a date sequence for the next 10 years and store as a > > dataframe to make the prediction possible. > > Now, I am trying to get the plot with the actual data (from year 2004 to > > 2018) and with the 10 more years extrapolation. > > > > Thanks for your help. > > > > #################################################### > > date <-as.Date(c("2018-12-31", "2017-12-31", "2016-12-31", "2015-12-31", > > "2014-12-31", "2013-12-31", "2012-12-31", "2011-12-31", "2010-12-31", > > "2009-12-31", "2008-12-31", "2007-12-31", "2006-12-31", "2005-12-31", > > "2004-12-31")) > > > > value <-c(15348, 13136, 11733, 10737, 15674, 11098, 13721, 13209, 11099, > > 10087, 14987, 11098, 13421, 9023, 12098) > > > > model <- lm(value~date) > > > > plot(value~date ,col="grey",pch=20,cex=1.5,main="Plot") > > abline(model,col="darkorange",lwd=2) > > > > dfuture <- data.frame(date=seq(as.Date("2019-12-31"), by="1 year", > > length.out=10)) > > > > predict(model,dfuture,interval="prediction") > > ######################################################## > > > > ______________________________________________ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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 -- To UNSUBSCRIBE and more, see 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.