合并不规律的时间序列 [英] Merging irregular time series

查看:104
本文介绍了合并不规律的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个时间序列,一个是每天的时间序列,另一个是一个离散的。在我的情况下,我有共同的价格和评级我需要合并,但合并的时间序列保持每日日期根据股票价格,评级是通过股票和日期适应日常数据。
简单的合并命令只会查找确切的日期和代码,并将NA应用于非适用的情况。但是,我想查找完整的比赛并填写最后评分之间的日期。

 每日时间序列:

股票代码股票stock.price
AA美国股票2004- 09-06 1
AA美国股票2004-09-07 2
AA美国股票2004-09-08 3
AA美国股票2004-09-09 4
AA美国股票2004-09-10 5
AA美国股票2004-09-11 6

离散时间序列
代码日期评级Last_Rating
AA美国股票2004-09-08 A A +
AA美国股票2004-09-11 AA A
AAL LN股权2005-09-08 BB BB
AAL LN股权2007-09-09 AA AA-
ABE SM股权2006-09-10 AA AA-
ABE SM权益2009-09-11 AA AA-


必需输出:

代码日期股票价格评级
AA美国股票2004-09-06 1 A +
AA美国股票2004-09-07 2 A +
AA美国股票2004-09-08 3 A
AA美国股票2004-09-09 4 A
AA美国股票2004-09-10 5 A
AA美国股票2004-09-11 6 AA

我将非常乐意为您的帮助。

解决方案

也许这是你想要的解决方案。
时间序列包 zoo 中的函数 na.locf 可用于将值转发(或

$ b

 库(zoo)
库(plyr)
options(stringsAsFactors = FALSE)

daily_ts = data.frame(
ticker = c('A','A','A','A','B','B','B' B'),
date = c(1,2,3,4,1,2,3,4),
stock.price = c(1.1,1.2,1.3,1.4,4.1,4.2 ,4.3,4.4)

discrete_ts = data.frame(
ticker = c('A','A','B','B'),
date = c(2,4,2,4),
Rating = c('A','AA','BB','BB-'),
Last_Rating = c('A +', 'a','BB +','BB')


res = ddply(
merge(daily_ts,discrete_ts,by = c(ticker ),
ticker,
function(x)
data.frame(
x [,c(ticker,date,stock)价格)],
Rating = na.locf(x $ Rating,na.rm = FALSE),
Last_Rating = na.locf(x $ Last_Rating,n a.rm = FALSE,fromLast = TRUE)



res = within(
res,
评级< -ifelse(
is.na(Rating),
Last_Rating,Rating

)[,setdiff(colnames(res),Last_Rating)]

res

提供

 #ticker date stock.price评价
#1 A 1 1.1 A +
#2 A 2 1.2 A
#3 A 3 1.3 A
#4 A 4 1.4 AA
#5 B 1 4.1 BB +
#6 B 2 4.2 BB
#7 B 3 4.3 BB
#8 B 4 4.4 BB-


I have two time series, one being a daily time series and the other one a discrete one. In my case I have share prices and ratings I need to merge but in a way that the merged time series keeps the daily dates according to the stock prices and that the rating is fitted to the daily data by ticker and date. A simple merge command would only look for the exact date and ticker and apply NA to non-fitting cases. But I would like to look for the exact matches and fill the dates between with last rating.

 Daily time series:

         ticker       date        stock.price
          AA US Equity 2004-09-06  1
          AA US Equity 2004-09-07  2
          AA US Equity 2004-09-08  3
          AA US Equity 2004-09-09  4
          AA US Equity 2004-09-10  5
          AA US Equity 2004-09-11  6

  Discrete time series
          ticker        date        Rating Last_Rating
          AA US Equity   2004-09-08   A         A+
          AA US Equity   2004-09-11   AA        A
          AAL LN Equity  2005-09-08   BB        BB
          AAL LN Equity  2007-09-09   AA        AA-
          ABE SM Equity  2006-09-10   AA        AA-
          ABE SM Equity  2009-09-11   AA        AA-


  Required Output:

           ticker       date        stock.price  Rating
          AA US Equity 2004-09-06    1             A+
          AA US Equity 2004-09-07    2             A+
          AA US Equity 2004-09-08    3             A
          AA US Equity 2004-09-09    4             A
          AA US Equity 2004-09-10    5             A
          AA US Equity 2004-09-11    6             AA

I would be very greatful for your help.

解决方案

Maybe this is the solution you want. The function na.locf in the time series package zoo can be used to carry values forward (or backward).

library(zoo)
library(plyr)
options(stringsAsFactors=FALSE)

daily_ts=data.frame(
    ticker=c('A','A','A','A','B','B','B','B'),
    date=c(1,2,3,4,1,2,3,4),
    stock.price=c(1.1,1.2,1.3,1.4,4.1,4.2,4.3,4.4)
    )
discrete_ts=data.frame(
    ticker=c('A','A','B','B'),
    date=c(2,4,2,4),
    Rating=c('A','AA','BB','BB-'),
    Last_Rating=c('A+','A','BB+','BB')
    )

res=ddply(
    merge(daily_ts,discrete_ts,by=c("ticker","date"),all=TRUE),
    "ticker",
    function(x) 
        data.frame(
            x[,c("ticker","date","stock.price")],
            Rating=na.locf(x$Rating,na.rm=FALSE),
            Last_Rating=na.locf(x$Last_Rating,na.rm=FALSE,fromLast=TRUE)
            )
    )

res=within(
    res,
    Rating<-ifelse(
        is.na(Rating),
        Last_Rating,Rating
        )
    )[,setdiff(colnames(res),"Last_Rating")]

res

Gives

#  ticker date stock.price Rating
#1      A    1         1.1     A+
#2      A    2         1.2      A
#3      A    3         1.3      A
#4      A    4         1.4     AA
#5      B    1         4.1    BB+
#6      B    2         4.2     BB
#7      B    3         4.3     BB
#8      B    4         4.4    BB-

这篇关于合并不规律的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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