Eric Fail Eric Fail - 1 month ago 9
R Question

transform "mFilter" object (list of Time-Series) to plot with ggplot2

I'm working with the

hpfilter
from the
mFilter
package and I can't seem to find a simple way to convert the list of Time-Series objects by
hpfilter
to a format I can use with ggplot2. I realize I can take it all apart and put it back together, but I imagine there's some simple way I have overlooked? I tried the code suggested in the SO discussion R list to data frame. However I couldn't find a way to convert the list of Time-Series objects to a
data.frame
in any simple way. The final goal is to reproduce the default plot produced by the
mFilter
object (see below)

Here's some example code

# install.packages(c("mFilter"), dependencies = TRUE)
library(mFilter)
data(unemp)
unemp.hp <- hpfilter(unemp, type=c("lambda"), freq = 1606)
# str(unemp.hp)
class(unemp.hp)
# [1] "mFilter"
plot(unemp.hp)
Hit <Return> to see next plot:


Also, why am I asked to " Hit
<Return>
" to see the plot?

default plot produced by the <code>mFilter</code> object

Answer

The plot function calls plot.mFilter which has parameter ask=interactive() and it is set as TRUE for interactive sessions, you could disable this by ask=FALSE in call for plot

plot(unemp.hp,ask=FALSE)

Data:

library(mFilter)
library(ggplot2)
library(gridExtra)
# library(zoo)


data(unemp)
unemp.hp <- hpfilter(unemp, type=c("lambda"), freq = 1606)
# str(unemp.hp)
class(unemp.hp)
# [1] "mFilter"
plot(unemp.hp,ask=FALSE)

To check for slots of object unemp.hp

names(unemp.hp)
# [1] "cycle"   "trend"   "fmatrix" "title"   "xname"   "call"    "type"    "lambda"  "method" 
#[10] "x"

The relevant objects are x (the main unemp series) , trend and cycle. All three objects are of class ts, we first convert them to data.frame using custom function and plot using ggplot and gridExtra (for grid.arrange)

objectList = list(unemp.hp$x,unemp.hp$trend,unemp.hp$cycle)
names(objectList) = c("unemp","trend","cycle")


sapply(objectList,class)
#unemp trend cycle 
# "ts"  "ts"  "ts"

Conversion from ts to data.frame:

fn_ts_to_DF = function(x)  {

DF = data.frame(date=zoo::as.Date(time(objectList[[x]])),tseries=as.matrix(objectList[[x]])) 
colnames(DF)[2]=names(objectList)[x]
return(DF)
}


DFList=lapply(seq_along(objectList),fn_ts_to_DF)
names(DFList) = c("unemp","trend","cycle")

seriesTrend = merge(DFList$unemp,DFList$trend,by="date")
cycleSeries = DFList$cycle

Plots:

gSeries = ggplot(melt(seriesTrend,"date"),aes(x=date,y=value,color=variable)) + geom_line() +
          ggtitle('Hodrick-Prescot Filter for unemp') + 
          theme(legend.title = element_blank(),legend.justification = c(0.1, 0.8), legend.position = c(0, 1),
          legend.direction = "horizontal",legend.background = element_rect(fill="transparent",size=.5, linetype="dotted"))
gCycle = ggplot(cycleSeries,aes(x=date,y=cycle)) + geom_line(color="#619CFF") + ggtitle("Cyclical component (deviations from trend)")

gComb = grid.arrange(gSeries,gCycle,nrow=2)

enter image description here

Comments