我有一些每月资产收益的数据,我想将这些资产合并到投资组合中。我想为每个月计算该月持有的所有资产的回报。
通过计算收益,我得到了两个不同的结果。
我运行以下代码:
方法1:
mean(data[data$date == "2007-06-29",]$monthly.returns)
Output1
[1] 0.1061636
方法2:
data %>%
tq_portfolio(assets_col = ID,
returns_col = monthly.returns,
col_rename = "returns",
rebalance_on = "months") %>%
filter(date == "2007-06-29")
Output2
# A tibble: 1 x 2
date returns
<date> <dbl>
1 2007-06-29 0.0212
方法3:
data %>%
group_by(date) %>%
summarise(ave_month_ret = mean(monthly.returns)) %>%
filter(date == "2007-06-29")
# A tibble: 1 x 2
date ave_month_ret
<date> <dbl>
1 2007-06-29 0.106
与方法1一致。我只是不知道方法2在使用什么计算方法...
返回值要小得多……而且我使用的是相同的数据/代码。
编辑 新数据:
data <- structure(list(ID = c("94106L10", "94106L10", "94106L10", "94106L10",
"94106L10", "94106L10", "94106L10", "94106L10", "94106L10", "94106L10",
"94106L10", "94106L10", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "50540R40", "50540R40", "50540R40", "50540R40",
"50540R40", "50540R40", "50540R40", "50540R40", "50540R40", "50540R40",
"50540R40", "50540R40", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "48116510", "48116510", "48116510", "48116510",
"48116510", "48116510", "48116510", "48116510", "48116510", "48116510",
"48116510", "48116510", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "12526910", "12526910", "12526910", "12526910",
"12526910", "12526910", "58283910", "58283910", "58283910", "58283910",
"58283910", "58283910", "58283910", "58283910", "58283910", "58283910",
"58283910", "58283910", "48116510", "48116510", "48116510", "48116510",
"48116510", "48116510", "48116510", "48116510", "48116510", "48116510",
"48116510", "48116510", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "48116510", "48116510", "48116510", "48116510",
"48116510", "48116510", "48116510", "48116510", "48116510", "48116510",
"48116510", "48116510", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "58943310", "58943310", "58943310", "58943310",
"58943310", "58943310", "58943310", "58943310", "58943310", "58943310",
"58943310", "58943310", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "58283910", "58283910", "58283910", "58283910",
"58283910", "58283910", "58283910", "58283910", "58283910", "58283910",
"58283910", "58283910", "90781810", "90781810", "90781810", "90781810",
"90781810", "90781810", "90781810", "90781810", "90781810", "90781810",
"90781810", "90781810", "22207020", "22207020", "22207020", "22207020",
"22207020", "22207020", "22207020", "22207020", "22207020", "22207020",
"22207020", "22207020", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "11012210", "11012210", "11012210", "11012210",
"11012210", "11012210", "11012210", "74144T10", "74144T10", "74144T10",
"74144T10", "74144T10", "74144T10", "74144T10", "12526910", "12526910",
"12526910", "12526910", "12526910", "12526910", "12526910"),
date = structure(c(13693, 13725, 13756, 13784, 13817, 13847,
13878, 13909, 13938, 13969, 13999, 14029, 13693, 13725, 13756,
13784, 13817, 13847, 13878, 13909, 13938, 13969, 13999, 14029,
14425, 14456, 14487, 14517, 14547, 14578, 14609, 14638, 14666,
14699, 14729, 14757, 14425, 14456, 14487, 14517, 14547, 14578,
14609, 14638, 14666, 14699, 14729, 14757, 14425, 14456, 14487,
14517, 14547, 14578, 14609, 14638, 14666, 14699, 14729, 14757,
14790, 14820, 14852, 14882, 14911, 14943, 14974, 15005, 15033,
15064, 15093, 15121, 14790, 14820, 14852, 14882, 14911, 14943,
14974, 15005, 15033, 15064, 15093, 15121, 15155, 15184, 15217,
15247, 15278, 15308, 15338, 15370, 15399, 15429, 15460, 15490,
15520, 15552, 15583, 15611, 15644, 15674, 15705, 15736, 15764,
15792, 15825, 15855, 15520, 15552, 15583, 15611, 15644, 15674,
15705, 15736, 15764, 15792, 15825, 15855, 15884, 15917, 15947,
15978, 16009, 16038, 16070, 16101, 16129, 16160, 16190, 16220,
15884, 15917, 15947, 15978, 16009, 16038, 16070, 16101, 16129,
16160, 16190, 16220, 15884, 15917, 15947, 15978, 16009, 16038,
16070, 16101, 16129, 16160, 16190, 16220, 15884, 15917, 15947,
15978, 16009, 16038, 16070, 16101, 16129, 16160, 16190, 16220,
15884, 15917, 15947, 15978, 16009, 16038, 16070, 16101, 16129,
16160, 16190, 16220, 16251, 16282, 16311, 16343, 16374, 16402,
16435, 16465, 16493, 16525, 16555, 16584, 16251, 16282, 16311,
16343, 16374, 16402, 16435, 16465, 16493, 16525, 16555, 16584,
16251, 16282, 16311, 16343, 16374, 16402, 16435, 16465, 16493,
16525, 16555, 16584, 16616, 16647, 16678, 16708, 16738, 16769,
16800, 16829, 16860, 16891, 16920, 16948, 16616, 16647, 16678,
16708, 16738, 16769, 16800, 16829, 16860, 16891, 16920, 16948,
16616, 16647, 16678, 16708, 16738, 16769, 16800, 16829, 16860,
16891, 16920, 16948, 16982, 17011, 17044, 17074, 17105, 17135,
17165, 17197, 17225, 17256, 17284, 17316, 16982, 17011, 17044,
17074, 17105, 17135, 17165, 17197, 17225, 17256, 17284, 17316,
16982, 17011, 17044, 17074, 17105, 17135, 17165, 17197, 17225,
17256, 17284, 17316, 17347, 17378, 17409, 17438, 17470, 17500,
17529, 17562, 17590, 17619, 17651, 17681, 17347, 17378, 17409,
17438, 17470, 17500, 17529, 17562, 17590, 17619, 17651, 17681,
17347, 17378, 17409, 17438, 17470, 17500, 17529, 17562, 17590,
17619, 17651, 17681, 17711, 17743, 17774, 17802, 17835, 17865,
17896, 17711, 17743, 17774, 17802, 17835, 17865, 17896, 17711,
17743, 17774, 17802, 17835, 17865, 17896), class = "Date"),
monthly.returns = c(-0.0227727694760084, -0.0261203707475507,
-0.00946622723920743, 0.00185833588861728, -0.0357711242389441,
-0.0568837518423548, -0.0480769679647677, -0.00795832066438062,
0.0129590245485958, 0.0222357447920023, 0.0756852502053702,
0.0506925736400397, 0.23509996528303, -0.0402404386704445,
0.101774570901307, 0.198642056960844, 0.157950168427595,
0.0350398381978172, 0.209716349721439, -0.0284390089301215,
0.141681487726935, -0.151212310022894, 0.29029138587484,
0.0239341587231359, 0.0962160534970569, -0.00885084033069661,
0.0386962104427435, -0.0586044406595664, 0.0485540729007834,
0.0590796883565639, 0.025767506537949, -0.0499732502241718,
0.031082969506351, 0.0327377111690756, 0.0377757316726486,
-0.0376734007378084, -0.006674674348077, 0.120950351259758,
-0.0301862529447343, 0.00883005590763375, 0.0663019415473178,
0.00410426359159222, 0.0882893862227756, -0.0681690341430085,
0.0215638798439521, 0.084434853224457, 0.0467527676505903,
-0.139381305703097, -0.0722062821678602, 0.0647424209775214,
0.0344565636759657, 0.0559637458961404, -0.0345587758427702,
0.0253453526768961, 0.0634957606629634, 0.0229125562791819,
0.144087843694531, -0.14175449794253, -0.0823645767832291,
-0.180231889100862, 0.0843478493068521, -0.000801942956512258,
0.0465489503956982, 0.0394938914746412, -0.00774625551801411,
-0.0617100323774203, 0.0491283590476501, -0.0490936276258981,
0.0250198233967893, 0.0240217300295384, 0.0631857758990213,
0.0131672215965088, -0.113795183581069, 0.0865059744398955,
-0.0920588569116774, 0.143297501255742, 0.103964885125911,
0.0553645982983011, 0.106463207432413, 0.0213820457676193,
0.0160801762353151, -0.00836078172471943, -0.032670855597221,
-0.0281712275998599, 0.00717743792807513, -0.058667565571353,
-0.0584506996376505, -0.106768869359527, 0.106133549220015,
0.0741861876916237, 0.00334747087781739, 0.015627732708084,
0.0648340247252268, 0.0602370983922462, -0.0334609689307233,
-0.0913412509075884, 0.140166564517708, -0.035101637811455,
0.0113580020857449, 0.0302734481107707, 0.0249604744929042,
-0.0032367307854908, 0.00691364497253999, 0.0966397419387792,
-0.00308081794455839, 0.0516926559765842, -0.0316549039620571,
0.0691034777411104, 0.212769987437646, 0.0104262875833523,
0.0574683301781924, 0.0735713800805162, -0.0767188743265328,
0.0430820031810046, -0.0507872502882032, 0.128027141356501,
-0.123663640809552, -0.0520838849698677, -0.0202763077657058,
0.0438046132087873, -0.0972842207128838, 0.0199876621698352,
-0.00767678925485249, 0.0390879891163065, 0.111873020808188,
-0.00334799342218495, 0.0341230597297544, -0.0974525662398461,
0.0418639713230742, 0.0545454545454545, 0.0410345011744,
-0.053494526086419, -0.0609372035423539, -0.032445670011178,
-0.0358465076686083, 0.1100983522136, 0.134831500498185,
-0.0217060049450167, 0.0344492102173604, -0.0598306719450883,
0.0760456102507803, -0.0338478645960549, -0.0358036685858628,
-0.00698739215521882, -0.0365886586715675, 0.0278688661280919,
-0.0677830757432401, 0.0255204010707701, 0.0761852263823679,
0.0394005332395586, 0.0411384240151262, -0.0636265299659813,
0.034803616044994, 0.0145373947492247, -0.00267153885585358,
-0.00730547295977324, -0.113969817301305, 0.142915419864933,
-0.0289270870253876, 0.107649500301504, 0.0226248837026684,
0.00825602378898527, 0.0720396911011352, -0.00935458607357742,
0.0868058270516758, 0.0388203307648247, -0.0593539579882222,
-0.0075865751284514, -0.0410312185542581, -0.0806513536324847,
0.0300659328873014, -0.0102625170677582, 0.0996498964853725,
0.0349007879783321, -0.00887468916153855, -0.0820200880058296,
0.0606061087150531, 0.0194971953205381, 0.061582905751375,
0.0137094839699294, 0.061358175546423, -0.0376746255364924,
0.0656429853952964, -0.136342049126051, -0.0350201652979498,
-0.0682120478762982, -0.0513866315399212, -0.0984523169816596,
0.0567477621851786, -0.115974716268977, 0.0883103383747428,
-0.0867730022490958, -0.0118150708749615, 0.0434962004402912,
0.000592702382994359, 0.010463943994673, 0.136967531843181,
0.0147791825167145, -0.000338703776833316, 0.021006296867851,
0.0107847636675746, 0.0587656260449405, -0.0119379915932356,
0.0136513249354131, 0.0327908992426151, -0.0799668251054702,
0.0429435864950873, -0.0320390287388762, 0.0470662594412536,
0.0168108776266995, 0.028633034080461, -0.0831586226286106,
0.0492885822313425, -0.0196125320473831, 0.00246970782849543,
-0.00603594311317535, -0.0777069869314776, -0.27044200854134,
-0.0829231558769659, -0.38356726967603, 0.150703278902144,
-0.106519202048837, -0.178501671446223, -0.209357609954451,
0.295887635933256, 0.243808018570374, 0.325451128908283,
-0.218309849065137, 0.0112461676208042, -0.0135257215734256,
-0.0939975354405369, -0.00454011185844061, 0.114020204363381,
0.0160728618201482, 0.0265631804589126, -0.0963803020769938,
-0.0037001213449307, 0.0314871750901062, 0.129931106735742,
-0.0120532383333837, -0.0317637700689766, -0.00771910559118838,
-0.0680669024627425, -0.0331107031769513, 0.088057593476,
0.00700871145261717, -0.0611950567135018, -0.00755351701865092,
-0.0259337056664409, 0.0629431114991126, 0.0249115417078927,
0.0244387875616459, 0.0208456520176783, 0.0495087594370764,
-0.0264316930765137, -0.0197963653645624, -0.127716926139201,
0.224917334203661, 0.0648065227409875, 0.0363482274846763,
0.0228385243605778, 0.0303029934300199, -0.0936532411759149,
-0.0785653037414443, -0.0515986625257318, -0.0312457001665778,
-0.0162682343494345, -0.0437158595844728, -0.0374435768987895,
0.157006640946117, 0.016203140928911, -0.103906450609303,
0.0559014903112138, -0.0429714590357441, 0.0402053969540854,
-0.0156580705305179, 0.0904830409618358, -0.0170799234353476,
-0.0463004192232698, -0.0712354247129779, -0.0536641612267443,
-0.0358432583322087, -0.0184490812764968, -0.00423972643726345,
0.245955181042198, 0.0146941667852203, -0.00404131794961526,
0.00710096082901068, -0.0205934653685186, -0.0546323053674277,
0.0227273114616595, 0.101329514337245, -0.00155212816488148,
0.0924950254753545, 0.0600790996325375, -0.00447431824166233,
-0.0243445692883895, 0.0320920742862265, -0.00595096211080237,
0.0936166583081903, -0.0172865333194544, 0.0916843972478469,
-0.19042967312998, -0.00301563554226025, -0.0683606183409371,
0.118831166820551, 0.154381887294401, -0.0140773649014047,
-0.0147884195054189, -0.0527950555836166, -0.0519125079342569,
-0.216714705833528, 0.0374668020538464, 0.114674602361112,
0.0198259110909933, 0.0745614138219373, 0.0248207386886548,
0.107857873826567, 0.0195297529342309, 0.0638520626013142,
0.00241874298221156, -0.0351206456799478, 0.0541817023986408,
0.0744157550818212, 0.0756073728272992, 0.0616190791111271,
0.0306382848861362, 0.0252684164041697, -0.185889184522457,
0.0577759817184391, -0.0276842418178466, -0.0576345966621118,
0.0257559271899821, -0.0267887335602566, -0.057899724909925,
-0.111650507486109, 0.0244355376717398, -0.0708534709368143,
0.0944047784043569, 0.000450374828359967, 0.169518300414036,
0.0479306607738461, -0.117744305824932, -0.121590678763565,
0.0312870286274094)), row.names = c(NA, -345L), class = c("tbl_df",
"tbl", "data.frame"))