您可以使用xts
andlubridate
来简化日期操作
数据:
require(xts)
require(lubridate)
DF = read.csv(text="
date,rt_1_period,rt_12_mth_window
04-04-13, ,
10-04-13,0.729096362,
24-05-13,1.002535647,
30-05-13,0.993675716,
21-07-13,1.002662843,
03-08-13,1.009516582,
01-09-13,0.963099395,
20-10-13,1.012470278,
25-10-13,1.01308502 ,
03-11-13,1.005440704,
01-01-14,1.024208021,
11-01-14,0.996613924,
17-02-14,1.009811368,
24-02-14,1.008139557,
30-03-14,1.002794709,
30-04-14,0.998745849,1.042345473
02-05-14,1.002324076,1.044767963
27-06-14,0.997741026,1.046389027
24-08-14,1.015767546,1.050072129
05-09-14,1.014405005,1.106010894
02-11-14,1.013830296,1.09319212
09-11-14,1.013127219,1.101549487
16-11-14,1.012614177,1.115444628
18-01-15,0.986893629,1.078458006
24-01-15,1.028120919,1.108785236
10-04-15,0.912452762,0.991025615
09-08-15,1.004676152,0.981376513
07-01-16,1.004236123,0.934086003
01-04-16,1.02341302 ,0.94215696",header=TRUE,stringsAsFactors=FALSE,na.strings="")
#Convert to xts time series for ease in date manipulation
DF_xts = xts(DF[,-1],order.by = as.Date(DF[,1],format="%d-%m-%y"))
head(DF_xts)
#
# rt_1_period rt_12_mth_window
#2013-04-04 NA NA
#2013-04-10 0.729096362 NA
#2013-05-24 1.002535647 NA
#2013-05-30 0.993675716 NA
#2013-07-21 1.002662843 NA
#2013-08-03 1.009516582 NA
#set lag period as 1 year
lagPeriod = 1
累计12m产品:
为每个日期构建一个窗口[prevYearDate,date],子集1m返回位于该窗口中,计算累积产品并选择最后一个产品
rt_12_mth_window_Calc = do.call(rbind,lapply(as.Date(index(DF_xts)),function(x) {
prevYearDate = x-years(lagPeriod)
rt_12_mth_window_Calc = last(cumprod(DF_xts[paste0(prevYearDate,"/",x),"rt_1_period"]))
colnames(rt_12_mth_window_Calc) = "rt_12_mth_window_Calc"
return(rt_12_mth_window_Calc)
}))
最终数据集:
#Merge with original time series for final dataset
new_DF = merge.xts(DF_xts,rt_12_mth_window_Calc)
#Calculate difference in original and calculated 12 month returns
new_DF$delta = new_DF$rt_12_mth_window_Calc - new_DF$rt_12_mth_window
new_DF
# rt_1_period rt_12_mth_window rt_12_mth_window_Calc delta
#2013-04-04 NA NA NA NA
#2013-04-10 0.729096362 NA NA NA
#2013-05-24 1.002535647 NA NA NA
#2013-05-30 0.993675716 NA NA NA
#2013-07-21 1.002662843 NA NA NA
#2013-08-03 1.009516582 NA NA NA
#2013-09-01 0.963099395 NA NA NA
#2013-10-20 1.012470278 NA NA NA
#2013-10-25 1.013085020 NA NA NA
#2013-11-03 1.005440704 NA NA NA
#2014-01-01 1.024208021 NA NA NA
#2014-01-11 0.996613924 NA NA NA
#2014-02-17 1.009811368 NA NA NA
#2014-02-24 1.008139557 NA NA NA
#2014-03-30 1.002794709 NA NA NA
#2014-04-30 0.998745849 1.042345473 1.042345470 -2.64001643e-09
#2014-05-02 1.002324076 1.044767963 1.044767960 -2.54864396e-09
#2014-06-27 0.997741026 1.046389027 1.046389025 -1.97754613e-09
#2014-08-24 1.015767546 1.050072129 1.050072127 -1.66086833e-09
#2014-09-05 1.014405005 1.106010894 1.106010893 -1.34046041e-09
#2014-11-02 1.013830296 1.093192120 1.093192120 -6.47777387e-11
#2014-11-09 1.013127219 1.101549487 1.101549488 5.99306826e-10
#2014-11-16 1.012614177 1.115444628 1.115444628 -1.89856353e-10
#2015-01-18 0.986893629 1.078458006 1.078458005 -1.15637744e-09
#2015-01-24 1.028120919 1.108785236 1.108785235 -9.57268265e-10
#2015-04-10 0.912452762 0.991025615 0.991025613 -1.54581248e-09
#2015-08-09 1.004676152 0.981376513 0.996850412 1.54738992e-02
#2016-01-07 1.004236123 0.934086003 0.934086002 -9.15302278e-10
#2016-04-01 1.023413020 0.942156960 0.942156960 -1.82048598e-10
除 2015-08-09 外,所有观测值的计算值和原始值都非常接近,值的偏差为 1.55%,您能否确认您在此期间的计算