r anomalize::time_decomposition错误:`time_scale == key_value`

qvtsj1bj  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(65)

我正在尝试使用anomalize包分析时间序列中的异常。每秒记录数据。当我试图调用time_decompose函数时,我得到了一个错误,如下所示。

library(tidyverse)
library(lubridate)
library(tibbletime)
library(anomalize)

structure(list(t = c(1001, 1002, 1003, 1004, 1005, 1006, 1007, 
                     1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 
                     1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 
                     1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 
                     1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 
                     1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 
                     1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 
                     1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 
                     1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 
                     1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 
                     1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 
                     1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 
                     1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 
                     1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 
                     1151, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 
                     1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, 1172, 
                     1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1182, 1183, 
                     1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 
                     1195, 1196, 1197, 1198, 1199, 1200, 1201, 1202, 1203, 1204, 1205, 
                     1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 
                     1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 
                     1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 
                     1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 
                     1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 
                     1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 
                     1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279, 1280, 1281, 1282, 
                     1283, 1284, 1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 
                     1294, 1295, 1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 
                     1305, 1306, 1307, 1308, 1309, 1310, 1311, 1312, 1313, 1314, 1315, 
                     1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 
                     1327, 1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 
                     1338, 1339, 1340, 1341, 1342, 1343, 1344, 1345, 1346, 1347, 1348, 
                     1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 
                     1360, 1361, 1362, 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1370, 
                     1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1380, 1381, 
                     1382, 1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391, 1392, 
                     1393, 1394, 1395, 1396, 1397, 1398, 1399), value = c(118.62, 
                                                                          121.57, 121.08, 118.5, 118.09, 115.28, 115.8, 111.83, 116.73, 
                                                                          120.34, 120.11, 124.51, 125.28, 127.2, 128.45, 124.24, 122.7, 
                                                                          121, 116.26, 114.12, 111.08, 109.83, 107.71, 109.31, 108.4, 106.59, 
                                                                          103.34, 104.02, 106.15, 105.9, 105.96, 104.79, 104.04, 103.45, 
                                                                          102.07, 99.71, 97.9, 99.12, 100.45, 99, 97.3, 96.11, 95.09, 95.98, 
                                                                          95.3, 92.88, 93.1, 91.2099999999999, 85.21, 85.6, 82.9500000000001, 
                                                                          80.85, 77.41, 78.66, 77.93, 73.88, 72.68, 71.09, 67.04, 68.25, 
                                                                          70.23, 67.86, 67.94, 69.44, 68.5, 67.11, 65.8899999999999, 64.7299999999999, 
                                                                          66.4900000000001, 67.2099999999999, 68.5400000000001, 69.56, 
                                                                          66.68, 67.24, 68.79, 69.74, 72.43, 73.17, 75.39, 79.2, 80.72, 
                                                                          83.04, 84.73, 87.82, 88.7, 92.38, 95.55, 97.0499999999999, 97.32, 
                                                                          97.59, 97.97, 97.96, 100.63, 100.77, 104.89, 105.38, 109.1, 107.84, 
                                                                          107.78, 105.08, 106.36, 103.95, 107.74, 107.58, 109.69, 112.46, 
                                                                          115.77, 117.11, 121.28, 123.4, 127.66, 127.01, 130.15, 131.31, 
                                                                          130.12, 129.88, 129.22, 128.48, 126.17, 127.26, 128.93, 127.57, 
                                                                          127.93, 128.92, 128.53, 128.72, 129.58, 129.12, 126.49, 127.31, 
                                                                          125.46, 125.09, 127.55, 129.01, 128.58, 128.7, 128.04, 127.13, 
                                                                          125.86, 124.27, 124.5, 125.45, 124.67, 126.13, 127.71, 126.17, 
                                                                          127.14, 125.11, 122.11, 120.4, 118.86, 117.7, 113.78, 109.02, 
                                                                          109.34, 108.79, 105.44, 108.11, 105.5, 106.75, 104.14, 100.34, 
                                                                          99.8099999999999, 95.9300000000001, 97.44, 97.63, 96.53, 97.25, 
                                                                          94.95, 93.06, 92.09, 86.22, 83.3400000000001, 77.04, 77.37, 74.88, 
                                                                          77.41, 78.21, 78.77, 81.79, 80.51, 84.71, 88.06, 88.25, 86.5300000000001, 
                                                                          86.6099999999999, 86.57, 89.06, 89.3499999999999, 90.92, 89.9100000000001, 
                                                                          90.26, 88.7699999999999, 88.08, 86.2699999999999, 84.93, 83.81, 
                                                                          83.18, 83.39, 84.43, 87.67, 87.23, 90.16, 89.88, 94.14, 97.64, 
                                                                          99.49, 100.61, 102.85, 103.01, 104.28, 106.94, 107.88, 107.5, 
                                                                          111.68, 110.92, 113.76, 113.8, 114.3, 115.95, 116.02, 113.34, 
                                                                          114.1, 118.21, 120.39, 122.09, 123.47, 122.46, 120.2, 117.88, 
                                                                          120.75, 119.2, 119.76, 120.43, 121.09, 120.36, 121.01, 120.87, 
                                                                          119.03, 120.17, 118.65, 118.13, 118.57, 118.84, 120.07, 119.93, 
                                                                          122.45, 125.04, 126.96, 124.86, 127.39, 129.6, 129.93, 127.71, 
                                                                          124.05, 120.7, 118.72, 116.6, 114.48, 110.81, 105.67, 97.48, 
                                                                          93.07, 95.25, 91.2100000000001, 89.47, 83.41, 84.85, 83.2500000000001, 
                                                                          80.89, 85.66, 86.3000000000001, 87.1499999999999, 87.1800000000001, 
                                                                          89.01, 91.27, 96.7400000000001, 100.41, 103.36, 108.56, 110.08, 
                                                                          109.31, 111, 109.77, 112.32, 114.05, 112.37, 114.59, 114.84, 
                                                                          114.31, 115.14, 115.38, 116.1, 112.19, 108.39, 106.65, 103.69, 
                                                                          100.23, 96.56, 92.07, 88.1000000000001, 80.79, 80.2800000000001, 
                                                                          76.8999999999999, 72.3900000000001, 70.37, 66.37, 66.8700000000001, 
                                                                          64.9199999999999, 63.23, 61.96, 53.95, 58.94, 59.9199999999999, 
                                                                          61, 60.3500000000001, 60.01, 57.33, 58.25, 59.37, 61.28, 62.61, 
                                                                          62.16, 63.38, 65.02, 68.9599999999999, 70.56, 71.54, 67.54, 69.34, 
                                                                          71.69, 73.87, 74.94, 78.94, 82.14, 84.88, 86.9299999999999, 90.26, 
                                                                          92.2500000000001, 95.15, 96.98, 99.2799999999999, 100.41, 105.08, 
                                                                          106.06, 106.95, 107.14, 106.84, 106.68, 104.91, 106.05, 102.63, 
                                                                          104.43, 103.92, 103.85, 103.71, 99.32, 103.02, 100.79, 101.5, 
                                                                          106.55, 108.73, 109.68, 112.88, 116.66, 116.59, 119.13, 122.81, 
                                                                          124.18, 127.61, 127.22, 129.81, 130.39, 131.11, 130.48, 126.35, 
                                                                          125.6, 118.47, 118.32, 116.57, 111.24, 109.76, 108.27, 105.05, 
                                                                          104.8, 103.92, 106.12, 108.32, 105.49, 104.84, 105.95, 103.16, 
                                                                          99.33, 98.52, 94.97, 92.6499999999999, 94.0800000000001, 92.09, 
                                                                          89.09)), row.names = c(NA, -399L), class = c("tbl_df", "tbl", 
                                                                                                                       "data.frame")) %>% 
  mutate(t = make_datetime(sec = t)) %>% 
  as_tbl_time(index = t) %>% 
  time_decompose(value, merge = TRUE)
