R拾取经济衰退期的开始日期和结束日期

R pick up the starting date and ending date of the Recession period(R拾取经济衰退期的开始日期和结束日期)
本文介绍了R拾取经济衰退期的开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在 R 中绘制衰退阴影期.考虑以下示例,衰退期被识别为 1,非衰退期被识别为 0.

日期衰退1918-09-01 11918-10-01 11918-11-01 11918-12-01 11919-01-01 11919-02-01 11919-03-01 11919-04-01 01919-05-01 01919-06-01 01919-07-01 01919-08-01 01919-09-01 01919-10-01 01919-11-01 01919-12-01 01920-01-01 01920-02-01 11920-03-01 11920-04-01 11920-05-01 1

谁能帮我确定经济衰退期的开始日期和结束日期?例如:

开始结束1918-09-01 1919-03-011920-02-01 1920-05-01

几年前也有人问过同样的问题,但我认为答案无法解决这个问题.请参阅 R 衰退日期转换

提前致谢!

解决方案

使用 data.table 包中的 rleid() 函数:

库(data.table)data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)][衰退 == 1,.(开始 = V1,结束 = V2)]

<块引用>

 开始 结束1: 1918-09-01 1919-03-012: 1920-02-01 1920-05-01

说明

第一个data.table 表达式查找所有 个周期的开始和结束日期.rleid() 是一个方便的函数,用于生成用于分组操作的游程类型 id 列.

data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)]

<块引用>

 rleid Recession V1 V21:1 1 1918-09-01 1919-03-012:2 0 1919-04-01 1920-01-013:3 1 1920-02-01 1920-05-01

第二个表达式仅选择衰退期并返回 StartEnd 日期.

数据

DF <- readr::read_table(日期衰退1918-09-01 11918-10-01 11918-11-01 11918-12-01 11919-01-01 11919-02-01 11919-03-01 11919-04-01 01919-05-01 01919-06-01 01919-07-01 01919-08-01 01919-09-01 01919-10-01 01919-11-01 01919-12-01 01920-01-01 01920-02-01 11920-03-01 11920-04-01 11920-05-01 1")

I'm trying to plot the Recession Shading periods in R. Consider the following example, recession periods are recognized as 1, and non recession periods are 0.

Date           Recession  
1918-09-01     1  
1918-10-01     1  
1918-11-01     1  
1918-12-01     1  
1919-01-01     1  
1919-02-01     1  
1919-03-01     1  
1919-04-01     0  
1919-05-01     0  
1919-06-01     0  
1919-07-01     0  
1919-08-01     0  
1919-09-01     0  
1919-10-01     0  
1919-11-01     0  
1919-12-01     0  
1920-01-01     0  
1920-02-01     1  
1920-03-01     1  
1920-04-01     1  
1920-05-01     1  

Can anyone help me to pick up the starting date and ending dates of the recession periods? For example:

Start                 End
1918-09-01     1919-03-01
1920-02-01     1920-05-01

The same question has been asked few years ago, but I think the answer is not able solve this question. see R Recession Dates Conversion

Thanks in advance!

解决方案

Using the rleid() function from the data.table package:

library(data.table)
data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)][
  Recession == 1, .(Start = V1, End = V2)]

        Start        End
1: 1918-09-01 1919-03-01
2: 1920-02-01 1920-05-01

Explanation

The first data.table expression finds start and end dates of all periods. rleid() is a convenience function for generating a run-length type id column to be used in grouping operations.

data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)]

   rleid Recession         V1         V2
1:     1         1 1918-09-01 1919-03-01
2:     2         0 1919-04-01 1920-01-01
3:     3         1 1920-02-01 1920-05-01

The second expression picks only the Recession periods and returns the Start and End dates.

Data

DF <- readr::read_table(
  "Date           Recession  
  1918-09-01     1  
  1918-10-01     1  
  1918-11-01     1  
  1918-12-01     1  
  1919-01-01     1  
  1919-02-01     1  
  1919-03-01     1  
  1919-04-01     0  
  1919-05-01     0  
  1919-06-01     0  
  1919-07-01     0  
  1919-08-01     0  
  1919-09-01     0  
  1919-10-01     0  
  1919-11-01     0  
  1919-12-01     0  
  1920-01-01     0  
  1920-02-01     1  
  1920-03-01     1  
  1920-04-01     1  
  1920-05-01     1  "
)

这篇关于R拾取经济衰退期的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)