在 R 中每周计算平均绝对百分比误差的问题

problem with calculating mean absolute percentage error on a weekly basis in R

我有大量时间序列的每个刻钟的实际和预测发电值,如下所示:

    df<-structure(list(DATETIME = structure(c(1604188800, 1604189700, 
1604190600, 1604191500, 1604192400, 1604193300, 1604194200, 1604195100, 
1604196000, 1604196900, 1604197800, 1604198700, 1604199600, 1604200500, 
1604201400, 1604202300, 1604203200, 1604204100, 1604205000, 1604205900, 
1604206800, 1604207700, 1604208600, 1604209500, 1604210400, 1604211300, 
1604212200, 1604213100, 1604214000, 1604214900, 1604215800, 1604216700, 
1604217600, 1604218500, 1604219400, 1604220300, 1604221200, 1604222100, 
1604223000, 1604223900, 1604224800, 1604225700, 1604226600, 1604227500, 
1604228400, 1604229300, 1604230200, 1604231100, 1604232000, 1604232900, 
1604233800, 1604234700, 1604235600, 1604236500, 1604237400, 1604238300, 
1604239200, 1604240100, 1604241000, 1604241900, 1604242800, 1604243700, 
1604244600, 1604245500, 1604246400, 1604247300, 1604248200, 1604249100, 
1604250000, 1604250900, 1604251800, 1604252700, 1604253600, 1604254500, 
1604255400, 1604256300, 1604257200, 1604258100, 1604259000, 1604259900, 
1604260800, 1604261700, 1604262600, 1604263500, 1604264400, 1604265300, 
1604266200, 1604267100, 1604268000, 1604268900, 1604269800, 1604270700, 
1604271600, 1604272500, 1604273400, 1604274300, 1604275200, 1604276100, 
1604277000, 1604277900, 1604278800, 1604279700, 1604280600, 1604281500, 
1604282400, 1604283300, 1604284200, 1604285100, 1604286000, 1604286900, 
1604287800, 1604288700, 1604289600, 1604290500, 1604291400, 1604292300, 
1604293200, 1604294100, 1604295000, 1604295900, 1604296800, 1604297700, 
1604298600, 1604299500, 1604300400, 1604301300, 1604302200, 1604303100, 
1604304000, 1604304900, 1604305800, 1604306700, 1604307600, 1604308500, 
1604309400, 1604310300, 1604311200, 1604312100, 1604313000, 1604313900, 
1604314800, 1604315700, 1604316600, 1604317500, 1604318400, 1604319300, 
1604320200, 1604321100, 1604322000, 1604322900, 1604323800, 1604324700, 
1604325600, 1604326500, 1604327400, 1604328300, 1604329200, 1604330100, 
1604331000, 1604331900, 1604332800, 1604333700, 1604334600, 1604335500, 
1604336400, 1604337300, 1604338200, 1604339100, 1604340000, 1604340900, 
1604341800, 1604342700, 1604343600, 1604344500, 1604345400, 1604346300, 
1604347200, 1604348100, 1604349000, 1604349900, 1604350800, 1604351700, 
1604352600, 1604353500, 1604354400, 1604355300, 1604356200, 1604357100, 
1604358000, 1604358900, 1604359800, 1604360700, 1604361600, 1604362500, 
1604363400, 1604364300, 1604365200, 1604366100, 1604367000, 1604367900, 
1604368800, 1604369700, 1604370600, 1604371500, 1604372400, 1604373300, 
1604374200, 1604375100, 1604376000, 1604376900, 1604377800, 1604378700, 
1604379600, 1604380500, 1604381400, 1604382300, 1604383200, 1604384100, 
1604385000, 1604385900, 1604386800, 1604387700, 1604388600, 1604389500, 
1604390400, 1604391300, 1604392200, 1604393100, 1604394000, 1604394900, 
1604395800, 1604396700, 1604397600, 1604398500, 1604399400, 1604400300, 
1604401200, 1604402100, 1604403000, 1604403900, 1604404800, 1604405700, 
1604406600, 1604407500, 1604408400, 1604409300, 1604410200, 1604411100, 
1604412000, 1604412900, 1604413800, 1604414700, 1604415600, 1604416500, 
1604417400, 1604418300, 1604419200, 1604420100, 1604421000, 1604421900, 
1604422800, 1604423700, 1604424600, 1604425500, 1604426400, 1604427300, 
1604428200, 1604429100, 1604430000, 1604430900, 1604431800, 1604432700, 
1604433600, 1604434500, 1604435400, 1604436300, 1604437200, 1604438100, 
1604439000, 1604439900, 1604440800, 1604441700, 1604442600, 1604443500, 
1604444400, 1604445300, 1604446200, 1604447100, 1604448000, 1604448900, 
1604449800, 1604450700, 1604451600, 1604452500, 1604453400, 1604454300, 
1604455200, 1604456100, 1604457000, 1604457900, 1604458800, 1604459700, 
1604460600, 1604461500, 1604462400, 1604463300, 1604464200, 1604465100, 
1604466000, 1604466900, 1604467800, 1604468700, 1604469600, 1604470500, 
1604471400, 1604472300, 1604473200, 1604474100, 1604475000, 1604475900, 
1604476800, 1604477700, 1604478600, 1604479500, 1604480400, 1604481300, 
1604482200, 1604483100, 1604484000, 1604484900, 1604485800, 1604486700, 
1604487600, 1604488500, 1604489400, 1604490300, 1604491200, 1604492100, 
1604493000, 1604493900, 1604494800, 1604495700, 1604496600, 1604497500, 
1604498400, 1604499300, 1604500200, 1604501100, 1604502000, 1604502900, 
1604503800, 1604504700, 1604505600, 1604506500, 1604507400, 1604508300, 
1604509200, 1604510100, 1604511000, 1604511900, 1604512800, 1604513700, 
1604514600, 1604515500, 1604516400, 1604517300, 1604518200, 1604519100, 
1604520000, 1604520900, 1604521800, 1604522700, 1604523600, 1604524500, 
1604525400, 1604526300, 1604527200, 1604528100, 1604529000, 1604529900, 
1604530800, 1604531700, 1604532600, 1604533500, 1604534400, 1604535300, 
1604536200, 1604537100, 1604538000, 1604538900, 1604539800, 1604540700, 
1604541600, 1604542500, 1604543400, 1604544300, 1604545200, 1604546100, 
1604547000, 1604547900, 1604548800, 1604549700, 1604550600, 1604551500, 
1604552400, 1604553300, 1604554200, 1604555100, 1604556000, 1604556900, 
1604557800, 1604558700, 1604559600, 1604560500, 1604561400, 1604562300, 
1604563200, 1604564100, 1604565000, 1604565900, 1604566800, 1604567700, 
1604568600, 1604569500, 1604570400, 1604571300, 1604572200, 1604573100, 
1604574000, 1604574900, 1604575800, 1604576700, 1604577600, 1604578500, 
1604579400, 1604580300, 1604581200, 1604582100, 1604583000, 1604583900, 
1604584800, 1604585700, 1604586600, 1604587500, 1604588400, 1604589300, 
1604590200, 1604591100, 1604592000, 1604592900, 1604593800, 1604594700, 
1604595600, 1604596500, 1604597400, 1604598300, 1604599200, 1604600100, 
1604601000, 1604601900, 1604602800, 1604603700, 1604604600, 1604605500, 
1604606400, 1604607300, 1604608200, 1604609100, 1604610000, 1604610900, 
1604611800, 1604612700, 1604613600, 1604614500, 1604615400, 1604616300, 
1604617200, 1604618100, 1604619000, 1604619900, 1604620800, 1604621700, 
1604622600, 1604623500, 1604624400, 1604625300, 1604626200, 1604627100, 
1604628000, 1604628900, 1604629800, 1604630700, 1604631600, 1604632500, 
1604633400, 1604634300, 1604635200, 1604636100, 1604637000, 1604637900, 
1604638800, 1604639700, 1604640600, 1604641500, 1604642400, 1604643300, 
1604644200, 1604645100, 1604646000, 1604646900, 1604647800, 1604648700, 
1604649600, 1604650500, 1604651400, 1604652300, 1604653200, 1604654100, 
1604655000, 1604655900, 1604656800, 1604657700, 1604658600, 1604659500, 
1604660400, 1604661300, 1604662200, 1604663100, 1604664000, 1604664900, 
1604665800, 1604666700, 1604667600, 1604668500, 1604669400, 1604670300, 
1604671200, 1604672100, 1604673000, 1604673900, 1604674800, 1604675700, 
1604676600, 1604677500, 1604678400, 1604679300, 1604680200, 1604681100, 
1604682000, 1604682900, 1604683800, 1604684700, 1604685600, 1604686500, 
1604687400, 1604688300, 1604689200, 1604690100, 1604691000, 1604691900, 
1604692800, 1604693700, 1604694600, 1604695500, 1604696400, 1604697300, 
1604698200, 1604699100, 1604700000, 1604700900, 1604701800, 1604702700, 
1604703600, 1604704500, 1604705400, 1604706300, 1604707200, 1604708100, 
1604709000, 1604709900, 1604710800, 1604711700, 1604712600, 1604713500, 
1604714400, 1604715300, 1604716200, 1604717100, 1604718000, 1604718900, 
1604719800, 1604720700, 1604721600, 1604722500, 1604723400, 1604724300, 
1604725200, 1604726100, 1604727000, 1604727900, 1604728800, 1604729700, 
1604730600, 1604731500, 1604732400, 1604733300, 1604734200, 1604735100, 
1604736000, 1604736900, 1604737800, 1604738700, 1604739600, 1604740500, 
1604741400, 1604742300, 1604743200, 1604744100, 1604745000, 1604745900, 
1604746800, 1604747700, 1604748600, 1604749500, 1604750400, 1604751300, 
1604752200, 1604753100, 1604754000, 1604754900, 1604755800, 1604756700, 
1604757600, 1604758500, 1604759400, 1604760300, 1604761200, 1604762100, 
1604763000, 1604763900, 1604764800, 1604765700, 1604766600, 1604767500, 
1604768400, 1604769300, 1604770200, 1604771100, 1604772000, 1604772900, 
1604773800, 1604774700, 1604775600, 1604776500, 1604777400, 1604778300, 
1604779200, 1604780100, 1604781000, 1604781900, 1604782800, 1604783700, 
1604784600, 1604785500, 1604786400, 1604787300, 1604788200, 1604789100, 
1604790000, 1604790900, 1604791800, 1604792700), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Actual = c(5.718, 5.844, 5.971, 5.925, 
5.414, 5.432, 5.521, 5.513, 5.524, 6.182, 6.947, 7.133, 7.746, 
7.058, 6.615, 9.665, 12.015, 12.029, 11.957, 12.981, 16.03, 16.821, 
16.897, 15.383, 14.029, 14.642, 10.721, 9.187, 8.466, 6.89, 5.916, 
7.977, 11.577, 12.432, 14.191, 14.655, 16.77, 17.376, 16.656, 
16.659, 16.249, 15.771, 15.969, 15.623, 14.488, 14.506, 14.37, 
13.399, 11.806, 10.78, 9.962, 10.093, 8.922, 10.099, 9.832, 8.406, 
7.077, 6.514, 5.942, 6.502, 5.731, 5.276, 7.513, 7.141, 6.74, 
6.36, 7.061, 8.619, 8.845, 9.808, 9.702, 10.079, 8.703, 7.287, 
7.239, 7.768, 7.338, 7.11, 6.975, 7.35, 6.209, 6.441, 6.641, 
5.892, 5.148, 4.533, 4.223, 3.89, 3.498, 2.941, 3.06, 3.244, 
3.521, 3.703, 3.314, 3.507, 3.705, 3.073, 2.472, 2.344, 2.695, 
2.729, 2.652, 2.582, 2.731, 2.759, 2.866, 3.283, 3.138, 3.009, 
3.126, 3.389, 3.205, 3.39, 3.942, 4.029, 4.186, 4.282, 4.335, 
4.026, 3.863, 3.772, 3.25, 3.282, 3.332, 3.18, 2.929, 3.054, 
3.579, 3.886, 3.145, 2.984, 3.305, 3.535, 3.59, 4.079, 4.141, 
3.957, 3.786, 3.505, 3.101, 2.892, 2.501, 2.243, 2.151, 1.96, 
1.907, 1.911, 1.901, 1.96, 1.854, 1.88, 2.25, 2.252, 2.154, 2.11, 
2.081, 2.27, 2.545, 2.864, 2.854, 3.257, 3.8, 4.158, 3.732, 3.771, 
4.19, 4.964, 4.323, 5.286, 4.407, 4.795, 5.225, 5.726, 5.732, 
5.836, 6.799, 6.322, 6.262, 5.851, 5.172, 5.007, 5.641, 5.812, 
4.964, 4.531, 5.07, 5.403, 5.176, 5.122, 5.618, 6.368, 5.941, 
6.191, 6.094, 6.703, 6.977, 6.391, 6.664, 6.671, 6.65, 6.687, 
7.659, 8.364, 8.088, 7.329, 7.634, 7.981, 8.941, 8.922, 8.898, 
8.921, 8.65, 9.39, 8.253, 7.656, 7.588, 6.226, 5.269, 4.915, 
4.918, 4.594, 4.873, 4.414, 4.384, 3.967, 3.432, 3.287, 3.48, 
3.374, 3.476, 4.54, 6.395, 6.67, 6.094, 5.212, 5.289, 4.99, 4.113, 
3.32, 3.09, 2.25, 2.42, 2.812, 2.544, 2.466, 2.892, 3.04, 2.665, 
2.303, 2.162, 1.896, 1.704, 1.683, 1.788, 1.841, 2.162, 2.288, 
2.108, 1.943, 1.582, 1.347, 1.256, 1.301, 1.649, 1.615, 1.669, 
1.855, 1.952, 2.354, 2.513, 2.314, 2.314, 2.623, 2.646, 2.655, 
2.795, 2.829, 3.225, 3.759, 4.926, 6.119, 6.206, 6.608, 6.237, 
5.74, 6.116, 8.257, 9.366, 9.073, 8.124, 6.595, 5, 4.428, 4.25, 
4.662, 6.04, 7.415, 6.713, 6.646, 6.287, 6.502, 6.023, 5.789, 
6.211, 7.477, 8.396, 9.687, 11.208, 9.911, 9.085, 8.758, 8.411, 
8.321, 8.393, 8.796, 9.682, 9.908, 9.637, 10.39, 11.094, 12.521, 
15.086, 14.875, 15.56, 15.396, 15.365, 16.238, 17.188, 17.16, 
17.902, 15.802, 14.354, 12.045, 11.883, 12.716, 13.031, 11.346, 
12.645, 13.082, 14.082, 14.606, 15.297, 15.215, 14.762, 15.61, 
17.65, 17.997, 17.933, 17.884, 17.323, 17.169, 19.862, 23.073, 
25.928, 27.872, 28.236, 30.207, 29.643, 28.742, 28.017, 25.973, 
26.97, 28.061, 26.099, 25.133, 23.174, 20.483, 19.969, 21.094, 
23.736, 26.382, 29.764, 33.129, 36.769, 38.491, 35.788, 35.61, 
37.34, 35.794, 35.368, 34.635, 33.84, 33.404, 32.614, 30.159, 
32.87, 33.89, 34.26, 33.309, 34.895, 34.596, 35.942, 37.642, 
38.688, 39.047, 39.552, 40.228, 41.329, 42.208, 43.073, 41.838, 
40.083, 40.6, 41.215, 41.782, 41.454, 41.65, 42.493, 42.297, 
42.925, 43.51, 42.537, 42.897, 42.832, 42.54, 43.833, 44.497, 
43.118, 42.778, 41.69, 40.37, 40.367, 43.267, 44.982, 46.84, 
46.205, 46.376, 43.302, 40.977, 41.712, 42.041, 42.881, 43.467, 
43.469, 45.591, 42.482, 45.196, 44.643, 43.234, 43.791, 45.037, 
44.114, 43.488, 42.825, 41.382, 41.102, 39.78, 40.251, 40.823, 
41.788, 43.272, 42.782, 41.288, 42.616, 44.222, 47.462, 48.783, 
48.18, 48.067, 47.608, 46.76, 49.739, 47.404, 47.416, 44.787, 
46.945, 49.951, 51.052, 50.017, 51.615, 51.965, 53.686, 54.802, 
57.408, 57.979, 57.367, 57.363, 53.659, 50.096, 47.254, 43.275, 
43.44, 42.811, 43.405, 47.903, 51.26, 52.405, 55.277, 54.637, 
54.626, 54.327, 53.697, 53.366, 54.955, 52.65, 51.526, 52.107, 
53.781, 50.12, 51.497, 54.426, 55.57, 54.662, 50.37, 54.62, 58.996, 
60.24, 57.378, 56.576, 56.589, 58.439, 59.768, 59.263, 56.691, 
59.286, 59.953, 60.097, 55.379, 49.2, 46.915, 52.828, 53.813, 
52.989, 53.829, 53.209, 54.683, 55.321, 56.154, 55.016, 54.061, 
53.296, 53.772, 52.342, 51.84, 52.607, 53.034, 54.727, 55.591, 
54.567, 53.198, 50.51, 49.453, 49.908, 48.499, 47.923, 49.602, 
48.64, 51.539, 52.353, 51.426, 50.687, 48.629, 46.685, 48.598, 
48.944, 48.143, 46.887, 47.518, 46.229, 45.746, 46.078, 47.155, 
46.933, 49.07, 49.243, 48.567, 48.352, 48.28, 48.954, 49.512, 
48.704, 49.681, 50.053, 50.957, 49.603, 47.978, 50, 50.963, 50.415, 
50.266, 50.155, 50.662, 51.918, 53.166, 52.792, 54.156, 55.07, 
53.702, 54.07, 54.098, 54.46, 53.783, 54.023, 53.997, 55.079, 
54.819, 54.935, 53.642, 54.181, 54.903, 56.18, 56.016, 55.307, 
53.558, 52.03, 54.562, 58.919, 58.351, 57.536, 59.128, 60.585, 
60.938, 60.858, 60.539, 59.114, 57.326, 58.094, 58.355, 58.507, 
57.85, 55.829, 57.801, 59.946, 58.755, 56.767, 55.223, 55.452, 
58.029, 60.207, 60.816, 60.403, 59.365, 58.132, 57.989, 59.469, 
58.991, 58.748, 58.43, 57.926, 58.671, 58.192, 58.335, 58.222, 
58.802, 58.458, 58.537, 59.014, 59.015, 57.656, 56.055, 53.788, 
54.659, 53.911, 54.722, 56.387, 57.327, 57.251, 57.596, 56.207, 
54.747, 53.934, 55.108, 55.669, 57.554, 57.824, 56.961, 54.605, 
54.705, 54.599, 54.044, 53.627), Forecasted = c(4.843, 4.843, 
4.843, 4.843, 3.695, 3.695, 3.695, 3.695, 3.313, 3.313, 3.313, 
3.313, 3.198, 3.198, 3.198, 3.198, 3.74, 3.74, 3.74, 3.74, 5.115, 
5.115, 5.115, 5.115, 7.01, 7.01, 7.01, 7.01, 9.236, 9.236, 9.236, 
9.236, 16.695, 16.695, 16.695, 16.695, 28.66, 28.66, 28.66, 28.66, 
34.778, 34.778, 34.778, 34.778, 35.03, 35.03, 35.03, 35.03, 32.805, 
32.805, 32.805, 32.805, 28.593, 28.593, 28.593, 28.593, 22.283, 
22.283, 22.283, 22.283, 19.164, 19.164, 19.164, 19.164, 17.839, 
17.839, 17.839, 17.839, 15.454, 15.454, 15.454, 15.454, 12.195, 
12.195, 12.195, 12.195, 8.568, 8.568, 8.568, 8.568, 7.477, 7.477, 
7.477, 7.477, 6.314, 6.314, 6.314, 6.314, 5.763, 5.763, 5.763, 
5.763, 4.615, 4.615, 4.615, 4.615, 3.57, 3.57, 3.57, 3.57, 3.438, 
3.438, 3.438, 3.438, 3.618, 3.618, 3.618, 3.618, 2.983, 2.983, 
2.983, 2.983, 2.513, 2.513, 2.513, 2.513, 2.075, 2.075, 2.075, 
2.075, 2.015, 2.015, 2.015, 2.015, 2.315, 2.315, 2.315, 2.315, 
2.325, 2.325, 2.325, 2.325, 2.363, 2.363, 2.363, 2.363, 2.058, 
2.058, 2.058, 2.058, 1.455, 1.455, 1.455, 1.455, 1.878, 1.878, 
1.878, 1.878, 2.165, 2.165, 2.165, 2.165, 2.633, 2.633, 2.633, 
2.633, 3.279, 3.279, 3.279, 3.279, 2.935, 2.935, 2.935, 2.935, 
2.833, 2.833, 2.833, 2.833, 2.89, 2.89, 2.89, 2.89, 3.952, 3.952, 
3.952, 3.952, 4.037, 4.037, 4.037, 4.037, 4.311, 4.311, 4.311, 
4.311, 4.242, 4.242, 4.242, 4.242, 2.478, 2.478, 2.478, 2.478, 
6.285, 6.285, 6.285, 6.285, 6.803, 6.803, 6.803, 6.803, 7.54, 
7.54, 7.54, 7.54, 8.383, 8.383, 8.383, 8.383, 8.86, 8.86, 8.86, 
8.86, 8.65, 8.65, 8.65, 8.65, 8.11, 8.11, 8.11, 8.11, 7.783, 
7.783, 7.783, 7.783, 6.995, 6.995, 6.995, 6.995, 5.98, 5.98, 
5.98, 5.98, 5.22, 5.22, 5.22, 5.22, 5.023, 5.023, 5.023, 5.023, 
7.343, 7.343, 7.343, 7.343, 7.29, 7.29, 7.29, 7.29, 7.315, 7.315, 
7.315, 7.315, 7.63, 7.63, 7.63, 7.63, 7.875, 7.875, 7.875, 7.875, 
7.993, 7.993, 7.993, 7.993, 5.315, 5.315, 5.315, 5.315, 5.787, 
5.787, 5.787, 5.787, 5.097, 5.097, 5.097, 5.097, 6.878, 6.878, 
6.878, 6.878, 6.337, 6.337, 6.337, 6.337, 4.025, 4.025, 4.025, 
4.025, 2.665, 2.665, 2.665, 2.665, 4.573, 4.573, 4.573, 4.573, 
6.248, 6.248, 6.248, 6.248, 5.534, 5.534, 5.534, 5.534, 6.268, 
6.268, 6.268, 6.268, 5.964, 5.964, 5.964, 5.964, 5.23, 5.23, 
5.23, 5.23, 7.155, 7.155, 7.155, 7.155, 9.775, 9.775, 9.775, 
9.775, 12.565, 12.565, 12.565, 12.565, 11.868, 11.868, 11.868, 
11.868, 11.473, 11.473, 11.473, 11.473, 10.698, 10.698, 10.698, 
10.698, 12.933, 12.933, 12.933, 12.933, 13.148, 13.148, 13.148, 
13.148, 12.845, 12.845, 12.845, 12.845, 11.588, 11.588, 11.588, 
11.588, 16.374, 16.374, 16.374, 16.374, 19.543, 19.543, 19.543, 
19.543, 21.938, 21.938, 21.938, 21.938, 22.425, 22.425, 22.425, 
22.425, 22.738, 22.738, 22.738, 22.738, 28.695, 28.695, 28.695, 
28.695, 29.352, 29.352, 29.352, 29.352, 34.76, 34.76, 34.76, 
34.76, 31.81, 31.81, 31.81, 31.81, 33.791, 33.791, 33.791, 33.791, 
35.23, 35.23, 35.23, 35.23, 35.308, 35.308, 35.308, 35.308, 35.13, 
35.13, 35.13, 35.13, 37.11, 37.11, 37.11, 37.11, 37.073, 37.073, 
37.073, 37.073, 40.596, 40.596, 40.596, 40.596, 40.7, 40.7, 40.7, 
40.7, 45.062, 45.062, 45.062, 45.062, 43.892, 43.892, 43.892, 
43.892, 44.33, 44.33, 44.33, 44.33, 42.638, 42.638, 42.638, 42.638, 
45.95, 45.95, 45.95, 45.95, 50.997, 50.997, 50.997, 50.997, 50.458, 
50.458, 50.458, 50.458, 49.61, 49.61, 49.61, 49.61, 50.95, 50.95, 
50.95, 50.95, 49.273, 49.273, 49.273, 49.273, 46.993, 46.993, 
46.993, 46.993, 47.368, 47.368, 47.368, 47.368, 48.526, 48.526, 
48.526, 48.526, 51.974, 51.974, 51.974, 51.974, 61.343, 61.343, 
61.343, 61.343, 61.38, 61.38, 61.38, 61.38, 61.763, 61.763, 61.763, 
61.763, 61.96, 61.96, 61.96, 61.96, 59.295, 59.295, 59.295, 59.295, 
59.98, 59.98, 59.98, 59.98, 59.233, 59.233, 59.233, 59.233, 56.633, 
56.633, 56.633, 56.633, 57.81, 57.81, 57.81, 57.81, 63.66, 63.66, 
63.66, 63.66, 67.34, 67.34, 67.34, 67.34, 67.59, 67.59, 67.59, 
67.59, 68.453, 68.453, 68.453, 68.453, 66.291, 66.291, 66.291, 
66.291, 62.495, 62.495, 62.495, 62.495, 61.142, 61.142, 61.142, 
61.142, 51.96, 51.96, 51.96, 51.96, 48.465, 48.465, 48.465, 48.465, 
51.932, 51.932, 51.932, 51.932, 54.498, 54.498, 54.498, 54.498, 
56.787, 56.787, 56.787, 56.787, 58.053, 58.053, 58.053, 58.053, 
57.649, 57.649, 57.649, 57.649, 54.156, 54.156, 54.156, 54.156, 
39.908, 39.908, 39.908, 39.908, 40.138, 40.138, 40.138, 40.138, 
41.303, 41.303, 41.303, 41.303, 41.175, 41.175, 41.175, 41.175, 
39.023, 39.023, 39.023, 39.023, 39.09, 39.09, 39.09, 39.09, 39.238, 
39.238, 39.238, 39.238, 44.308, 44.308, 44.308, 44.308, 41.595, 
41.595, 41.595, 41.595, 45.503, 45.503, 45.503, 45.503, 45.773, 
45.773, 45.773, 45.773, 46.703, 46.703, 46.703, 46.703, 51.37, 
51.37, 51.37, 51.37, 50.908, 50.908, 50.908, 50.908, 46.036, 
46.036, 46.036, 46.036, 43.526, 43.526, 43.526, 43.526, 38.05, 
38.05, 38.05, 38.05, 34.29, 34.29, 34.29, 34.29, 34.306, 34.306, 
34.306, 34.306, 37.668, 37.668, 37.668, 37.668, 41.563, 41.563, 
41.563, 41.563, 41.171, 41.171, 41.171, 41.171, 42.225, 42.225, 
42.225, 42.225, 43.463, 43.463, 43.463, 43.463)), row.names = c(NA, 
672L), class = "data.frame")

我想计算数据集每个(日历)周的平均绝对百分比误差,我尝试了几种方法,例如函数 apply.weekly,但其中 none 似乎工作。

apply.weekly(df, function(x) mean(abs((df$Actual-df$Forecasted)/df$Actual)))

我一直在为数据集的每个季度获取相同的值作为结果,而不是每周的结果。 你对如何实现这个计算有什么想法吗?

预先感谢您的帮助。

也许是这样:


library(lubridate)
library(magrittr)

df %>% group_by( isoweek( DATETIME ), year( DATETIME ) ) %>%
    summarise( DateCount = n_distinct(date(DATETIME)), MeanError = mean( abs(Forecasted-Actual)/Actual ) )

它输出:

  `isoweek(DATETIME)` `year(DATETIME)` DateCount MeanError
                <dbl>            <dbl>     <int>     <dbl>
1                  44             2020         1     0.858
2                  45             2020         6     0.357

使用更新后的数据源:


df <- read.delim( "https://raw.githubusercontent.com/Argiro1983/data-set/main/test1", sep=";" ) %>%
    as_tibble

df %<>% mutate( DATETIME = dmy_hm(DATETIME) )

df %>% group_by( year( DATETIME ), isoweek( DATETIME )  ) %>%
    summarise( DateCount = n_distinct(date(DATETIME)), MeanError = mean( abs(Forecasted-Actual)/Actual ) )

输出:


   `year(DATETIME)` `isoweek(DATETIME)` DateCount MeanError
              <dbl>               <dbl>     <int>     <dbl>
 1             2020                  44         1     0.858
 2             2020                  45         7     0.321
 3             2020                  46         7     0.505
 4             2020                  47         7     0.439
 5             2020                  48         7     0.309
 6             2020                  49         7     0.545
 7             2020                  50         7     0.342
 8             2020                  51         7     0.357
 9             2020                  52         7     0.204
10             2020                  53         4     0.120
# … with 14 more rows

如果您设置正确,日期应该可以正常工作。