#> Error in `dplyr::filter()`:
#> ℹ In argument: `time_scale == key_value`.
#> Caused by error:
#> ! `..1` must be of size 8 or 1, not size 0.

#> Backtrace:
#>      ▆
#>   1. ├─... %>% time_decompose(value, merge = TRUE)
#>   2. ├─anomalize::time_decompose(., value, merge = TRUE)
#>   3. ├─anomalize:::time_decompose.tbl_time(., value, merge = TRUE)
#>   4. │ └─data %>% ...
#>   5. ├─anomalize::decompose_stl(...)
#>   6. │ └─anomalize::time_frequency(data, period = frequency, message = message)
#>   7. │   └─template %>% ...
#>   8. ├─anomalize:::target_time_decomposition_scale(...)
#>   9. │ └─template %>% dplyr::filter(time_scale == key_value) %>% ...
#>  10. ├─dplyr::pull(., !!target_expr)
#>  11. ├─dplyr::filter(., time_scale == key_value)
#>  12. ├─dplyr:::filter.data.frame(., time_scale == key_value)
#>  13. │ └─dplyr:::filter_rows(.data, dots, by)
#>  14. │   └─dplyr:::filter_eval(dots, mask = mask, error_call = error_call)
#>  15. │     ├─base::withCallingHandlers(...)
#>  16. │     └─mask$eval_all_filter(dots, env_filter)
#>  17. │       └─dplyr (local) eval()
#>  18. ├─dplyr:::dplyr_internal_error(...)
#>  19. │ └─rlang::abort(class = c(class, "dplyr:::internal_error"), dplyr_error_data = data)
#>  20. │   └─rlang:::signal_abort(cnd, .file)
#>  21. │     └─base::signalCondition(cnd)
#>  22. └─dplyr (local) `<fn>`(`<dpl:::__>`)
#>  23.   └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

