我正在尝试使用data.table来填补一个大型不平衡的多维面板中的缺失观测值。以下是带有一些注释的数据示例,说明我的意图:
mydat <- structure(list(fund = c(1, 1, 1, 1, 2, 2, 2, 3, 3), holdingid = c(10,
10, 11, 11, 15, 15, 14, 20, 20), yearqtr = structure(c(2000,
2000.5, 2000, 2000.25, 2000, 2000.75, 2000.25, 2000.25, 2000.5
), class = "yearqtr"), shares = c(20, 25, 30, 30, 34, 34, 4,
8, 10)), .Names = c("fund", "holdingid", "yearqtr", "shares"), row.names = c(NA,
-9L), class = "data.frame")
allqtrs <- structure(c(2000, 2000.25, 2000.5, 2000.75), class = "yearqtr")
#note that there are missing yearqtrs for some fund-holding series
#if a fund-holding series is missing an observation I want to create
#that fund-holding-quarter and fill it with NA
我正在尝试平衡面板,最终目标是正确地滞后(或差异化)每个基金持有ID系列(以便处理数据的不规则性)。显然,我可以为每个基金持有ID组使用zooreg,并使用此方法进行滞后,但我的数据> 2000万行,我正在尝试编写更高效的解决方案。感谢您的帮助。
编辑:为了更明确一些,我正在寻找类似于Oracle SQL的分区外连接所示的操作,如此处所示 http://st-curriculum.oracle.com/obe/db/10g/r2/prod/bidw/outerjoin/outerjoin_otn.htm。 < p > < em >编辑-2 我在描述中使用了很多时间序列术语。更具体地说,对于每个基金持有对,我希望在所有季度中为每个yearqtr都有一个观察值。因此,在这种情况下,由于有3个基金分别拥有3、2和1个持股,因此输出中应该有(2+2+1)*4行总数,因为每个基金持股都有4个可能的季度。另一个重要的点是,持有id非常多样化。类似于expand.grid(unique(fund),unique(holdingid),unique(allqtrs))将导致行数过多,因为每个基金只会拥有可能持有的一小部分。