我认为我有一个非常基本的问题,但我无法在我的书籍或谷歌中找到任何帮助。我需要每月退货的年度退货。
我尝试了无效的聚合功能。所以我决定使用这个公式,它来自PerformanceAnalytics包,但我只得到一个数字,而不是每年。如果你有一个简单的方法与R基础我希望它。
Return.annualized(ODB_REK$V3, scale = 12, geometric = FALSE)
日期存储在名为ODB_REK的DataFrame中。
'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 ...
我的数据:
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
非常感谢您提供的任何帮助。
答案 0 :(得分:0)
要根据示例中的月度数据获取一年的年度回报,您需要将月度回报复合。您可以通过在Excel中输入数组公式来测试:年=(产品(1 +每月数据)-1)。
这应该适用于使用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
答案 1 :(得分:-1)
library(lubridate)
library(dplyr)
ODB_REK%>%
mutate(year = year(date))%>%
group_by(year)%>%
summarise(annual_return = sum(monthly_return))
应该做的工作(虽然我希望我有你的数据)。
date
(应为字符串,请参阅此link)与您的日期变量对应,metric
与您的上一个变量对应。