Re: [R] Plot for 10 years extrapolation
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 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@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 __ 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.
Re: [R] Plot for 10 years extrapolation
À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@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 __ 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.
Re: [R] Plot for 10 years extrapolation
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 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 > 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.
Re: [R] Plot for 10 years extrapolation
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 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.
[R] Plot for 10 years extrapolation
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.