Navigation
« Plotting forecast() objects in ggplot part 2: Visualize Observations, Fits, and Forecasts | Main | Property rights and the economic origins of the Sicilian mafia »
Wednesday
Mar142012

Plotting forecast() objects in ggplot part 1: Extracting the Data

Lately I've been using Rob J Hyndman's excellent forecast package. The package comes with some built in plotting functions but I found I wanted to customize and make my own plots in ggplot. In order to do that, I need a generalizable function that will extract all the data I want (forecasts, fitted values, training data, actual observations in the forecast period, confidence intervals, et cetera) and place it into a data.frame with a properly formatted date field (ie, not  a ts() object).

The function below does all that and should work for any forecast object (though I've only tested it on Arima() outputs). The only arguments it takes are the original observations and the forecast object (whatever results from calling forecast()). In my next post I'll give some examples of plotting the results using ggplot and explain why I wanted more than the default plot.forecast() function.

 

#--Produces a data.frame with the Source Data+Training Data, Fitted Values+Forecast Values, forecast data Confidence Intervals
funggcast<-function(dn,fcast){ 
	require(zoo) #needed for the 'as.yearmon()' function
 
	en<-max(time(fcast$mean)) #extract the max date used in the forecast
 
	#Extract Source and Training Data
	ds<-as.data.frame(window(dn,end=en))
	names(ds)<-'observed'
	ds$date<-as.Date(time(window(dn,end=en)))
 
	#Extract the Fitted Values (need to figure out how to grab confidence intervals)
	dfit<-as.data.frame(fcast$fitted)
	dfit$date<-as.Date(time(fcast$fitted))
	names(dfit)[1]<-'fitted'
 
	ds<-merge(ds,dfit,all.x=T) #Merge fitted values with source and training data
 
	#Exract the Forecast values and confidence intervals
	dfcastn<-as.data.frame(fcast)
	dfcastn$date<-as.Date(as.yearmon(row.names(dfcastn)))
	names(dfcastn)<-c('forecast','lo80','hi80','lo95','hi95','date')
 
	pd<-merge(ds,dfcastn,all.x=T) #final data.frame for use in ggplot
	return(pd)
 
}

Created by Pretty R at inside-R.org

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (28)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
  • Response
    Response: Pascher Shop
    Endesa League: 7 days nine notes Classification: Information, Notes, Con Mens Work Boots Are A Necessity cheap pants for men- tests, Liga Endesa, newgorras Endesa Group,Mens Work Boots Are A Necessity cheap pants for men- , Perspective Express Endesa Little league Spherical 9: synopsis. We have reached the cen ...
  • Response
    Digitala Analytics är en uppsättning av affärs-och tekniska verksamheter som definiera, skapa, samla in, kontrollera eller omvandla digitala data till rapportering, forskning, analyser, rekommendationer, optimeringar, prognoser och automatiseringar
  • Response
    Response: book editing rates
    Recently noticed some mass anomaly that meteorologists are very often wrong in their predictions about the weather! Previously, it was authentic, and it is not clear what happened!
  • Response
    Response: Fine ARt
    I think your article is really cool and great i like this thanks for posting.
  • Response
    Response: Click here
    Dichon Hat
  • Response
    SAXO盛宝金融 亚洲外汇网 :等待戈多 来源:亚洲外汇网 作 盛宝金融 者:盛宝金融 SteenJakobsen写到,资产市场在第三季度表现良好,但这也将标志着"延期并假装不知"心态的终结。 股票市场在第二季度和第三季度初期持续回升、一路高涨,此时引力定律和投资理智已失去作用。但第三季度接下来会出现什么新情 盛宝公司债券网上交易 况?或者我们在等待市场回归现实和常态吗,就像爱斯特拉冈和弗拉第米尔(萨缪尔?贝克的戏剧人物)对戈多的徒劳等待? 在今天的世界,我们等待的戈多就是一声起床号,即让世界经
  • Response
    Never is not well versed in all sorts of functions, always trying to avoid them away! I hope that there should be nothing extraordinary in the conduct of only one formula!
  • Response
    - Frank Davenport's Research Blog - Plotting forecast() objects in ggplot part 1: Extracting the Data
  • Response
    - Frank Davenport's Research Blog - Plotting forecast() objects in ggplot part 1: Extracting the Data
  • Response
    - Frank Davenport's Research Blog - Plotting forecast() objects in ggplot part 1: Extracting the Data
  • Response
    - Frank Davenport's Research Blog - Plotting forecast() objects in ggplot part 1: Extracting the Data
  • Response
    - Frank Davenport's Research Blog - Plotting forecast() objects in ggplot part 1: Extracting the Data
  • Response
    I like your all essay writing tips and content writing tips. It is so much important to our future education and future essay writing service life. This is much useful to our essay writing knowledge.
  • Response
    Response: freedom apk free
  • Response
    While the thing that is needed to be understood is if money will be anything which is so important in this world than what will be the purpose of living and existence of human being. The need is we have to understand our importance in this world.
  • Response
    Response: 840压瓦机
    彩钢瓦生产设备 彩钢瓦生产设备机械厂家联系人 不要为旧的悲伤而浪费新的眼泪写的真好 手机:13832771638王女士 祁先生15133711101 QQ:59458991 座机:0317-8086188 传真:0317-8086188 剪切机 ...
  • Response
    Response: movie hd app
  • Response
    Response: movietube app
  • Response
    Response: movietube-app
  • Response
    вконтакте
  • Response
    Response: ShowBox App
  • Response
    Response: MovieTube
    Exceptional information
  • Response
    Exciting and Fascinating post. Awesome Tips.
  • Response
  • Response
  • Response
    Response: Clash Royale APK
  • Response

Reader Comments (6)

Thanks for showing how to do this Frank. Just a couple of comments. You shouldn't need to pass the original observations as they are stored as component x in the forecast object. Also, if the forecast object contains other prediction intervals than 80 and 95% intervals, this function will cause an error. Finally, rather than use as.Date() on the row names, it would be simpler (and probably more robust) to use times() on the mean component.

March 22, 2012 | Unregistered CommenterRob J Hyndman

Hi Rob-

Thanks for the feedback! The idea behind including the original observations was that they also contain the observations during the forecast period. I suppose to make it simpler though I could just have the user pass the those observations rather than the whole series.

March 23, 2012 | Unregistered CommenterFrank Davenport

Hello Rob, i'm using ur function via R 3.1 on Windows 8 64 bits

Your function is very helpful since i'm also conducting the ARIMA forecast
however, it seems that there's some bug in the function ?

after running with my time series data
this function left the "NA" in all forecast value

so i changed pd<-merge(ds,dfcastn,all.x=T)
to pd<-merge(ds,dfcastn,all=TRUE)

and all work fine

Here is the debugged function

funggcast<-function(dn,fcast){
require(zoo) #needed for the 'as.yearmon()' function



en<-max(time(fcast$mean)) #extract the max date used in the forecast

#Extract Source and Training Data
ds<-as.data.frame(window(dn,end=en))
names(ds)<-'observed'
ds$date<-as.Date(time(window(dn,end=en)))

#Extract the Fitted Values (need to figure out how to grab confidence intervals)
dfit<-as.data.frame(fcast$fitted)
dfit$date<-as.Date(time(fcast$fitted))
names(dfit)[1]<-'fitted'

ds<-merge(ds,dfit,all.x=T) #Merge fitted values with source and training data

#Exract the Forecast values and confidence intervals
dfcastn<-as.data.frame(fcast)



dfcastn$date<-as.Date(as.yearmon(row.names(dfcastn)))




names(dfcastn)<-c('forecast','lo80','hi80','lo95','hi95','date')


#pd<-merge(ds,dfcastn,all.x=T) #final data.frame for use in ggplot

#Changed
pd<-merge(ds,dfcastn,all=TRUE)
return(pd)


}


June 12, 2014 | Unregistered CommenterRaynus

Thanks a lot for the great content here. If you are looking for some writing services, make sure to check this best writing services review here. It will help you avoid fake & scam services out there. Enjoy.

May 1, 2017 | Unregistered CommenterJaredDeans

Hello Frank,

With some extra research, i think it's easier to use autoplot as it's a member of ggplot2. It gives you ggplot2 compatible plot and you could customize the plot as much as ggplot2 allows.

Cheers,
Islam

June 23, 2017 | Unregistered CommenterIslam Ibrahim

That is good to work at such a good company. I am pretty sure of its success. I must be going.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>