Site icon R-bloggers

Plotting forecast() objects in ggplot part 2: Visualize Observations, Fits, and Forecasts

[This article was first published on Frank Davenport's Blog on R, Statistics, and all Things Spatial - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In my last post I presented a function for extracting data from a forecast() object and formatting the data so that it can be plotted in ggplot.  The scenario is that you are fitting a model to a time series object with training data, then forecasting out, and then visually evaluating the fit with the observations that your forecast tried to duplicate. Then you want a plot that includes: the original observations, the fitted values, the forecast values, and the observations in the forecast period. The function I presented in the last post extracts all that information in a nice ggplot ready data.frame. In this post I simulate data from an Arima process, fit an incorrect model, use the function from the last post to extract the data, and then plot in ggplot.

 

#----------Simulate an Arima (2,1,1) Process-------------
library(forecast)
 
y<-arima.sim(model=list(order=c(2,1,1),ar=c(0.5,.3),ma=0.3),n=144)
y<-ts(y,freq=12,start=c(2000,1))
 
#-- Extract Training Data, Fit the Wrong Model, and Forecast
yt<-window(y,end=2009.99)
 
yfit<-Arima(yt,order=c(1,0,1))
 
yfor<-forecast(yfit)
 
#---Extract the Data for ggplot using funggcast()
pd<-funggcast(y,yfor)
 
#---Plot in ggplot2 0.9
library(ggplot2)
library(scales)
 
 
p1a<-ggplot(data=pd,aes(x=date,y=observed)) 
p1a<-p1a+geom_line(col='red')
p1a<-p1a+geom_line(aes(y=fitted),col='blue')
p1a<-p1a+geom_line(aes(y=forecast))+geom_ribbon(aes(ymin=lo95,ymax=hi95),alpha=.25)
p1a<-p1a+scale_x_date(name='',breaks='1 year',minor_breaks='1 month',labels=date_format("%b-%y"),expand=c(0,0))
p1a<-p1a+scale_y_continuous(name='Units of Y')
p1a<-p1a+opts(axis.text.x=theme_text(size=10),title='Arima Fit to Simulated Data\n (black=forecast, blue=fitted, red=data, shadow=95% conf. interval)')
#p1a

 

 

To leave a comment for the author, please follow the link and comment on their blog: Frank Davenport's Blog on R, Statistics, and all Things Spatial - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.