Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Funggcast Function to Plot Time Series and Predicted Series With Forecast on the Same Ggplot

I have this time series data which I want to plot its series, its predicted series, and its forecast on the same ggplot. I got this function here that demonstrates what I want but I have a date Issue.

#----------Simulate an Arima (3, 1, 4) Process-------------
library(forecast)

y <- arima.sim(model=list(order=c(3, 1, 4),ar=c(0.7662, 0.8768, - 0.7183), ma = c(- 0.0256, - 0.9333, 0.0041, - 0.0207)),n = 42)
y <- ts(y,freq=1,start=c(1980))

#-- Extract Training Data, Fit the Wrong Model, and Forecast
yt<-window(y,end=2020)

yfit<-Arima(yt,order=c(3, 1, 4))

yfor<-forecast(yfit)

#---Extract the Data for ggplot using funggcast()
funggcast <- function(dn, fcast){
  
  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
  
  # Extract the Forecast values and confidence intervals
  dfcastn <- as.data.frame(fcast)
  dfcastn$date <- as.Date(paste(row.names(dfcastn),"01","01",sep="-"))
  names(dfcastn) <- c('forecast','lo80','hi80','lo95','hi95','date')
  
  pd <- merge(ds, dfcastn,all= T) # final data.frame for use in ggplot
  return(pd)
  
} 
pd<-funggcast(y,yfor)

#---Plot in ggplot2 
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)')

I get this Error Message:

pd<-funggcast(y,yfor)
Error in as.Date.default(time(window(dn, end = en))) :
do not know how to convert ‘time(window(dn, end = en))’ to class “Date”

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You get the error because you did not load zoo package, I removed opts in ggplot:

library(forecast)
library(zoo)

y <- arima.sim(model=list(order=c(3, 1, 4),ar=c(0.7662, 0.8768, - 0.7183), ma = c(- 0.0256, - 0.9333, 0.0041, - 0.0207)),n = 42)
y <- ts(y,freq=1,start=c(1980))
               

#-- Extract Training Data, Fit the Wrong Model, and Forecast
yt<-window(y,end=2020)

yfit<-Arima(yt,order=c(3, 1, 4))

yfor<-forecast(yfit)

#---Extract the Data for ggplot using funggcast()
funggcast <- function(dn, fcast){
  
  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
  
  # Extract the Forecast values and confidence intervals
  dfcastn <- as.data.frame(fcast)
  dfcastn$date <- as.Date(paste(row.names(dfcastn),"01","01",sep="-"))
  names(dfcastn) <- c('forecast','lo80','hi80','lo95','hi95','date')
  
  pd <- merge(ds, dfcastn,all= T) # final data.frame for use in ggplot
  return(pd)
  
} 

pd<-funggcast(y,yfor)

#---Plot in ggplot2 
library(ggplot2)
library(scales)

ggplot(data=pd,aes(x=date,y=observed)) +
  geom_line(col='red')+
  geom_line(aes(y=fitted),col='blue')+
  geom_line(aes(y=forecast))+geom_ribbon(aes(ymin=lo95,ymax=hi95),alpha=.25)+
  scale_x_date(date_labels = "%b-%y",date_breaks = "1 year",date_minor_breaks = "1 month")+
  scale_y_continuous(name='Units of Y')+
  theme(axis.text.x=element_text(angle = 45, vjust = 0.5, hjust=1),
        plot.title=element_text(hjust=0.5))+
  ggtitle('Arima Fit to Simulated Data\n (black=forecast, blue=fitted, red=data, shadow=95% conf. interval)')

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading