How to aggregate daily log returns into weekly log returns

Hi all!

I am busy finding out how to aggregate the daily log returns for a certain week. I have tried some suggestions, but they seem not to fit my problem.

Example:

1990-01-01 - 0.01
1990-01-02 - 0.01
1990-01-03 - 0.01
1990-01-04 - 0.01
1990-01-05 - 0.01

Should give me a weekly log return of 0.05 (out of simplicity I just took a random year)

library(tidyverse)
library(lubridate)
(mydf <- structure(
  list(
    date = structure(c(7305, 7306, 7307, 7308, 7309), class = "Date"),
    val = c(0.01, 0.01, 0.01, 0.01, 0.01)
  ),
  row.names = c(NA, -5L),
  class = c("tbl_df", "tbl", "data.frame")
))


mydf %>% group_by(wk_of_date=week(date)) %>% summarise(sumval=sum(val))

For some applications, you may want to keep the week represented by a date (usually the start day of the week) instead of a number, here an example using a specialized package (tsibble).

library(dplyr)
library(tsibble)
library(lubridate)

# Sample data on a copy/paste friendly format
mydf <- data.frame(
    date = seq.Date(as.Date("1990-01-01"), as.Date("1990-01-31"), by = 1),
    val = 0.01
)

# Relevant code
mydf %>%
    as_tsibble(index = date) %>%
    index_by(week = ~ floor_date(., unit = "week", week_start = 1)) %>% # weekly aggregates
    summarise(
        sumval = sum(val, na.rm = TRUE)
    )
#> # A tsibble: 5 x 2 [7D]
#>   week       sumval
#>   <date>      <dbl>
#> 1 1990-01-01   0.07
#> 2 1990-01-08   0.07
#> 3 1990-01-15   0.07
#> 4 1990-01-22   0.07
#> 5 1990-01-29   0.03

Created on 2020-10-04 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.