字符串
创建于2023-02-14带有reprex v2.0.2
如果我将时间序列的采样率更改为make_datetime中的一分钟(不正确),则time_decompose函数可以正常工作。但是,我想在正确的单位(秒)上进行此分析。当然,数据集是真实的数据集的一小部分,但它允许您重现错误。

library(tidyverse)
library(lubridate)
library(tibbletime)
library(anomalize)

structure(list(t = c(1001, 1002, 1003, 1004, 1005, 1006, 1007, 
                     1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 
                     1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 
                     1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 
                     1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 
                     1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 
                     1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 
                     1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 
                     1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 
                     1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 
                     1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 
                     1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 
                     1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 
                     1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 
                     1151, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 
                     1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, 1172, 
                     1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1182, 1183, 
                     1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 
                     1195, 1196, 1197, 1198, 1199, 1200, 1201, 1202, 1203, 1204, 1205, 
                     1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 
                     1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 
                     1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 
                     1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 
                     1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 
                     1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 
                     1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279, 1280, 1281, 1282, 
                     1283, 1284, 1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 
                     1294, 1295, 1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 
                     1305, 1306, 1307, 1308, 1309, 1310, 1311, 1312, 1313, 1314, 1315, 
                     1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 
                     1327, 1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 
                     1338, 1339, 1340, 1341, 1342, 1343, 1344, 1345, 1346, 1347, 1348, 
                     1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 
                     1360, 1361, 1362, 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1370, 
                     1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1380, 1381, 
                     1382, 1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391, 1392, 
                     1393, 1394, 1395, 1396, 1397, 1398, 1399), value = c(118.62, 
                                                                          121.57, 121.08, 118.5, 118.09, 115.28, 115.8, 111.83, 116.73, 
                                                                          120.34, 120.11, 124.51, 125.28, 127.2, 128.45, 124.24, 122.7, 
                                                                          121, 116.26, 114.12, 111.08, 109.83, 107.71, 109.31, 108.4, 106.59, 
                                                                          103.34, 104.02, 106.15, 105.9, 105.96, 104.79, 104.04, 103.45, 
                                                                          102.07, 99.71, 97.9, 99.12, 100.45, 99, 97.3, 96.11, 95.09, 95.98, 
                                                                          95.3, 92.88, 93.1, 91.2099999999999, 85.21, 85.6, 82.9500000000001, 
                                                                          80.85, 77.41, 78.66, 77.93, 73.88, 72.68, 71.09, 67.04, 68.25, 
                                                                          70.23, 67.86, 67.94, 69.44, 68.5, 67.11, 65.8899999999999, 64.7299999999999, 
                                                                          66.4900000000001, 67.2099999999999, 68.5400000000001, 69.56, 
                                                                          66.68, 67.24, 68.79, 69.74, 72.43, 73.17, 75.39, 79.2, 80.72, 
                                                                          83.04, 84.73, 87.82, 88.7, 92.38, 95.55, 97.0499999999999, 97.32, 
                                                                          97.59, 97.97, 97.96, 100.63, 100.77, 104.89, 105.38, 109.1, 107.84, 
                                                                          107.78, 105.08, 106.36, 103.95, 107.74, 107.58, 109.69, 112.46, 
                                                                          115.77, 117.11, 121.28, 123.4, 127.66, 127.01, 130.15, 131.31, 
                                                                          130.12, 129.88, 129.22, 128.48, 126.17, 127.26, 128.93, 127.57, 
                                                                          127.93, 128.92, 128.53, 128.72, 129.58, 129.12, 126.49, 127.31, 
                                                                          125.46, 125.09, 127.55, 129.01, 128.58, 128.7, 128.04, 127.13, 
                                                                          125.86, 124.27, 124.5, 125.45, 124.67, 126.13, 127.71, 126.17, 
                                                                          127.14, 125.11, 122.11, 120.4, 118.86, 117.7, 113.78, 109.02, 
                                                                          109.34, 108.79, 105.44, 108.11, 105.5, 106.75, 104.14, 100.34, 
                                                                          99.8099999999999, 95.9300000000001, 97.44, 97.63, 96.53, 97.25, 
                                                                          94.95, 93.06, 92.09, 86.22, 83.3400000000001, 77.04, 77.37, 74.88, 
                                                                          77.41, 78.21, 78.77, 81.79, 80.51, 84.71, 88.06, 88.25, 86.5300000000001, 
                                                                          86.6099999999999, 86.57, 89.06, 89.3499999999999, 90.92, 89.9100000000001, 
                                                                          90.26, 88.7699999999999, 88.08, 86.2699999999999, 84.93, 83.81, 
                                                                          83.18, 83.39, 84.43, 87.67, 87.23, 90.16, 89.88, 94.14, 97.64, 
                                                                          99.49, 100.61, 102.85, 103.01, 104.28, 106.94, 107.88, 107.5, 
                                                                          111.68, 110.92, 113.76, 113.8, 114.3, 115.95, 116.02, 113.34, 
                                                                          114.1, 118.21, 120.39, 122.09, 123.47, 122.46, 120.2, 117.88, 
                                                                          120.75, 119.2, 119.76, 120.43, 121.09, 120.36, 121.01, 120.87, 
                                                                          119.03, 120.17, 118.65, 118.13, 118.57, 118.84, 120.07, 119.93, 
                                                                          122.45, 125.04, 126.96, 124.86, 127.39, 129.6, 129.93, 127.71, 
                                                                          124.05, 120.7, 118.72, 116.6, 114.48, 110.81, 105.67, 97.48, 
                                                                          93.07, 95.25, 91.2100000000001, 89.47, 83.41, 84.85, 83.2500000000001, 
                                                                          80.89, 85.66, 86.3000000000001, 87.1499999999999, 87.1800000000001, 
                                                                          89.01, 91.27, 96.7400000000001, 100.41, 103.36, 108.56, 110.08, 
                                                                          109.31, 111, 109.77, 112.32, 114.05, 112.37, 114.59, 114.84, 
                                                                          114.31, 115.14, 115.38, 116.1, 112.19, 108.39, 106.65, 103.69, 
                                                                          100.23, 96.56, 92.07, 88.1000000000001, 80.79, 80.2800000000001, 
                                                                          76.8999999999999, 72.3900000000001, 70.37, 66.37, 66.8700000000001, 
                                                                          64.9199999999999, 63.23, 61.96, 53.95, 58.94, 59.9199999999999, 
                                                                          61, 60.3500000000001, 60.01, 57.33, 58.25, 59.37, 61.28, 62.61, 
                                                                          62.16, 63.38, 65.02, 68.9599999999999, 70.56, 71.54, 67.54, 69.34, 
                                                                          71.69, 73.87, 74.94, 78.94, 82.14, 84.88, 86.9299999999999, 90.26, 
                                                                          92.2500000000001, 95.15, 96.98, 99.2799999999999, 100.41, 105.08, 
                                                                          106.06, 106.95, 107.14, 106.84, 106.68, 104.91, 106.05, 102.63, 
                                                                          104.43, 103.92, 103.85, 103.71, 99.32, 103.02, 100.79, 101.5, 
                                                                          106.55, 108.73, 109.68, 112.88, 116.66, 116.59, 119.13, 122.81, 
                                                                          124.18, 127.61, 127.22, 129.81, 130.39, 131.11, 130.48, 126.35, 
                                                                          125.6, 118.47, 118.32, 116.57, 111.24, 109.76, 108.27, 105.05, 
                                                                          104.8, 103.92, 106.12, 108.32, 105.49, 104.84, 105.95, 103.16, 
                                                                          99.33, 98.52, 94.97, 92.6499999999999, 94.0800000000001, 92.09, 
                                                                          89.09)), row.names = c(NA, -399L), class = c("tbl_df", "tbl", 
                                                                                                                       "data.frame")) %>% 
  mutate(t = make_datetime(min = t)) %>% 
  as_tbl_time(index = t) %>% 
  time_decompose(value, merge = TRUE)
#> frequency = 60 minutes
#> trend = 399 minutes
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> # A time tibble: 399 × 6
#> # Index:         t
#>    t                   value observed season trend remainder
#>    <dttm>              <dbl>    <dbl>  <dbl> <dbl>     <dbl>
#>  1 1970-01-01 16:41:00  119.     119. -0.485  104.     15.5 
#>  2 1970-01-01 16:42:00  122.     122. -0.462  104.     18.4 
#>  3 1970-01-01 16:43:00  121.     121.  0.274  104.     17.2 
#>  4 1970-01-01 16:44:00  118.     118.  0.284  104.     14.6 
#>  5 1970-01-01 16:45:00  118.     118.  0.348  104.     14.1 
#>  6 1970-01-01 16:46:00  115.     115. -0.421  104.     12.0 
#>  7 1970-01-01 16:47:00  116.     116.  0.582  104.     11.6 
#>  8 1970-01-01 16:48:00  112.     112.  1.26   104.      6.90
#>  9 1970-01-01 16:49:00  117.     117.  2.72   104.     10.3 
#> 10 1970-01-01 16:50:00  120.     120.  4.42   104.     12.2 
#> # … with 389 more rows


创建于2023-02-14带有reprex v2.0.2

p8ekf7hl

p8ekf7hl1#

