0
$\begingroup$

I am trying to transform daily simple returns into weekly returns. I am using the following R code:

library(zoo)
tt <- seq(Sys.Date(), by='day', length=365)
vals <- data.frame(A=runif(365), B=rnorm(365), C=1:365)
z <- zoo(vals, tt)
week <- function(x)format(x, '%Y.%W')
dailytoweekly <- function(x) (prod(x+1)-1)
aggregate(z, by=week, FUN=dailytoweekly)

based on this post: https://stackoverflow.com/questions/16442396/convert-daily-to-weekly-monthly-data-with-r .
The function dailytoweekly is written on the basis of simple return's time-additive properties. What puzzles me is the result:

2015.50 19.7687016 -0.88561787 5.039000e+03
2015.51 19.8390703 -1.21981217 1.729728e+07
2015.52  4.9931289 -1.76381705 7.343900e+04
2016.00  2.2251422  2.98725821 7.979000e+03
2016.01 15.9745238 -1.33997280 5.967562e+09
2016.02 15.3588303 -0.91805535 3.389158e+10

Why there is a week 0 in the results and what is it supposed to represent? As there are 53 weeks in total, with indices from 0 to 52 inclusive. Any help on this issue would be appreciated :)

$\endgroup$
6
  • 1
    $\begingroup$ 0th week is just first week. Instead of your expected week labels from 1 to 53, R's function format() converts week indices to 0-52 range. Out of curiosity, why don't you use a readily available to.weekly() function from xts package? $\endgroup$ Commented Dec 15, 2015 at 21:13
  • $\begingroup$ @bushmanov Now i noticed there are also years where instead of 0-52 indices there are 1-53. But I hope this is exactly the same. I tried using it, however, when inputting an univariate xts object into 'to.weekly()' the function returned an OHLC structure as a result and I did not know how to work make it return a univariate object. $\endgroup$
    – Masher
    Commented Dec 15, 2015 at 22:06
  • $\begingroup$ @bushmanov: xts::to.weekly wouldn't work (as the OP noted in their comment), but xts::apply.weekly would. $\endgroup$ Commented Dec 16, 2015 at 14:24
  • $\begingroup$ @JoshuaUlrich I bet it would had he used it properly $\endgroup$ Commented Dec 16, 2015 at 16:31
  • $\begingroup$ @bushmanov: I'll bet you anything it wouldn't. They have daily returns. to.weekly will return the first, highest, lowest, and last return of each week. It won't sum them. Regardless, if you happen to be able to make it work somehow, I can always change the function and push to CRAN in order to win the bet. ;) $\endgroup$ Commented Dec 17, 2015 at 20:47

1 Answer 1

1
$\begingroup$

Why there is a week 0 in the results and what is it supposed to represent?

The answer is found in ?strptime:

'%W' Week of the year as decimal number (00-53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention.

Perhaps you were expecting the output from one of the other formats?

'%U' Week of the year as decimal number (00-53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.

'%V' Week of the year as decimal number (01-53) as defined in ISO 8601. If the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. (Accepted but ignored on input.)


You might try see if xts::apply.weekly solves your problem. Using it will avoid the problem in your question.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.