【发布时间】:2014-12-14 16:38:42
【问题描述】:
我有一个投资组合的数据集:
# Input test data
portolios <- structure(list(portfolioid = c(1L, 1L, 1L, 1L, 1L, 1L), secid = c("A", "B", "A", "C", "C", "A"), reportdate = c("2010-03-31", "2010-03-31", "2010-06-30", "2010-06-30", "2010-07-15", "2010-08-31"), report_type = c("Full", "Full", "Full", "Full", "Partial", "Full"), shares = c(100L, 100L, 130L, 50L, 75L, 80L)), .Names = c("portfolioid", "secid", "reportdate", "report_type", "shares"), row.names = c(NA, -6L), class = c("data.table", "data.frame"))
portfolioid secid reportdate report_type shares
1: 1 A 2010-03-31 Full 100
2: 1 B 2010-03-31 Full 100
3: 1 A 2010-06-30 Full 130
4: 1 C 2010-06-30 Full 50
5: 1 C 2010-07-15 Partial 75
6: 1 A 2010-08-31 Full 80
我需要估算以下缺失记录:
7: 1 B 2010-06-30 Full 0
8: 1 C 2010-08-31 Full 0
业务问题是,有时未针对 Full report_type 报告职位销售(股票 = 0),因此必须根据之前的报告估算缺少的 SecID。
最终,我正在寻求从每个投资组合 ID 的先前报告中计算每个 SecID 的份额变化,以便我的数据集如下所示:
changes <- structure(list(portfolioid = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), secid = c("A", "B", "A", "B", "C", "C", "A", "C"), reportdate = c("2010-03-31", "2010-03-31", "2010-06-30", "2010-06-30", "2010-06-30", "2010-07-15", "2010-08-31", "2010-08-31"), report_type = c("Full", "Full", "Full", "Full", "Full", "Partial", "Full", "Full"), shares = c(100L, 100L, 130L, 0L, 50L, 75L, 80L, 0L), change = c(100L, 100L, 30L, -100L, 50L, 25L, -50L, -75L)), .Names = c("portfolioid", "secid", "reportdate", "report_type", "shares", "change"), row.names = c(NA, -8L), class = c("data.table", "data.frame"))
portfolioid secid reportdate report_type shares change
1: 1 A 2010-03-31 Full 100 100
2: 1 B 2010-03-31 Full 100 100
3: 1 A 2010-06-30 Full 130 30
4: 1 B 2010-06-30 Full 0 -100
5: 1 C 2010-06-30 Full 50 50
6: 1 C 2010-07-15 Partial 75 25
7: 1 A 2010-08-31 Full 80 -50
8: 1 C 2010-08-31 Full 0 -75
我不知道如何为外部连接组合[i] 创建 i。我的问题是我不想使用i <- CJ(reportdate, secid),因为它会产生太多不必要的记录,因为并非每个 secid 都存在于每个 ReportDate 并且不能正确表示需要填充的数据。
我想我需要reportdate,reportdate[-1,secid] 之间的滚动交叉连接
当完整报告中缺少 secid 但它存在于之前的报告(部分或完整)中时,我想前滚 secid 并设置共享:= 0。我相信我会使用选项 roll=1 来做到这一点,但我不确定在哪里或如何实施。
我认为我的问题类似于
How to Calculate a rolling statistic in R using data.table on unevenly spaced data
我确定我缺少一些基本的理解或 CJ() 的技巧,可以创建必要的 i
【问题讨论】:
-
您想加入
portolios什么?你的旧报告?它在哪里?你没有提供。或者您想滚动加入这两个特定的观察结果?目前还不清楚(至少对我而言)发生了什么。如果您只提供您拥有的数据集然后从连接中提供所需的输出会更容易 -
@davidarenburg 提供的唯一数据是投资组合表。我需要创建或从中派生一个要加入的表,以实现我描述的逻辑。所需的输出也显示为我提供的更改表。
-
我不明白你怎么知道哪些是丢失的记录。您只指定
shares := 0,但您怎么知道secid和缺少的日期?例如,为什么1 B 2010-08-31 Full 0也没有丢失? -
@davidarenburg 丢失的记录必须基于此规则进行估算:如果
secid存在于先前的reportdate中但在以下Full reportdate中丢失,则此secid应该是结转和shares:=0。诀窍是如何计算应该从先前的reportdate结转哪个secid。这就是为什么我相信roll=1可能是解决方案的一部分。 -
@davidarenburg
1 B 2010-08-31 Full 0没有丢失的原因是因为 B 不在输入数据集中2010-06-30的先前报告中。您也可以将其视为只想延续到下一个时期secid其中shares <> 0。您不想结转shares = 0的填充缺失值
标签: r data.table