我已经开发了一个解决方案来解决这个错误。在几分钟内生成一个带有索引的tibbletime对象。然后进行分解,然后返回到tibble对象,并在几秒钟内重新生成具有索引的tibbletime对象。唯一的困难是周期和趋势是以分钟而不是秒来指定的。Reprex下方。

library(tidyverse)
library(lubridate)
library(tibbletime)
#> (...)
library(anomalize)

structure(list(t = c(1001, 1002, 1003, 1004, 1005, 1006, 1007, 
                     1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 
                     1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 
                     1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 
                     1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 
                     1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 
                     1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 
                     1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 
                     1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 
                     1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 
                     1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 
                     1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 
                     1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 
                     1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 
                     1151, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 
                     1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, 1172, 
                     1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1182, 1183, 
                     1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 
                     1195, 1196, 1197, 1198, 1199, 1200, 1201, 1202, 1203, 1204, 1205, 
                     1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 
                     1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 
                     1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 
                     1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 
                     1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 
                     1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 
                     1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279, 1280, 1281, 1282, 
                     1283, 1284, 1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 
                     1294, 1295, 1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 
                     1305, 1306, 1307, 1308, 1309, 1310, 1311, 1312, 1313, 1314, 1315, 
                     1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 
                     1327, 1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 
                     1338, 1339, 1340, 1341, 1342, 1343, 1344, 1345, 1346, 1347, 1348, 
                     1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 
                     1360, 1361, 1362, 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1370, 
                     1371, 1372, 1373, 1374, 1375, 1376, 1377, 1378, 1379, 1380, 1381, 
                     1382, 1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391, 1392, 
                     1393, 1394, 1395, 1396, 1397, 1398, 1399), value = c(118.62, 
                                                                          121.57, 121.08, 118.5, 118.09, 115.28, 115.8, 111.83, 116.73, 
                                                                          120.34, 120.11, 124.51, 125.28, 127.2, 128.45, 124.24, 122.7, 
                                                                          121, 116.26, 114.12, 111.08, 109.83, 107.71, 109.31, 108.4, 106.59, 
                                                                          103.34, 104.02, 106.15, 105.9, 105.96, 104.79, 104.04, 103.45, 
                                                                          102.07, 99.71, 97.9, 99.12, 100.45, 99, 97.3, 96.11, 95.09, 95.98, 
                                                                          95.3, 92.88, 93.1, 91.2099999999999, 85.21, 85.6, 82.9500000000001, 
                                                                          80.85, 77.41, 78.66, 77.93, 73.88, 72.68, 71.09, 67.04, 68.25, 
                                                                          70.23, 67.86, 67.94, 69.44, 68.5, 67.11, 65.8899999999999, 64.7299999999999, 
                                                                          66.4900000000001, 67.2099999999999, 68.5400000000001, 69.56, 
                                                                          66.68, 67.24, 68.79, 69.74, 72.43, 73.17, 75.39, 79.2, 80.72, 
                                                                          83.04, 84.73, 87.82, 88.7, 92.38, 95.55, 97.0499999999999, 97.32, 
                                                                          97.59, 97.97, 97.96, 100.63, 100.77, 104.89, 105.38, 109.1, 107.84, 
                                                                          107.78, 105.08, 106.36, 103.95, 107.74, 107.58, 109.69, 112.46, 
                                                                          115.77, 117.11, 121.28, 123.4, 127.66, 127.01, 130.15, 131.31, 
                                                                          130.12, 129.88, 129.22, 128.48, 126.17, 127.26, 128.93, 127.57, 
                                                                          127.93, 128.92, 128.53, 128.72, 129.58, 129.12, 126.49, 127.31, 
                                                                          125.46, 125.09, 127.55, 129.01, 128.58, 128.7, 128.04, 127.13, 
                                                                          125.86, 124.27, 124.5, 125.45, 124.67, 126.13, 127.71, 126.17, 
                                                                          127.14, 125.11, 122.11, 120.4, 118.86, 117.7, 113.78, 109.02, 
                                                                          109.34, 108.79, 105.44, 108.11, 105.5, 106.75, 104.14, 100.34, 
                                                                          99.8099999999999, 95.9300000000001, 97.44, 97.63, 96.53, 97.25, 
                                                                          94.95, 93.06, 92.09, 86.22, 83.3400000000001, 77.04, 77.37, 74.88, 
                                                                          77.41, 78.21, 78.77, 81.79, 80.51, 84.71, 88.06, 88.25, 86.5300000000001, 
                                                                          86.6099999999999, 86.57, 89.06, 89.3499999999999, 90.92, 89.9100000000001, 
                                                                          90.26, 88.7699999999999, 88.08, 86.2699999999999, 84.93, 83.81, 
                                                                          83.18, 83.39, 84.43, 87.67, 87.23, 90.16, 89.88, 94.14, 97.64, 
                                                                          99.49, 100.61, 102.85, 103.01, 104.28, 106.94, 107.88, 107.5, 
                                                                          111.68, 110.92, 113.76, 113.8, 114.3, 115.95, 116.02, 113.34, 
                                                                          114.1, 118.21, 120.39, 122.09, 123.47, 122.46, 120.2, 117.88, 
                                                                          120.75, 119.2, 119.76, 120.43, 121.09, 120.36, 121.01, 120.87, 
                                                                          119.03, 120.17, 118.65, 118.13, 118.57, 118.84, 120.07, 119.93, 
                                                                          122.45, 125.04, 126.96, 124.86, 127.39, 129.6, 129.93, 127.71, 
                                                                          124.05, 120.7, 118.72, 116.6, 114.48, 110.81, 105.67, 97.48, 
                                                                          93.07, 95.25, 91.2100000000001, 89.47, 83.41, 84.85, 83.2500000000001, 
                                                                          80.89, 85.66, 86.3000000000001, 87.1499999999999, 87.1800000000001, 
                                                                          89.01, 91.27, 96.7400000000001, 100.41, 103.36, 108.56, 110.08, 
                                                                          109.31, 111, 109.77, 112.32, 114.05, 112.37, 114.59, 114.84, 
                                                                          114.31, 115.14, 115.38, 116.1, 112.19, 108.39, 106.65, 103.69, 
                                                                          100.23, 96.56, 92.07, 88.1000000000001, 80.79, 80.2800000000001, 
                                                                          76.8999999999999, 72.3900000000001, 70.37, 66.37, 66.8700000000001, 
                                                                          64.9199999999999, 63.23, 61.96, 53.95, 58.94, 59.9199999999999, 
                                                                          61, 60.3500000000001, 60.01, 57.33, 58.25, 59.37, 61.28, 62.61, 
                                                                          62.16, 63.38, 65.02, 68.9599999999999, 70.56, 71.54, 67.54, 69.34, 
                                                                          71.69, 73.87, 74.94, 78.94, 82.14, 84.88, 86.9299999999999, 90.26, 
                                                                          92.2500000000001, 95.15, 96.98, 99.2799999999999, 100.41, 105.08, 
                                                                          106.06, 106.95, 107.14, 106.84, 106.68, 104.91, 106.05, 102.63, 
                                                                          104.43, 103.92, 103.85, 103.71, 99.32, 103.02, 100.79, 101.5, 
                                                                          106.55, 108.73, 109.68, 112.88, 116.66, 116.59, 119.13, 122.81, 
                                                                          124.18, 127.61, 127.22, 129.81, 130.39, 131.11, 130.48, 126.35, 
                                                                          125.6, 118.47, 118.32, 116.57, 111.24, 109.76, 108.27, 105.05, 
                                                                          104.8, 103.92, 106.12, 108.32, 105.49, 104.84, 105.95, 103.16, 
                                                                          99.33, 98.52, 94.97, 92.6499999999999, 94.0800000000001, 92.09, 
                                                                          89.09)), row.names = c(NA, -399L), class = c("tbl_df", "tbl", 
                                                                                                                       "data.frame")) %>% 
  mutate(t_sec = make_datetime(sec = t),
         t_min = make_datetime(min = t)) %>% # Dummy varialbe. Fictitious time expressed in minutes.
  as_tbl_time(index = t_min) %>% # Create a tibbletime with an index in minutes.
  time_decompose(value, merge = TRUE) %>% # Decomposition based on minutes. The period and trend are specified in minutes, although it is actually in seconds.
  as_tibble() %>% 
  as_tbl_time(index = t_sec) # Return to timetibble with index in seconds.
