[R] Plot for 10 years extrapolation
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Thu Oct 26 22:56:20 CEST 2023
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 using 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 using 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.
More information about the R-help
mailing list