问题
I am downloading recession band data into R via quantmod
. Now this comes as a binary information (in xts format) looking like this (just the first recession period shown)
1857-01-01 0
1857-02-01 0
1857-03-01 0
1857-04-01 0
1857-05-01 0
1857-06-01 0
1857-07-01 1
1857-08-01 1
1857-09-01 1
1857-10-01 1
1857-11-01 1
1857-12-01 1
1858-01-01 1
1858-02-01 1
1858-03-01 1
1858-04-01 1
1858-05-01 1
1858-06-01 1
1858-07-01 1
1858-08-01 1
1858-09-01 1
1858-10-01 1
1858-11-01 1
1858-12-01 1
Now, I have two issues:
- I would like to determine the start and end of each recession period (ie. get the start and end date) for each recession period. As there are intermediate zeros where there is no recession, I need a filter mechanism which a) filters out zeros (this is easy), b) make sure that each new recession period is recognized. Just selecting the ones does not yet do the trick as then there are no individual recession periods, but merely a collection of dates where there has been a recession.
I need to convert it in a table format like this and as shown here http://www.r-bloggers.com/use-geom_rect-to-add-recession-bars-to-your-time-series-plots-rstats-ggplot/
1857-06-01, 1858-12-01 1860-10-01, 1861-06-01 1865-04-01, 1867-12-01 1869-06-01, 1870-12-01 1873-10-01, 1879-03-01
Once this is done, I want to use it as event.lines in the library PerformanceAnalytics
.
Can anybody help me on how to do this?
If you want to download the series for trying, do
library(quantmod)
getSymbols("USREC",src="FRED")
回答1:
This does what you want, I think.
The basic idea is to detect transitions from 1 (recession) to 0 (no recession) and vice versa. We can do this with diff(...)
. diff(...)
returns the vector containing the difference between a given row and the previous one, for all rows (the first element is NA
). So, when we go into a recession diff returns 1, when we leave a recession, diff returns -1. All other times it returns 0.
library(quantmod)
getSymbols("USREC",src="FRED")
getSymbols("UNRATE", src="FRED")
unrate.df <- data.frame(date= index(UNRATE),UNRATE$UNRATE)
start <- index(USREC[which(diff(USREC$USREC)==1)])
end <- index(USREC[which(diff(USREC$USREC)==-1)-1])
reccesion.df <- data.frame(start=start, end=end[-1])
recession.df <- subset(reccesion.df, start >= min(unrate.df$date))
ggplot()+
geom_line(data=unrate.df, aes(x=date,y=UNRATE)) +
geom_rect(data=recession.df,
aes(xmin=start,xmax=end, ymin=0,ymax=max(unrate.df$UNRATE)),
fill="red", alpha=0.2)
EDIT (Response to OP's comment)
library(PerformanceAnalytics)
cycles.dates <- paste(format(start,"%Y-%m"),format(end[-1],"%Y-%m"),sep="/")
chart.TimeSeries(UNRATE,period.areas=cycles.dates,
period.color="lightblue", lwd=1)
来源:https://stackoverflow.com/questions/21739012/r-recession-dates-conversion