#> frequency = 60 minutes # In fact, 60 seconds
#> trend = 399 minutes # In fact, 399 seconds
#> # A time tibble: 399 × 8
#> # Index:         t_sec
#>        t value t_sec               t_min               observed season trend
#>    <dbl> <dbl> <dttm>              <dttm>                 <dbl>  <dbl> <dbl>
#>  1  1001  119. 1970-01-01 00:16:41 1970-01-01 16:41:00     119. -0.485  104.
#>  2  1002  122. 1970-01-01 00:16:42 1970-01-01 16:42:00     122. -0.462  104.
#>  3  1003  121. 1970-01-01 00:16:43 1970-01-01 16:43:00     121.  0.274  104.
#>  4  1004  118. 1970-01-01 00:16:44 1970-01-01 16:44:00     118.  0.284  104.
#>  5  1005  118. 1970-01-01 00:16:45 1970-01-01 16:45:00     118.  0.348  104.
#>  6  1006  115. 1970-01-01 00:16:46 1970-01-01 16:46:00     115. -0.421  104.
#>  7  1007  116. 1970-01-01 00:16:47 1970-01-01 16:47:00     116.  0.582  104.
#>  8  1008  112. 1970-01-01 00:16:48 1970-01-01 16:48:00     112.  1.26   104.
#>  9  1009  117. 1970-01-01 00:16:49 1970-01-01 16:49:00     117.  2.72   104.
#> 10  1010  120. 1970-01-01 00:16:50 1970-01-01 16:50:00     120.  4.42   104.
#> # ℹ 389 more rows
#> # ℹ 1 more variable: remainder <dbl>

字符串
创建于2023-07-17带有reprex v2.0.2

相关问题