繁体   English   中英

使用 group_by function 的累积产品

[英]cumulative product using group_by function

我正在寻找一种简单的方法来计算系列的累积回报。 数据贴在底部。 我想要的是一个 function ,它返回值1.0093883 *1.0610182=1.070979为第 115:134 行,同样为那些排名最差的。 我的代码目前如下所示:

CombData <- CombData %>%
  group_by(Date, ESG_P) %>%
  mutate(ESGE_Return = cumprod(1+mean(ExReturn)))

问题来自最后一个代码,因为它只返回1+mean(Exreturn)并且不会使其累积。

提前致谢

  structure(list(Date = structure(c(14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 14668, 
14668, 14668, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 
14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 
14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 
14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 
14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 14699, 
14699, 14699, 14699, 14699), class = "Date"), ExReturn = c(0.0582959641255605, 
-0.0830280830280831, 0.00854700854700852, -0.0827067669172933, 
0.145833333333333, -0.0830449826989619, 0.0138248847926268, -0.0796460176991151, 
-0.0886956521739131, -0.0618336886993603, -0.0815850815850816, 
0.0446428571428572, -0.05, -0.0833333333333334, -0.0204081632653061, 
0.146153846153846, -0.0423913043478261, 0.04480198019802, -0.0551181102362205, 
-0.0461538461538461, -0.0476190476190477, -0.0073260073260073, 
-0.135922330097087, -0.0357142857142857, 0.02, -0.0431818181818181, 
0.00403923831506048, 0.0535714285714286, -0.0155403814457264, 
0.028169014084507, 0.0556844547563806, -0.0106382978723405, -0.001, 
-0.0578947368421052, -0.0398126463700235, 0.107954545454545, 
0.0743801652892562, 0.0625, -0.0229885057471264, -0.055613850996852, 
-0.0228426395939086, 0.123919308357349, 0.142857142857143, -0.0632976008167433, 
-0.103030303030303, -0.0386100386100386, -0.0785714285714286, 
0.0540540540540539, -0.0398009950248757, -0.0458860759493671, 
-0.0728476821192053, -0.0111111111111111, -0.0457516339869281, 
-0.0513459621136589, -0.0385989992852037, -0.00649350649350644, 
-0.072463768115942, 0, 0.037037037037037, -0.117021276595745, 
-0.00874635568513116, 0.0117647058823529, -0.0206060606060606, 
-0.017391304347826, -0.040650406504065, 0.0227272727272727, 0.0769230769230769, 
-0.143243243243243, 0.00877192982456143, -0.106796116504854, 
0.0434782608695652, -0.0194805194805195, -0.100840336134454, 
0.0137672090112642, -0.146666666666667, -0.0990990990990991, 
-0.116822429906542, 0.0505050505050506, -0.034873949579832, -0.0576923076923077, 
0.0952380952380953, -0.0759493670886076, -0.0487804878048781, 
-0.138339920948617, -0.0580645161290323, -0.0145985401459854, 
-0.00816326530612244, -0.00624999999999998, 0.0421052631578946, 
0, 0.0126353790613718, 0.0615384615384615, -0.000762195121951192, 
0.00103842159916923, 0.0108695652173914, 0.0560875512995895, 
0.13768115942029, 0.0832826747720363, 0.0179028132992327, -0.160839160839161, 
-0.0158730158730159, -0.0729736154028999, -0.05, -0.0551181102362205, 
0.0347826086956522, 0.024390243902439, -0.0823529411764706, -0.0689655172413792, 
0, 0.14176245210728, 0.0316455696202531, -0.0489795918367347, 
0.0699604743083004, 0.0656934306569343, 0.108514232209738, 0.0425643447975621, 
0.0221222222222221, 0.0475190476190477, 0.102464102564103, -0.0826, 
0.118408655126498, -1e-04, -1e-04, 0.109159259259259, 0.1249, 
0.0972451327433628, 0.152217880794702, -0.0223222222222223, 0.0253237288135593, 
0.0559165975103735, 0.107931088082902, 0.15804226925338, 0.0872440285204992, 
-0.0340859623199112, 0.089056626506024, -1e-04, 0.0169261066969353, 
0.051560516605166, 0.0713285714285714, -0.0209333333333334, 0.123187671232877, 
0.0122456790123457, -0.0513820512820513, 0.0512157894736842, 
0.00444436737622577, -0.0491490490490491, 0.107042857142857, 
-0.0596238095238094, 0.00924579439252326, 0.146775, 0.152073913043478, 
-0.0283051282051282, 0.0752424657534247, 0.153746153846154, 0.0900639344262295, 
0.111011111111111, 0.1124, 0.0987847583643124, 0.0952470633104501, 
-1e-04, 0.133233333333333), ESG_P = c("Best", "Best", "Best", 
"Best", "Best", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Best", 
"Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best", 
"Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best", 
"Best", "Best", "Best", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", "Worst", "Worst", "Worst", "Worst", "Worst", "Worst", 
"Worst", NA, NA, NA, NA, NA, NA, NA), ESGE_Return = c(1.00938829121211, 
1.00938829121211, 1.00938829121211, 1.00938829121211, 1.00938829121211, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 0.984734747488776, 0.984734747488776, 0.984734747488776, 
0.984734747488776, 1.06101822024875, 1.06101822024875, 1.06101822024875, 
1.06101822024875, 1.06101822024875, 1.06101822024875, 1.06101822024875, 
1.06101822024875, 1.06101822024875, 1.06101822024875, 1.06101822024875, 
1.06101822024875, 1.06101822024875, 1.06101822024875, 1.06101822024875, 
1.06101822024875, 1.06101822024875, 1.06101822024875, 1.06101822024875, 
1.06101822024875, 1.04272990705584, 1.04272990705584, 1.04272990705584, 
1.04272990705584, 1.04272990705584, 1.04272990705584, 1.04272990705584, 
1.04272990705584, 1.04272990705584, 1.04272990705584, 1.04272990705584, 
1.04272990705584, 1.04272990705584, 1.04272990705584, 1.04272990705584, 
1.04272990705584, 1.04272990705584, 1.04272990705584, 1.04272990705584, 
1.04272990705584, 1.0371889473239, 1.0371889473239, 1.0371889473239, 
1.0371889473239, 1.0371889473239, 1.0371889473239, 1.0371889473239
), ESGV_Return = c(0.94730811878536, 0.94730811878536, 0.94730811878536, 
0.94730811878536, 0.94730811878536, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.00094157966403, 
1.00094157966403, 1.00094157966403, 1.00094157966403, 1.10461707840793, 
1.10461707840793, 1.10461707840793, 1.10461707840793, 1.10461707840793, 
1.10461707840793, 1.10461707840793, 1.10461707840793, 1.10461707840793, 
1.10461707840793, 1.10461707840793, 1.10461707840793, 1.10461707840793, 
1.10461707840793, 1.10461707840793, 1.10461707840793, 1.10461707840793, 
1.10461707840793, 1.10461707840793, 1.10461707840793, 1.04630896671243, 
1.04630896671243, 1.04630896671243, 1.04630896671243, 1.04630896671243, 
1.04630896671243, 1.04630896671243, 1.04630896671243, 1.04630896671243, 
1.04630896671243, 1.04630896671243, 1.04630896671243, 1.04630896671243, 
1.04630896671243, 1.04630896671243, 1.04630896671243, 1.04630896671243, 
1.04630896671243, 1.04630896671243, 1.04630896671243, 1.05551714122886, 
1.05551714122886, 1.05551714122886, 1.05551714122886, 1.05551714122886, 
1.05551714122886, 1.05551714122886)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -161L), groups = structure(list(
    Date = structure(c(14668, 14668, 14699, 14699, 14699), class = "Date"), 
    ESG_P = c("Best", "Worst", "Best", "Worst", NA), .rows = list(
        1:5, 6:114, 115:134, 135:154, 155:161)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

我想您正在寻找summarize function?

CombData %>%
   group_by(Date, ESG_P) %>%
   summarize(ESGE_Return = 1 + mean(ExReturn)) %>%
   group_by(ESG_P) %>%
   summarize(ESGE_Return = Reduce(`*`,ESGE_Return))
## A tibble: 3 x 2
#  ESG_P ESGE_Return
#  <chr>       <dbl>
#1 Best         1.07
#2 Worst        1.03
#3 NA           1.09

我认为原始数据没有ESGE_ReturnESGV_Return列。 请首先从您的示例数据中删除它们并运行以下代码。

library(dplyr)

CombData %>%
  group_by(Date, ESG_P) %>%
  summarise(ESGE_Return = mean(ExReturn) + 1) %>%
  group_by(ESG_P) %>%
  mutate(ESGV_Return = cumprod(ESGE_Return)) %>%
  right_join(CombData, by = c("Date", "ESG_P"))

Output (第 115 至 134 行)

# A tibble: 161 x 5
# Groups:   ESG_P [3]
    Date       ESG_P ESGE_Return ESGV_Return     ExReturn
    <date>     <chr>       <dbl>       <dbl>        <dbl>
⋮
115 2010-03-31 Best     1.06102     1.07098   0.108514   
116 2010-03-31 Best     1.06102     1.07098   0.0425643  
117 2010-03-31 Best     1.06102     1.07098   0.0221222  
118 2010-03-31 Best     1.06102     1.07098   0.0475190  
119 2010-03-31 Best     1.06102     1.07098   0.102464   
120 2010-03-31 Best     1.06102     1.07098  -0.0826     
121 2010-03-31 Best     1.06102     1.07098   0.118409   
122 2010-03-31 Best     1.06102     1.07098  -0.0001     
123 2010-03-31 Best     1.06102     1.07098  -0.0001     
124 2010-03-31 Best     1.06102     1.07098   0.109159   
125 2010-03-31 Best     1.06102     1.07098   0.1249     
126 2010-03-31 Best     1.06102     1.07098   0.0972451  
127 2010-03-31 Best     1.06102     1.07098   0.152218   
128 2010-03-31 Best     1.06102     1.07098  -0.0223222  
129 2010-03-31 Best     1.06102     1.07098   0.0253237  
130 2010-03-31 Best     1.06102     1.07098   0.0559166  
131 2010-03-31 Best     1.06102     1.07098   0.107931   
132 2010-03-31 Best     1.06102     1.07098   0.158042   
133 2010-03-31 Best     1.06102     1.07098   0.0872440  
134 2010-03-31 Best     1.06102     1.07098  -0.0340860  
⋮

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM