条件总和 - 滚动回报

时间:2017-02-21 05:30:36

标签: r analysis

我正在努力对各种索引的投资组合进行滚动回报分析。

我的数据如下:

Date    Week Tag    A   B   C   D
01-Apr-02   A   1000.22 1000.297241 1001.34 1138.95
08-Apr-02   B   1001.98 1003.909643 1009.08 1135.25
15-Apr-02   C   1002.75 1003.856787 1008.17 1134.15
22-Apr-02   D   1004.16 1006.063778 1009.84 1104.15
01-May-02   A   1005.76 1007.869839 1010.72 1084.5
08-May-02   B   1007.12 1009.566546 1010.34 1117.6
15-May-02   C   1008.32 1009.770422 1005.98 1107.8
22-May-02   D   1009.36 1009.037303 999.96  1045.3
01-Jun-02   A   1011.98 1012.746664 1008.07 1028.8
08-Jun-02   B   1013.39 1015.132131 1010.55 1048.8
15-Jun-02   C   1014.63 1016.568085 1012.27 1085.7
22-Jun-02   D   1016.13 1018.41581  1013.99 1062.55
01-Jul-02   A   1018.14 1023.218417 1019.7  1068.95
08-Jul-02   B   1019.84 1026.083814 1024.45 1082.05
15-Jul-02   C   1021.14 1028.486688 1027.564172 1048
22-Jul-02   D   1023.5  1031.240441 1031.13 1012
01-Aug-02   A   1024.63 1033.465421 1035.21 957.7
08-Aug-02   B   1025.7  1035.236438 1039.2  953.55
15-Aug-02   C   1026.201655 1036.09057  1039.246449 969.65
22-Aug-02   D   1027.97 1037.762276 1041.19 985.7
01-Sep-02   A   1029.59 1040.871387 1046.37 1010.6
08-Sep-02   B   1030.64 1042.705834 1049.239327 995.2
15-Sep-02   C   1031.62 1043.697729 1050.24 992
22-Sep-02   D   1032.23 1045.010044 1052.024472 969.6
01-Oct-02   A   1034.05 1046.977936 1054.24 955.2
08-Oct-02   B   1035.31 1048.97826  1058.36 960.8
15-Oct-02   C   1036.06 1050.510523 1060.84 972.45
22-Oct-02   D   1037.24 1051.926384 1063.8  962.5
01-Nov-02   A   1039.35 1055.232479 1069.54 951.45
08-Nov-02   B   1039.87 1056.367703 1073.43 956.95
15-Nov-02   C   1040.76 1058.060143 1079.49 990.35
22-Nov-02   D   1042.35 1062.160215 1084.65 1020.15
01-Dec-02   A   1043.56 1064.627876 1088.51 1050.15
08-Dec-02   B   1044.23 1065.741083 1090.44 1069.8
15-Dec-02   C   1045.35 1066.969671 1092.22 1086.2
22-Dec-02   D   1046.64 1068.907587 1097.91 1079.3
01-Jan-03   A   1048.14 1068.977982 1107.35 1100.15
08-Jan-03   B   1049.39 1070.66448  1111.02 1089.35
15-Jan-03   C   1050.33 1072.508757 1115.6  1085
22-Jan-03   D   1051.33 1073.329042 1115.95 1082.9
01-Feb-03   A   1051.63 1070.731786 1099.07 1041.85
08-Feb-03   B   1052.69 1070.854545 1093.24 1057.5
15-Feb-03   C   1054.19 1070.15693  1084.34 1036
22-Feb-03   D   1055.21 1070.568894 1086.2  1066.15
01-Mar-03   A   1056.12 1075.540611 1103.52 1063.4
08-Mar-03   B   1057.33 1075.492233 1100.81 1017.1
15-Mar-03   C   1058.38 1075.849639 1101.09 999.65
22-Mar-03   D   1059.39 1077.036704 1104.86 1037.15
01-Apr-03   A   1061.03 1078.899938 1109.51 984.3
08-Apr-03   B   1062.7  1081.54 1120.99 1018.1
15-Apr-03   C   1063.71 1083    1125.4  951.2
22-Apr-03   D   1064.71 1085.38 1128.64 943.5
01-May-03   A   1066.26 1086.7  1129.2  934.05
08-May-03   B   1067.38 1088.09 1131.75 941.55
15-May-03   C   1068.1  1089.78 1136.39 959.85
22-May-03   D   1069.07 1091.97 1142.48 963.25
01-Jun-03   A   1070.16 1093.43 1146.05 1006.8
08-Jun-03   B   1070.85 1094.48 1148.66 1046.4
15-Jun-03   C   1071.71 1095.06 1148.46 1056.2
22-Jun-03   D   1072.58 1095.36 1149.9  1100.25
01-Jul-03   A   1073.82 1096.34 1149.77 1130.7
08-Jul-03   B   1074.68 1097.21 1150.12 1145.9
15-Jul-03   C   1075.55 1098.48 1152.59 1159.85
22-Jul-03   D   1076.42 1099.89 1154.23 1109.2
01-Aug-03   A   1077.66 1102.39 1158.51 1195.75
08-Aug-03   B   1078.53 1103.03 1160.09 1222.65
15-Aug-03   C   1079.28 1103.5  1160.65 1247.75
22-Aug-03   D   1080.28 1104.48 1161.25 1311.15
01-Sep-03   A   1081.99 1110.28 1173.57 1375.95
08-Sep-03   B   1082.78 1110.74 1173.11 1417.35
15-Sep-03   C   1083.41 1110.98 1172.21 1329.25
22-Sep-03   D   1084.1  1112.67 1174.15 1302.9
01-Oct-03   A   1085.14 1118.15 1184.37 1420.85
08-Oct-03   B   1086.28 1122.37 1191.28 1478.6
15-Oct-03   C   1086.76 1124.63 1196.04 1537
22-Oct-03   D   1087.53 1122.64 1189.52 1494.1
01-Nov-03   A   1088.79 1121.45 1186.81 1555.9
08-Nov-03   B   1089.59 1121.9  1188.07 1592.05
15-Nov-03   C   1090.49 1120.69 1184.87 1562.8
22-Nov-03   D   1091.56 1120.05 1182.52 1540.7
01-Dec-03   A   1092.61 1119.75 1179.61 1657.65
08-Dec-03   B   1093.58 1121.41 1182.17 1646.25
15-Dec-03   C   1094.35 1125.38 1187.27 1723.95
22-Dec-03   D   1094.78 1127.79 1192.93 1789.15
01-Jan-04   A   1095.99 1129.56 1195.79 1912.25
08-Jan-04   B   1096.83 1130.5  1197.22 1968.55
15-Jan-04   C   1097.66 1130.77 1196.82 1944.45
22-Jan-04   D   1098.48 1130.76 1196.17 1770.5
01-Feb-04   A   1099.46 1130.17 1193.04 1809.75
08-Feb-04   B   1100.3  1131    1193.91 1833.65
15-Feb-04   C   1101.41 1129.7  1189.97 1913.6
22-Feb-04   D   1102.04 1129.02 1189.05 1852.65
01-Mar-04   A   1103.15 1131.99 1193.2  1852.7
08-Mar-04   B   1103.96 1134.27 1196.61 1885.25
15-Mar-04   C   1104.98 1137.07 1200.87 1763.4
22-Mar-04   D   1105.8  1138.56 1203.42 1685
01-Apr-04   A   1106.86 1141.88 1208.51 1819.65
08-Apr-04   B   1107.73 1144.11 1211.48 1853.55
15-Apr-04   C   1108.62 1145.34 1213.75 1861.95
22-Apr-04   D   1109.65 1146.1  1214.51 1889.55
01-May-04   A   1110.29 1146.35 1212.8  1796.1
08-May-04   B   1111.56 1147.19 1213    1804.45
15-May-04   C   1112.4  1146.48 1209.43 1582.4
22-May-04   D   1113.04 1146.21 1208.78 1560.2
01-Jun-04   A   1114.3  1145.92 1205.53 1507.9
08-Jun-04   B   1115.15 1146.89 1206.53 1550.55
15-Jun-04   C   1115.91 1142.64 1196.9  1501
22-Jun-04   D   1116.62 1141.45 1190.16 1474.7
01-Jul-04   A   1117.73 1143.1  1189.1  1537.2
08-Jul-04   B   1118.2  1144.28 1189.2  1518.15
15-Jul-04   C   1119.35 1144.03 1185.78 1539.4
22-Jul-04   D   1120.2  1145.66 1187.27 1598.1
01-Aug-04   A   1121.24 1144.47 1180.77 1632.3
08-Aug-04   B   1122.02 1144.06 1177.71 1633.4
15-Aug-04   C   1122.7  1137.81 1163.45 1598.2
22-Aug-04   D   1123.08 1141    1168.38 1590.35
01-Sep-04   A   1124.79 1146.57 1182.88 1635.45
08-Sep-04   B   1125.72 1147.68 1185.15 1656.25
15-Sep-04   C   1126.39 1147.07 1182.03 1683.2
22-Sep-04   D   1127.25 1149.7  1185.19 1753.9
01-Oct-04   A   1128.22 1149.92 1182.86 1775.15
08-Oct-04   B   1128.66 1147.61 1178.63 1820.2
15-Oct-04   C   1129.57 1149.09 1177.94 1795
22-Oct-04   D   1130.08 1149.62 1179.18 1779.75
01-Nov-04   A   1131.39 1149.97 1177.02 1797.75
08-Nov-04   B   1132.18 1149.15 1170.18 1862.8
15-Nov-04   C   1132.58 1149.17 1170.06 1872.95
22-Nov-04   D   1134.01 1150.71 1172.38 1873.35
01-Dec-04   A   1135.57 1154.08 1178.13 1962.05
08-Dec-04   B   1136.93 1155.34 1183.26 1977.95
15-Dec-04   C   1138.11 1156.44 1184.21 2028.7
22-Dec-04   D   1138.83 1157.68 1187.08 2035.35
01-Jan-05   A   1140.21 1159.81 1190.65 2080.5
08-Jan-05   B   1141.13 1161.03 1193.28 2015.5
15-Jan-05   C   1142.13 1162.01 1192.63 1931.1
22-Jan-05   D   1143.13 1162.86 1193.48 1925.3
01-Feb-05   A   1144.87 1165.1  1196.16 2059.85
08-Feb-05   B   1145.77 1166.64 1199.72 2055.15
15-Feb-05   C   1146.9  1168.41 1202.66 2089.95
22-Feb-05   D   1147.7  1169.66 1204.33 2058.4
01-Mar-05   A   1148.78 1170.55 1203.64 2084.4
08-Mar-05   B   1149.36 1171.7  1206.15 2168.95
15-Mar-05   C   1150.61 1173.11 1207.15 2128.95
22-Mar-05   D   1151.54 1174.18 1207.16 2061.6
01-Apr-05   A   1152.97 1176.03 1209.32 2067.65
08-Apr-05   B   1153.94 1176.55 1206.38 2031.2
15-Apr-05   C   1155.33 1177.89 1207.24 1956.3
22-Apr-05   D   1155.83 1178.74 1208.14 1967.35
01-May-05   A   1157.47 1179.71 1205.95 1902.5
08-May-05   B   1158.44 1180.95 1207.95 1977.5
15-May-05   C   1159.49 1182.48 1211.72 1988.3
22-May-05   D   1160.47 1183.87 1215.5  1992.4
01-Jun-05   A   1161.99 1185.58 1218.01 2087.55
08-Jun-05   B   1163.1  1187.27 1223.52 2112.4
15-Jun-05   C   1164    1188.03 1222.69 2128.65
22-Jun-05   D   1165.07 1189.36 1225.81 2187.35
01-Jul-05   A   1166.38 1190.71 1226.76 2211.9
08-Jul-05   B   1167.36 1191.5  1225.48 2196.2
15-Jul-05   C   1168.4  1193.31 1225.91 2212.55
22-Jul-05   D   1169.26 1194.45 1227.58 2265.6
01-Aug-05   A   1170.77 1196.6  1232.24 2318.05
08-Aug-05   B   1172.07 1197.81 1233.29 2324.4
15-Aug-05   C   1172.92 1198.65 1233.45 2361.55
22-Aug-05   D   1174.31 1200.17 1235.27 2367.85
01-Sep-05   A   1175.62 1201.89 1237.12 2405.75
08-Sep-05   B   1176.65 1203.38 1239.47 2454.45
15-Sep-05   C   1177.62 1204.69 1240.93 2523.95
22-Sep-05   D   1178.71 1205.93 1241.73 2476.5
01-Oct-05   A   1180.03 1207.37 1241.78 2601.4
08-Oct-05   B   1180.62 1207.99 1242.08 2574.05
15-Oct-05   C   1181.67 1209.1  1243.58 2484.4
22-Oct-05   D   1182.42 1209.13 1243.59 2443.75
01-Nov-05   A   1183.98 1210.97 1246.29 2386.75
08-Nov-05   B   1185.26 1212.43 1247.04 2492.65
15-Nov-05   C   1186.16 1213.5  1248.5  2558.7
22-Nov-05   D   1187.36 1214.17 1249.73 2572.85
01-Dec-05   A   1188.48 1215.36 1251.32 2698.95
08-Dec-05   B   1189.55 1216.12 1252.07 2706.7
15-Dec-05   C   1190.92 1215.07 1250.83 2778.55
22-Dec-05   D   1191.75 1211.08 1246.65 2835.25
01-Jan-06   A   1192.7  1212.4  1248.42 2836.55
08-Jan-06   B   1194.01 1216    1252.82 2914
15-Jan-06   C   1195.3  1216.92 1253.54 2850.55
22-Jan-06   D   1196.5  1218.08 1254.61 2900.95
01-Feb-06   A   1198.95 1217.12 1250.83 2971.55
08-Feb-06   B   1200.36 1218.38 1251.23 3008.95
15-Feb-06   C   1201.79 1218.57 1250.94 3022.2
22-Feb-06   D   1201.85 1218.93 1251.26 3050.8
01-Mar-06   A   1203.01 1217.79 1249.02 3123.1
08-Mar-06   B   1203.78 1215.52 1244.91 3116.7
15-Mar-06   C   1204.96 1216.77 1245.88 3195.35
22-Mar-06   D   1206.79 1218.57 1247.93 3240.15
01-Apr-06   A   1208.82 1220.91 1248.87 3402.55
08-Apr-06   B   1213.08 1225.48 1252.25 3454.8
15-Apr-06   C   1214.44 1227.29 1253.55 3345.5
22-Apr-06   D   1216.34 1231.04 1258.15 3573.05
01-May-06   A   1217.58 1232.59 1259.44 3557.6
08-May-06   B   1219.2  1234.02 1259.97 3693.15
15-May-06   C   1220.8  1235.63 1261.07 3502.95
22-May-06   D   1222.03 1236.6  1261.89 3081.35
01-Jun-06   A   1223.72 1238.58 1263.41 2962.25
08-Jun-06   B   1224.86 1239.42 1263.86 2724.35
15-Jun-06   C   1225.92 1240.45 1263.82 2798.8
22-Jun-06   D   1226.78 1241.21 1263.3  2994.75
01-Jul-06   A   1228.21 1241.79 1261.73 3128.2
08-Jul-06   B   1229.65 1243.25 1262.78 3075.85
15-Jul-06   C   1231.09 1244.79 1263.68 3123.35
22-Jul-06   D   1232.38 1248.04 1265.39 2945
01-Aug-06   A   1234.48 1251.74 1267.47 3147.8
08-Aug-06   B   1235.83 1253.53 1268.84 3212.4
15-Aug-06   C   1236.99 1255.16 1271.46 3313.1
22-Aug-06   D   1238.42 1256.67 1273.24 3364.6
01-Sep-06   A   1240.15 1258.7  1275.76 3435.45
08-Sep-06   B   1241.47 1260.42 1278.37 3471.45
15-Sep-06   C   1242.68 1261.67 1278.89 3478.6
22-Sep-06   D   1244.04 1263.09 1281.26 3544.05
01-Oct-06   A   1245.71 1264.89 1283.36 3588.4
08-Oct-06   B   1246.89 1266.33 1285.06 3569.7
15-Oct-06   C   1248.35 1267.76 1286.59 3676.05
22-Oct-06   D   1249.76 1268.87 1287.54 3683.5
01-Nov-06   A   1252.22 1271.16 1290.59 3767.05
08-Nov-06   B   1253.47 1272.12 1291.81 3777.3
15-Nov-06   C   1254.9  1273.46 1294.34 3876.3
22-Nov-06   D   1256.39 1274.83 1296.78 3954.75
01-Dec-06   A   1258.31 1276.77 1299.57 3997.6
08-Dec-06   B   1259.81 1278.37 1301.59 3962
15-Dec-06   C   1260.62 1278.29 1298.49 3888.65
22-Dec-06   D   1261.97 1278.9  1299.38 3871.15
01-Jan-07   A   1263.48 1278.7  1297.47 3966.4
08-Jan-07   B   1266.59 1281.09 1298.79 3933.4
15-Jan-07   C   1268.08 1282.32 1299.27 4078.4
22-Jan-07   D   1269.76 1282.83 1298.79 4102.45
01-Feb-07   A   1271.87 1285.08 1300.75 4137.2
08-Feb-07   B   1273.5  1286.13 1301.51 4223.4
15-Feb-07   C   1274.16 1284.91 1297    4146.2
22-Feb-07   D   1275.91 1283.71 1296.45 4040
01-Mar-07   A   1277.34 1283.69 1294.86 3811.2
08-Mar-07   B   1278.94 1284.86 1294.8  3761.65
15-Mar-07   C   1280.72 1286.77 1294.08 3643.6
22-Mar-07   D   1283.07 1288.37 1295.76 3875.9
01-Apr-07   A   1285.85 1290.28 1295.33 3821.55
08-Apr-07   B   1291.86 1294.06 1296.97 3752
15-Apr-07   C   1295.43 1296.81 1299.54 3917.35
22-Apr-07   D   1296.58 1297.76 1300.96 4083.55
01-May-07   A   1299.78 1300.22 1303.63 4087.9
08-May-07   B   1302.33 1303.77 1304.2  4077
15-May-07   C   1304.71 1306.28 1303.27 4120.3
22-May-07   D   1307.39 1309.61 1306.22 4278.1
01-Jun-07   A   1311.7  1314.34 1310.27 4297.05
08-Jun-07   B   1313.89 1317.99 1312.97 4145
15-Jun-07   C   1314.11 1318.72 1313.45 4171.45
22-Jun-07   D   1316.71 1321.84 1316.73 4252.05
01-Jul-07   A   1318.08 1324.5  1319.11 4318.3
08-Jul-07   B   1321.24 1329.95 1325.45 4384.85
15-Jul-07   C   1323.99 1336    1334.58 4504.55
22-Jul-07   D   1325.18 1341.91 1346.12 4566.05
01-Aug-07   A   1324.99 1341.25 1345.89 4345.85
08-Aug-07   B   1326.15 1342.06 1345.98 4462.1
15-Aug-07   C   1325.87 1339.18 1340.49 4370.2
22-Aug-07   D   1327.38 1340.64 1341.15 4153.15
01-Sep-07   A   1329    1341.94 1341.12 4464
08-Sep-07   B   1331.31 1344.77 1343.47 4509.5
15-Sep-07   C   1332.72 1348.65 1346.41 4518
22-Sep-07   D   1335.3  1352.27 1351.24 4837.55
01-Oct-07   A   1337.7  1354.67 1353.35 5068.95
08-Oct-07   B   1340.06 1358.23 1357.46 5085.1
15-Oct-07   C   1341.85 1360.91 1359.8  5670.4
22-Oct-07   D   1343.53 1363.68 1363.74 5184
01-Nov-07   A   1345.06 1366.07 1368.18 5866.45
08-Nov-07   B   1345.27 1366.27 1367.85 5698.75
15-Nov-07   C   1346.66 1367.6  1369.69 5912.1
22-Nov-07   D   1348.4  1368.9  1371.45 5519.35
01-Dec-07   A   1350.99 1371.58 1375.11 5762.75
08-Dec-07   B   1352.57 1373.67 1378.01 5974.3
15-Dec-07   C   1354.17 1374.46 1378.18 6047.7
22-Dec-07   D   1356.03 1376.54 1380.78 5766.5
01-Jan-08   A   1359.83 1383.16 1390.94 6144.35
08-Jan-08   B   1361.65 1386.53 1396.32 6287.85
15-Jan-08   C   1363.01 1388.43 1400.54 6074.25
22-Jan-08   D   1364.61 1390.15 1402.43 4899.3
01-Feb-08   A   1367.05 1392.11 1403.59 5317.25
08-Feb-08   B   1368.86 1394.03 1406.16 5120.35
15-Feb-08   C   1369.75 1393.9  1404.74 5302.9
22-Feb-08   D   1370.48 1392.48 1400.06 5110.75
01-Mar-08   A   1372.88 1394.91 1402.41 5223.5
08-Mar-08   B   1374.38 1396.12 1403.17 4771.6
15-Mar-08   C   1376.45 1396.59 1403.84 4745.8
22-Mar-08   D   1378.03 1398.25 1404.64 4573.95
01-Apr-08   A   1382.85 1404.31 1402.21 4739.55
08-Apr-08   B   1387.94 1408.24 1400.75 4709.65
15-Apr-08   C   1389.88 1401.82 1401.89 4879.65
22-Apr-08   D   1390.7  1410.81 1399.28 5049.3
01-May-08   A   1393.22 1415.78 1407.74 5165.9
08-May-08   B   1395.86 1418.98 1412.42 5081.7
15-May-08   C   1397.35 1420.06 1414    5115.25
22-May-08   D   1398.97 1420.15 1411.03 5025.45
01-Jun-08   A   1400.99 1422.46 1411.03 4870.1
08-Jun-08   B   1402.2  1422.96 1408.64 4627.8
15-Jun-08   C   1403.77 1422.05 1404.25 4517.1
22-Jun-08   D   1405.51 1420.19 1395.23 4347.55
01-Jul-08   A   1407.7  1418.38 1386.29 3896.75
08-Jul-08   B   1410.28 1418.95 1380.72 3988.55
15-Jul-08   C   1411.78 1419.48 1379.24 3861.1
22-Jul-08   D   1413.94 1421.16 1382.38 4240.1
01-Aug-08   A   1417.34 1422.69 1383.47 4413.55
08-Aug-08   B   1419.85 1425.76 1387.76 4529.5
15-Aug-08   C   1421.5  1427.823    1390.2  4430.7
22-Aug-08   D   1424.04 1429.12 1390.68 4327.45
01-Sep-08   A   1427.46 1432.89 1394.7  4348.65
08-Sep-08   B   1430.14 1435.4  1397.49 4482.3
15-Sep-08   C   1432.4  1437.75 1401.22 4072.9
22-Sep-08   D   1434.64 1438.65 1401.16 4223.05
01-Oct-08   A   1437.77 1441.48 1403.02 3950.75
08-Oct-08   B   1439.72 1440.87 1400.4  3513.65
15-Oct-08   C   1442.4  1442.29 1401.91 3338.4
22-Oct-08   D   1445.42 1448.08 1408.95 3065.15
01-Nov-08   A   1448.67 1449.61 1409.09 2885.6
08-Nov-08   B   1452.57 1453.15 1409.15 2973
15-Nov-08   C   1455.32 1456.37 1415.35 2810.35
22-Nov-08   D   1458.12 1461.13 1424.56 2693.45
01-Dec-08   A   1462.31 1467.14 1431.11 2682.9
08-Dec-08   B   1465.5  1484.28 1459.02 2784
15-Dec-08   C   1467.88 1495.1  1479.75 2981.2
22-Dec-08   D   1470.67 1505.65 1500.26 3039.3
01-Jan-09   A   1474.69 1512.35 1513.54 3033.45
08-Jan-09   B   1477.82 1517.8  1504.29 2920.4
15-Jan-09   C   1481.24 1526.16 1516.7  2736.7
22-Jan-09   D   1483.78 1524.45 1508.82 2713.8
01-Feb-09   A   1486.23 1525.53 1504.76 2874.8
08-Feb-09   B   1488.6  1529.78 1505.28 2843.1
15-Feb-09   C   1490.93 1533.54 1507.91 2948.35
22-Feb-09   D   1493.65 1536.49 1508.15 2736.45
01-Mar-09   A   1496.11 1539.75 1510.38 2763.65
08-Mar-09   B   1498.07 1541.08 1508.17 2620.15
15-Mar-09   C   1499.24 1534.94 1490.49 2719.25
22-Mar-09   D   1501.18 1539.94 1506.07 2807.05
01-Apr-09   A   1504.63 1541.81 1505.33 3060.35
08-Apr-09   B   1509.96 1552.99 1518.18 3342.95
15-Apr-09   C   1511.9  1557.92 1527.64 3484.15
22-Apr-09   D   1515.7  1571.78 1557.86 3330.3
01-May-09   A   1516.54 1569.84 1549.59 3473.95
08-May-09   B   1518.64 1574.21 1550.34 3620.7
15-May-09   C   1520    1574.82 1546.54 3671.65
22-May-09   D   1520.88 1574.21 1541.81 4238.5
01-Jun-09   A   1522.08 1571.33 1534.83 4529.9
08-Jun-09   B   1523.03 1571.46 1535.31 4429.9
15-Jun-09   C   1523.79 1571.71 1533.99 4484
22-Jun-09   D   1524.54 1573.78 1536.4  4235.25
01-Jul-09   A   1525.61 1580.23 1543.92 4340.9
08-Jul-09   B   1526.26 1582.28 1542.12 4078.9
15-Jul-09   C   1527    1586.49 1547.37 4233.5
22-Jul-09   D   1527.68 1585.49 1546.83 4398.9
01-Aug-09   A   1528.54 1585.1  1543.1  4636.45
08-Aug-09   B   1529.26 1582.68 1537.87 4481.4
15-Aug-09   C   1530    1582.87 1537.85 4580.05
22-Aug-09   D   1530.88 1583.4  1535.47 4528.8
01-Sep-09   A   1532.14 1582.37 1529.35 4625.35
08-Sep-09   B   1532.99 1583.18 1532.44 4805.25
15-Sep-09   C   1533.81 1585.22 1536.23 4892.1
22-Sep-09   D   1534.66 1587.71 1541.81 5020.2
01-Oct-09   A   1535.78 1592.26 1543.67 5083.4
08-Oct-09   B   1536.5  1591.3  1543.39 5002.25
15-Oct-09   C   1537.24 1591.67 1539.21 5108.85
22-Oct-09   D   1538.1  1594.37 1544.51 4988.6
01-Nov-09   A   1539.04 1597.95 1548.15 4711.7
08-Nov-09   B   1539.81 1601.49 1553.08 4796.15
15-Nov-09   C   1540.58 1604.69 1556.44 4998.95
22-Nov-09   D   1541.34 1609.04 1564.53 5052.45
01-Dec-09   A   1542.48 1608.55 1565.83 5122
08-Dec-09   B   1543.19 1608.41 1564.44 5147.95
15-Dec-09   C   1544.01 1610.41 1564.71 5033.05
22-Dec-09   D   1544.8  1609.97 1564.47 4985.85
01-Jan-10   A   1546.1  1613.87 1567.67 5201.05
08-Jan-10   B   1546.85 1615.17 1569.37 5244.75
15-Jan-10   C   1547.77 1616.2  1571.18 5252.2
22-Jan-10   D   1548.76 1619.26 1574.04 5036
01-Feb-10   A   1550.17 1620.97 1575.72 4899.7
08-Feb-10   B   1551.12 1621.23 1574.9  4760.4
15-Feb-10   C   1552.19 1620.59 1571.45 4801.95
22-Feb-10   D   1553.31 1620.39 1570.82 4856.4
01-Mar-10   A   1554.03 1621.98 1574.18 4922.3
08-Mar-10   B   1555.89 1624.94 1575.08 5124
15-Mar-10   C   1557.21 1628.28 1578.89 5128.9
22-Mar-10   D   1558.49 1629.68 1581.65 5205.2
01-Apr-10   A   1560.37 1632.55 1587.09 5290.5
08-Apr-10   B   1561.61 1634.19 1590    5304.45
15-Apr-10   C   1562.53 1634.86 1587.67 5273.6
22-Apr-10   D   1563.62 1639.58 1595.25 5269.35
01-May-10   A   1564.79 1643.38 1600.53 5278
08-May-10   B   1565.78 1644.81 1604.33 5018.05
15-May-10   C   1566.76 1645.4  1605.83 5093.5
22-May-10   D   1567.74 1646.25 1608.11 4931.15
01-Jun-10   A   1569.5  1647.51 1607.28 4970.2
08-Jun-10   B   1570.95 1649.87 1609.3  4987.1
15-Jun-10   C   1572.31 1650.66 1609.6  5222.35
22-Jun-10   D   1573.86 1652.59 1612.1  5316.55
01-Jul-10   A   1575.9  1654.61 1614.59 5251.4
08-Jul-10   B   1577.47 1655.51 1615.62 5296.85
15-Jul-10   C   1579.09 1656.05 1615.34 5378.75
22-Jul-10   D   1580.65 1657    1616.77 5441.95
01-Aug-10   A   1582.48 1655.29 1614.01 5367.6
08-Aug-10   B   1584.21 1656.48 1613.35 5439.25
15-Aug-10   C   1585.92 1658.06 1615.79 5452.1
22-Aug-10   D   1587.47 1659.56 1615.98 5530.65
01-Sep-10   A   1590.45 1662.88 1619.37 5471.85
08-Sep-10   B   1592.16 1664.65 1621    5607.85
15-Sep-10   C   1593.9  1666.18 1622.16 5860.95
22-Sep-10   D   1595.76 1667.91 1624.25 5991
01-Oct-10   A   1598.1  1670.52 1627.59 6143.4
08-Oct-10   B   1599.93 1672.05 1628.17 6103.45
15-Oct-10   C   1601.71 1672.09 1628.09 6062.65
22-Oct-10   D   1603.65 1673.06 1628.7  6066.05
01-Nov-10   A   1606.51 1676.17 1631.28 6117.55
08-Nov-10   B   1608.54 1678.66 1635.78 6273.2
15-Nov-10   C   1610.52 1679.77 1635.62 6121.6
22-Nov-10   D   1612.6  1681.49 1637.75 6010
01-Dec-10   A   1615.27 1681.92 1637.57 5960.9
08-Dec-10   B   1617.43 1680.56 1633.78 5903.7
15-Dec-10   C   1619.76 1682.77 1636.45 5892.3
22-Dec-10   D   1622.07 1685.25 1641.15 5984.4
01-Jan-11   A   1625.13 1688.32 1644.23 6134.5
08-Jan-11   B   1627.43 1690.6  1643.57 5904.6
15-Jan-11   C   1629.7  1691.08 1641.88 5654.55
22-Jan-11   D   1632.02 1690.14 1640.42 5696.5
01-Feb-11   A   1635.71 1693.04 1642.92 5417.2
08-Feb-11   B   1638.15 1693.66 1643.94 5312.55
15-Feb-11   C   1640.59 1696.64 1647.59 5481
22-Feb-11   D   1643.14 1699.53 1650.89 5469.2
01-Mar-11   A   1645.7  1701.95 1653.95 5522.3
08-Mar-11   B   1648.35 1704.86 1657.9  5520.8
15-Mar-11   C   1650.97 1708.02 1661.63 5449.65
22-Mar-11   D   1653.52 1710.83 1663.11 5413.85
01-Apr-11   A   1657.41 1716.42 1667.45 5826.05
08-Apr-11   B   1659.96 1720.73 1670.21 5842
15-Apr-11   C   1662.09 1722.9  1670.93 5824.55
22-Apr-11   D   1664    1722.81 1671.07 5884.7
01-May-11   A   1666.56 1725.16 1672.95 5749.5
08-May-11   B   1668.89 1724.4  1672.23 5551.45
15-May-11   C   1671.39 1725.74 1672.95 5544.75
22-May-11   D   1673.82 1728.21 1674.24 5486.35
01-Jun-11   A   1678.13 1730.51 1673.73 5592
08-Jun-11   B   1680.75 1733.87 1677.37 5526.85
15-Jun-11   C   1683.37 1738.86 1680.42 5447.5
22-Jun-11   D   1685.96 1742.84 1684.97 5278.3
01-Jul-11   A   1689.1  1747.43 1688.09 5627.2
08-Jul-11   B   1691.28 1750.9  1692.17 5660.65
15-Jul-11   C   1693.83 1755.82 1699.4  5581.1
22-Jul-11   D   1696.23 1759.39 1700.2  5633.95
01-Aug-11   A   1699.69 1761.09 1701.23 5516.8
08-Aug-11   B   1702.31 1766.2  1707.04 5118.5
15-Aug-11   C   1703.75 1767.65 1708.59 5072.95
22-Aug-11   D   1707.47 1771.6  1711.29 4898.8
01-Sep-11   A   1710.36 1774.44 1713.36 5001
08-Sep-11   B   1713.71 1777.76 1716.23 5153.25
15-Sep-11   C   1716.27 1779.98 1717.29 5075.7
22-Sep-11   D   1718.95 1782.12 1718.98 4923.65
01-Oct-11   A   1721.98 1784    1718.92 4943.25
08-Oct-11   B   1724.53 1786.33 1718.46 4888.05
15-Oct-11   C   1727.15 1787.92 1719.15 5132.3
22-Oct-11   D   1729.83 1790.17 1720.92 5049.95
01-Nov-11   A   1734.09 1795.44 1724.87 5257.95
08-Nov-11   B   1736.77 1798.22 1728.27 5289.35
15-Nov-11   C   1739.5  1800.54 1729.02 5068.5
22-Nov-11   D   1742.28 1803.69 1734.18 4812.35
01-Dec-11   A   1745.89 1807.69 1741.23 4936.85
08-Dec-11   B   1748.66 1812.1  1750.8  4943.65
15-Dec-11   C   1751.44 1815.15 1755.35 4746.35
22-Dec-11   D   1754.16 1817.86 1759.43 4733.85
01-Jan-12   A   1757.55 1820.74 1757.68 4624.3
08-Jan-12   B   1760.28 1823.78 1767.02 4746.9
15-Jan-12   C   1763.109    1826.648    1769.412    4866
22-Jan-12   D   1766.03 1830.59 1774.9  5048.6
01-Feb-12   A   1771.08 1836.12 1779.47 5235.7
08-Feb-12   B   1774.04 1839.64 1781.96 5368.15
15-Feb-12   C   1776.95 1842.22 1784.91 5531.95
22-Feb-12   D   1780.01 1845.24 1787.89 5505.35
01-Mar-12   A   1783.35 1847.98 1789.92 5339.75
08-Mar-12   B   1786.05 1848.8  1790.23 5220.45
15-Mar-12   C   1789.77 1851.26 1790.46 5380.5
22-Mar-12   D   1793.13 1854.26 1792.44 5228.45
01-Apr-12   A   1796.96 1858.13 1795.12 5295.55
08-Apr-12   B   1799.59 1860.54 1794.97 5322.9
15-Apr-12   C   1803.55 1867.66 1804.65 5207.45
22-Apr-12   D   1806.59 1871.34 1808.6  5290.85
01-May-12   A   1810.56 1874.26 1810.7  5248.15
08-May-12   B   1813.83 1877.81 1814.41 4999.95
15-May-12   C   1816.77 1879.85 1816.39 4942.8
22-May-12   D   1819.71 1882.23 1818.53 4860.5
01-Jun-12   A   1823.97 1886.93 1823.28 4841.6
08-Jun-12   B   1826.91 1891.17 1827.7  5068.35
15-Jun-12   C   1829.78 1895.09 1831.23 5139.05
22-Jun-12   D   1832.56 1897.34 1832.31 5146.05
01-Jul-12   A   1835.48 1900.63 1834.39 5278.9
08-Jul-12   B   1838.2  1904.73 1837.99 5316.95
15-Jul-12   C   1840.95 1907.82 1841.74 5227.25
22-Jul-12   D   1843.79 1912.91 1848.54 5205.1
01-Aug-12   A   1848.51 1917.39 1850.28 5240.5
08-Aug-12   B   1851.34 1922.2  1855.76 5338
15-Aug-12   C   1853.68 1924.25 1856.96 5380.35
22-Aug-12   D   1856.79 1927.87 1859.43 5412.85
01-Sep-12   A   1860.23 1931.78 1862.92 5258.5
08-Sep-12   B   1862.92 1935.06 1867.04 5358.7
15-Sep-12   C   1865.56 1939.05 1871.67 5577.65
22-Sep-12   D   1868.23 1943.17 1876.64 5691.15
01-Oct-12   A   1872.06 1949.93 1883.64 5718.8
08-Oct-12   B   1874.71 1952.75 1887.32 5676
15-Oct-12   C   1877.41 1957.84 1891.53 5687.25
22-Oct-12   D   1880.08 1961.14 1895.97 5717.15
01-Nov-12   A   1883.93 1961.84 1893.7  5645.05
08-Nov-12   B   1886.64 1965    1897.23 5738.75
15-Nov-12   C   1889.43 1967.95 1900.25 5631
22-Nov-12   D   1892.23 1970.43 1901.39 5627.75
01-Dec-12   A   1895.39 1974.32 1906.42 5879.85
08-Dec-12   B   1898.12 1976.65 1909.84 5907.4
15-Dec-12   C   1900.91 1979.59 1913.5  5879.6
22-Dec-12   D   1903.7  1982.08 1915.74 5847.7
01-Jan-13   A   1908.06 1988.17 1926.37 5950.85
08-Jan-13   B   1910.9  1992.35 1935.56 6001.7
15-Jan-13   C   1913.66 1996.74 1944.78 6056.6
22-Jan-13   D   1916.48 1998.42 1942.38 6048.5
01-Feb-13   A   1920.41 2001.24 1941.95 5998.9
08-Feb-13   B   1923.19 2003.9  1946.22 5903.5
15-Feb-13   C   1926.04 2005.48 1950.53 5887.4
22-Feb-13   D   1929.07 2007.59 1951.77 5850.3
01-Mar-13   A   1932.08 2010.59 1950.1  5719.7
08-Mar-13   B   1935.2  2015.18 1955.86 5945.7
15-Mar-13   C   1938.31 2020.34 1961.18 5872.6
22-Mar-13   D   1941.34 2023.26 1957.7  5651.35

类似的数据集已有很多年了。

指数的单位是指数值的倒数。我想在一定时期内获得累积单位。例如 - 对于5年的时间段,我的第一次迭代将是......从01-01-2002到01-01-2007(期间所有A单位的总和)...然后08-01-2002到08 -01-2007(期间所有B单位的总和)等等......我想要在几年内积累的相应单位总和......

我尝试了各种和函数,但没有一个能给我正确的结果。

我的代码:

library(dplyr)
library(date)
Goal_Period <- 5
Goal_Amount <- 10000
NAV$Date <- as.Date(NAV$Date)
NAV$Unit_A <- 1/NAV$A
NAV$Unit_B <- 1/NAV$B
NAV$Unit_C <- 1/NAV$C
NAV$Unit_D <- 1/NAV$D
NAV$From_Date <- NAV$Date
y <- as.POSIXlt(NAV$From_Date)
y$year <- y$year + Goal_Period
y <- as.date(y)
NAV$To_Date <- y
NAV$To_Date <- as.Date(NAV$To_Date)
NAV$From_Date <- as.Date(NAV$From_Date)
NAV$Date <- as.Date(NAV$Date)

总结数据:

NAV$CU_A2 <- sum(NAV$Unit_A[NAV$From_Date <= NAV$Date & NAV$To_Date > NAV$Date])

这为所有日期范围提供相同的金额:

NAV$CU_A <- sapply(seq_len(nrow(NAV)),function(i) with (NAV, sum(Unit_A[NAV$From_Date < Date[i] & NAV$To_Date >= Date[i] & NAV$Week.Tag == Week.Tag[i]])))

这给出了一些结果,但我无法理解这段代码

Analysis$CU_A3 <- sum(NAV$Unit_A[NAV$Date >= Analysis$From_Date & NAV$Date < Analysis$To_Date])

这是一个警告消息 - 较长的对象长度不是较短对象长度的倍数

预期产出:

From_Date   To_Date A   B   C   D
01-Apr-02   01-Apr-07   0.05322592  0.05229192  0.05086775  0.03577431
08-Apr-02   08-Apr-07   0.05317151  0.05223121  0.05079648  0.03544052
15-Apr-02   15-Apr-07   0.05312343  0.05219724  0.05079003  0.03553256
22-Apr-02   22-Apr-07   0.05307237  0.05214680  0.05073264  0.03562883
01-May-02   01-May-07   0.05300383  0.05206724  0.05064109  0.03515798
08-May-02   08-May-07   0.05294756  0.05200787  0.05057651  0.03482618
15-May-02   15-May-07   0.05289812  0.05197221  0.05056764  0.03490611
22-May-02   22-May-07   0.05284777  0.05192339  0.05051105  0.03496804
01-Jun-02   01-Jun-07   0.05277892  0.05184415  0.05041879  0.03448052
08-Jun-02   08-Jun-07   0.05272248  0.05178435  0.05035349  0.03417669
15-Jun-02   15-Jun-07   0.05267282  0.05174742  0.05034088  0.03424613
22-Jun-02   22-Jun-07   0.05262193  0.05169593  0.05027657  0.03424513

1 个答案:

答案 0 :(得分:0)

此代码提供与您预期完全相同的结果(您在<中混淆了<= / > / >= / sapply条件:< / p>

f <- function(row) { //current row
  unitA_filtered <- NAV$Unit_A[row["From_Date"] <= NAV$Date & row["To_Date"] > NAV$Date & NAV$Week == row["Week"]]
  sum(unitA_filtered) 
}

NAV$UNIT_SUM_A = apply(NAV, 1, f)

解释:

  • apply是通过NAV计算总和的显式循环,NAV$Unit_A[condition]是嵌套的&#34;隐式&#34;循环过滤NAV$Unit_A中的数据以在求和之前准备。
  • NAV$Unit_A[condition]filter(condition)中的dyplr相似。例如,NAV$Date是一个代表当前行Date的变量。在遍历UnitA时,NAV$Date会从中获取每个值。
  • NAV$Unit_A[row["From_Date"] <= NAV$Date & row["To_Date"] > NAV$Date检查日期范围
  • NAV$Week == row["Week"]检查来自给定NAV行(NAV$Week)的周等于Arow["Week"]

结果:

> NAV$UNIT_SUM_A
  [1] 0.0532259165 0.0531715059 0.0531234305 0.0530723684 0.0530038321
  [6] 0.0529475597 0.0528981174 0.0528477710 0.0527789201 0.0527224839
 [11] 0.0526728226 0.0526219269 0.0525531277 0.0524967957 0.0524482130
 [16] 0.0523972695 0.0523296238 0.0522731145 0.0522242081 0.0521748444

我使用的准备代码(您的as.Date无法在我的RStudio中工作):

library(dplyr)
library(date)
Goal_Period <- 5
Goal_Amount <- 10000
NAV$Date <- as.Date(NAV$Date, "%d-%b-%Y")
NAV$Unit_A <- 1/NAV$A
NAV$Unit_B <- 1/NAV$B
NAV$Unit_C <- 1/NAV$C
NAV$Unit_D <- 1/NAV$D
NAV$From_Date <- NAV$Date
y <- as.POSIXlt(NAV$From_Date)
y$year <- y$year + Goal_Period
y <- as.Date(y)
NAV$To_Date <- y
NAV$To_Date <- as.Date(NAV$To_Date)
NAV$From_Date <- as.Date(NAV$From_Date)

P.S。如您所见,代码为O(N*N)。通过在某些表中保存部分和(它们可以重新计算几次)或单独的累加器,可以更有效地解决您的任务,但代码将不太容易理解。