R 衰退日期转换 [英] R Recession Dates Conversion

查看:63
本文介绍了R 衰退日期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 quantmod 将衰退带数据下载到 R 中.现在这是一个二进制信息(xts 格式),看起来像这样(仅显示第一个衰退期)

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

现在,我有两个问题:

  1. 我想确定每个衰退期的开始和结束(即获取开始和结束日期).由于存在没有衰退的中间零点,我需要一个过滤机制,a) 过滤掉零点(这很容易),b) 确保识别每个新的衰退期.仅仅选择那些还不能解决问题,因为那时没有单独的衰退期,而只是一系列出现衰退的日期.
  2. 我需要将它转换成这样的表格格式,如下所示http://www.r-bloggers.com/use-geom_rect-to-add-recession-bars-to-your-time-series-plots-rstats-ggplot/

1857-06-01、1858-12-011860-10-01, 1861-06-011865-04-01, 1867-12-011869-06-01, 1870-12-011873-10-01、1879-03-01

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

完成后,我想将其用作库 PerformanceAnalytics 中的 event.lines.

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")

推荐答案

我认为这可以满足您的需求.

This does what you want, I think.

基本思想是检测从 1(衰退)到 0(无衰退)的转变,反之亦然.我们可以用 diff(...) 做到这一点.diff(...) 返回包含给定行和前一行之间差异的向量,对于所有行(第一个元素是 NA).因此,当我们进入衰退时,diff 返回 1,当我们离开衰退时,diff 返回 -1.所有其他时间返回 0.

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(对 OP 评论的回应)

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)

这篇关于R 衰退日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