I have a data frame of daily data like following:
Date bhpau_o bhpau_c_1d bhpau_l bbl_o bbl_c_1d bbl_l blt_o blt_c_1d
2007-01-01 19.89669 19.93834 19.94399 18.585 18.585 18.600 18.28723 18.39005
2007-01-02 20.17571 19.94399 20.32699 18.585 18.585 18.600 18.59225 18.28723
2007-01-03 20.42895 20.32699 20.15213 18.510 18.600 18.080 18.62441 18.98699
2007-01-04 19.45845 20.15213 19.15969 17.525 18.080 17.450 17.75146 18.19537
2007-01-05 18.79020 19.15969 18.82920 17.445 17.450 17.275 17.26992 17.49870
2007-01-08 18.67068 18.82920 18.63943 17.380 17.275 17.355 17.09140 17.05766
2007-01-09 18.78240 18.63943 19.26600 17.405 17.355 17.540 17.50670 17.23673
2007-01-10 18.80098 19.26600 18.65337 17.350 17.540 17.500 17.08860 17.22542
2007-01-11 19.06616 18.65337 19.23807 17.830 17.500 18.200 17.58981 17.13693
2007-01-12 19.62668 19.23807 19.66585 17.950 18.200 18.330 18.12275 18.08571
2007-01-15 19.74896 19.66585 19.79600 17.950 18.200 18.330 18.26427 18.06403
What I wish to do is apply calculation on subsets of a dataset. Specifically, I wish to do the following calculation:
Compute the absolute value of daily overnight return of bbl (bbl_l-bbl_o)
Compute the sum of such absolute values(obtained in step 1) with a 10-day period
Compute the weights of each overnight return
Compute the overnight return of bhpau (bhpau_l-bhpau_o) then divided by the overnight return of bbl(non-absolute value obtained in step 1)
Multiple the weights(obtained from step 3) by the value from step 4
Sum up every 10 days' products(from step 5)
The code I used to calculate WPC is as shown below. However, they are only for calculating a pooled WPC.
##calculating WPC
WF1 = abs(data$bbl_l-data$bbl_o)/sum(abs(data$bbl_l-data$bbl_o))
CON1 = (data$bhpau_o-data$bhpau_c_1d)/(data$bbl_l-data$bbl_o)
WPC1 = sum(WF1*CON1)
How can I loop the function and get WPCs for every 10 days?
I tried rollapply
but it ended up with error message.
Any help will be appreciated.