[R] Plot for 10 years extrapolation

varin sacha v@r|n@@ch@ @end|ng |rom y@hoo@|r
Sat Oct 28 21:59:57 CEST 2023


Dear Rui,

I really thank you a lot for your precious R help. It is exactly what I was trying to do! Once more, many thanks!

Best,
Sacha






Le vendredi 27 octobre 2023 à 09:36:18 UTC+2, Rui Barradas <ruipbarradas using sapo.pt> a écrit : 





Às 19:23 de 26/10/2023, varin sacha via R-help escreveu:
> 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.
Hello,

Here is a way with base R graphics. Explained in the code comments.



####################################################
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)

dfuture <- data.frame(date = seq(as.Date("2019-12-31"), by="1 year", 
length.out=10))

########################################################

predfuture <- predict(model, dfuture, interval="prediction")
dfuture <- cbind(dfuture, predfuture)

# start the plot with the required x and y limits
xlim <- range(c(date, dfuture$date))
ylim <- range(c(value, dfuture$fit))

plot(value ~ date, col="grey", pch=20, cex=1.5, main="Plot"
      , xlim = xlim, ylim = ylim)

# abline extends the fitted line past the x value (date)
# limit making the next ten years line ugly and not even
# completely overplotting the abline drawn line
abline(model, col="darkorange", lwd=2)
lines(fit ~ date, dfuture
      # , lty = "dashed"
      , lwd=2
      , col = "black")

# if lines() is used for both the interpolated and extrapolated
# values you will have a gap between both fitted and predicted lines
# but it is closer to what you want

# get the fitted values first (interpolated values)
ypred <- predict(model)


plot(value ~ date, col="grey", pch=20, cex=1.5, main="Plot"

      , xlim = xlim, ylim = ylim)

# plot the interpolated values
lines(ypred ~ date, col="darkorange", lwd = 2)
# and now the extrapolated values
# I use normal orange to make the difference more obvious
lines(fit ~ date, dfuture, lty = "dashed", lwd=2, col = "orange")



Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list