0

I think i got a really basic question, but i can't find any help in my books or google. I need the annual Returns from my monthly Returns.

I tried the aggregate function which didn't work. So i decided to use this formula, its from the PerformanceAnalytics package, but i get only only one number, not from each year. If you have an easy way with R basic I would prefer it.

Return.annualized(ODB_REK$V3, scale = 12, geometric = FALSE)

The Date is storaged in a DataFrame called ODB_REK.

'data.frame':   1104 obs. of  3 variables:
 $ V1: Date, format: "1926-01-30" "1926-02-27" ...
 $ V2: num  -0.0104 -0.0245 -0.1156 0.0898 0.0369 ...
 $ V3: num  -0.0104 -0.0248 -0.1228 0.086 0.0363 ...

My Data:

8610 2015-01-30 -0.045457722924783298
8710 2015-02-27 0.061754336389734224
8810 2015-03-31 -0.008931769762681013
8910 2015-04-30  0.065063695709261307
9010 2015-05-29 -0.002081164120491289
9110 2015-06-30 -0.042074862364842577
9210 2015-07-31 -0.004127506447239640
9310 2015-08-31 -0.082799169893954930
9410 2015-09-30 -0.019941516940899878
9510 2015-10-30 -0.034313015625708092
9610 2015-11-30  0.004613342153957855
9710 2015-12-31 -0.012995071374819037
9810 2016-01-29 -0.097864238637579010
9990 2016-02-29 0.058956407591106048
1001 2016-03-31 0.144829082708177265
1011 2016-04-29 -0.037060328465702078
1021 2016-05-31 0.061671602952122026
1031 2016-06-30 -0.012830965901229322
1041 2016-07-29 0.056609040739955464
1051 2016-08-31 -0.002292626059884113
1061 2016-09-30 -0.000189017862750737
1071 2016-10-31 -0.033022283173137955
1081 2016-11-30 0.063047097017319087
1091 2016-12-30 0.022974063559821404

Any help you can provide will be greatly appreciated.

Kindou
  • 23
  • 1
  • 6

2 Answers2

1

To get the annual returns for a year given the monthly data as in your example, you need to compound the monthly returns. You can test this by entering an array formula in excel: Annual = (Product(1 + monthlydata) -1).

This should work for your data set using R:

# Load packages
library(lubridate)
library(dplyr)

# Create reproducible example
mydates <-c("2015-01-30",
            "2015-02-27",
            "2015-03-31",
            "2015-04-30",
            "2015-05-29",
            "2015-06-30",
            "2015-07-31",
            "2015-08-31",
            "2015-09-30",
            "2015-10-30",
            "2015-11-30",
            "2015-12-31",
            "2016-01-29",
            "2016-02-29",
            "2016-03-31",
            "2016-04-29",
            "2016-05-31",
            "2016-06-30",
            "2016-07-29",
            "2016-08-31",
            "2016-09-30",
            "2016-10-31",
            "2016-11-30",
            "2016-12-30"
            )
mydates <- as.Date(mydates)

myreturns <- c(-0.045457722924783298,
               0.061754336389734224,
               -0.008931769762681013,
               0.065063695709261307,
               -0.002081164120491289,
               -0.042074862364842577,
               -0.004127506447239640,
               -0.082799169893954930,
               -0.019941516940899878,
               -0.034313015625708092,
               0.004613342153957855,
               -0.012995071374819037,
               -0.097864238637579010,
               0.058956407591106048,
               0.144829082708177265,
               -0.037060328465702078,
               0.061671602952122026,
               -0.012830965901229322,
               0.056609040739955464,
               -0.002292626059884113,
               -0.000189017862750737,
               -0.033022283173137955,
               0.063047097017319087,
               0.022974063559821404
               )

# Create data frame
df <- data.frame(dates = mydates, monthly_returns = myreturns)

# Group by year, then compound the monthly returns and subtract 1 to get annual return
df %>%
  group_by(year(dates)) %>%
  summarize(annual_return = prod(1 + monthly_returns) - 1)


# A tibble: 2 x 2
  `year(dates)` annual_return
          <dbl>         <dbl>
1          2015    -0.1234031
2          2016     0.2233210
DaveM
  • 664
  • 6
  • 19
-2
library(lubridate)
library(dplyr)

ODB_REK%>%
mutate(year = year(date))%>%
group_by(year)%>%
summarise(annual_return = sum(monthly_return))

should do the job (although I wish i had your data).

date (should be a string, see this link) corresponds with your date variable and metric corresponds with your last variable.

InfiniteFlash
  • 1,038
  • 1
  • 10
  • 22
  • My data is saved like this : 'data.frame': 1104 obs. of 3 variables: $ V1: Date, format: "1926-01-30" "1926-02-27" ... $ V3: num -0.0104 -0.0248 -0.1228 0.086 0.0363 ... I got an error message by trying this code (%>% these Symbolys are from the dplyr package or ? Error in ODB_REK$V3 %>% mutate(year = year(date)) %>% group_by(year) %>% : could not find function "%>%" – Kindou Dec 28 '17 at 00:00
  • 1
    Make sure to have `dplyr` loaded in. – InfiniteFlash Dec 28 '17 at 00:01
  • Look man, you have to be able to be discern between the variable types and what you're computing. You need to provide a dataset in the OP. – InfiniteFlash Dec 28 '17 at 00:39
  • An actual dataset: using `dput()` – InfiniteFlash Dec 28 '17 at 00:40