I would like to clean multiple time series of outliers in R
.
(我想清除R
多个时间序列离群值。)
forecast::tsclean()
function works well for univariate time series data (a single time series of observations) but tsclean()
does not work for multivariate timeseries (many different timeseries of observations over a given period). (forecast::tsclean()
函数适用于单变量时间序列数据(单个时间序列的观测值),但tsclean()
不适用于多元时间序列(给定时间段内许多不同的观测时间序列)。)
tidyverse
as I am using tsibbles
. (我更喜欢在tidyverse
按照我使用的tsibbles
。)
I am aware of the feasts
package which makes working with multivariate time series easier, but I do not believe it has adapted the tsclean
function.
(我知道feasts
包使使用多元时间序列更容易,但是我不相信它已经适应了tsclean
函数。)
I provide an example where I attempt two solutions, which both fail, which highlights my problem:
(我提供了一个示例,其中我尝试了两个都失败的解决方案,这突出了我的问题:)
-
Attempt 1
naively applies thetsclean
function to thetsibble
which fails.(
Attempt 1
天真地将tsclean
函数应用于失败的tsibble
。) -
Attempt 2
tries to exploit thattsclean
works on univariate series, by applying thegroup_by_key()
function in order to deal with each of the many time series separately.(
Attempt 2
尝试通过应用group_by_key()
函数来利用tsclean
在单变量序列上工作,以便分别处理多个时间序列中的每个时间序列。)
library(fable)
#> Loading required package: fabletools
library(tidyverse)
df <- structure(list(date = structure(c(
11413, 11504, 11596, 11688,
11778, 11869, 11961, 12053, 12143, 12234, 12326, 12418, 12509,
12600, 12692, 12784, 12874, 12965, 13057, 13149, 13239, 13330,
13422, 13514, 13604, 13695, 13787, 13879, 13970, 14061, 14153,
14245, 14335, 14426, 14518, 14610, 14700, 14791, 14883, 14975,
15065, 15156, 15248, 15340, 15431, 15522, 15614, 15706, 15796,
15887, 15979, 16071, 16161, 16252, 16344, 16436, 16526, 16617,
16709, 16801, 16892, 16983, 17075, 17167, 17257, 17348, 17440,
17532, 17622, 17713, 17805, 11413, 11504, 11596, 11688, 11778,
11869, 11961, 12053, 12143, 12234, 12326, 12418, 12509, 12600,
12692, 12784, 12874, 12965, 13057, 13149, 13239, 13330, 13422,
13514, 13604, 13695, 13787, 13879, 13970, 14061, 14153, 14245,
14335, 14426, 14518, 14610, 14700, 14791, 14883, 14975, 15065,
15156, 15248, 15340, 15431, 15522, 15614, 15706, 15796, 15887,
15979, 16071, 16161, 16252, 16344, 16436, 16526, 16617, 16709,
16801, 16892, 16983, 17075, 17167, 17257, 17348, 17440, 17532,
17622, 17713, 17805, 11413, 11504, 11596, 11688, 11778, 11869,
11961, 12053, 12143, 12234, 12326, 12418, 12509, 12600, 12692,
12784, 12874, 12965, 13057, 13149, 13239, 13330, 13422, 13514,
13604, 13695, 13787, 13879, 13970, 14061, 14153, 14245, 14335,
14426, 14518, 14610, 14700, 14791, 14883, 14975, 15065, 15156,
15248, 15340, 15431, 15522, 15614, 15706, 15796, 15887, 15979,
16071, 16161, 16252, 16344, 16436, 16526, 16617, 16709, 16801,
16892, 16983, 17075, 17167, 17257, 17348, 17440, 17532, 17622,
17713, 17805, 11413, 11504, 11596, 11688, 11778, 11869, 11961,
12053, 12143, 12234, 12326, 12418, 12509, 12600, 12692, 12784,
12874, 12965, 13057, 13149, 13239, 13330, 13422, 13514, 13604,
13695, 13787, 13879, 13970, 14061, 14153, 14245, 14335, 14426,
14518, 14610, 14700, 14791, 14883, 14975, 15065, 15156, 15248,
15340, 15431, 15522, 15614, 15706, 15796, 15887, 15979, 16071,
16161, 16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892,
16983, 17075, 17167, 17257, 17348, 17440, 17532, 17622, 17713,
17805, 11413, 11504, 11596, 11688, 11778, 11869, 11961, 12053,
12143, 12234, 12326, 12418, 12509, 12600, 12692, 12784, 12874,
12965, 13057, 13149, 13239, 13330, 13422, 13514, 13604, 13695,
13787, 13879, 13970, 14061, 14153, 14245, 14335, 14426, 14518,
14610, 14700, 14791, 14883, 14975, 15065, 15156, 15248, 15340,
15431, 15522, 15614, 15706, 15796, 15887, 15979, 16071, 16161,
16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892, 16983,
17075, 17167, 17257, 17348, 17440, 17532, 17622, 17713, 17805,
11413, 11504, 11596, 11688, 11778, 11869, 11961, 12053, 12143,
12234, 12326, 12418, 12509, 12600, 12692, 12784, 12874, 12965,
13057, 13149, 13239, 13330, 13422, 13514, 13604, 13695, 13787,
13879, 13970, 14061, 14153, 14245, 14335, 14426, 14518, 14610,
14700, 14791, 14883, 14975, 15065, 15156, 15248, 15340, 15431,
15522, 15614, 15706, 15796, 15887, 15979, 16071, 16161, 16252,
16344, 16436, 16526, 16617, 16709, 16801, 16892, 16983, 17075,
17167, 17257, 17348, 17440, 17532, 17622, 17713, 17805, 11413,
11504, 11596, 11688, 11778, 11869, 11961, 12053, 12143, 12234,
12326, 12418, 12509, 12600, 12692, 12784, 12874, 12965, 13057,
13149, 13239, 13330, 13422, 13514, 13604, 13695, 13787, 13879,
13970, 14061, 14153, 14245, 14335, 14426, 14518, 14610, 14700,
14791, 14883, 14975, 15065, 15156, 15248, 15340, 15431, 15522,
15614, 15706, 15796, 15887, 15979, 16071, 16161, 16252, 16344,
16436, 16526, 16617, 16709, 16801, 16892, 16983, 17075, 17167,
17257, 17348, 17440, 17532, 17622, 17713, 17805, 11413, 11504,
11596, 11688, 11778, 11869, 11961, 12053, 12143, 12234, 12326,
12418, 12509, 12600, 12692, 12784, 12874, 12965, 13057, 13149,
13239, 13330, 13422, 13514, 13604, 13695, 13787, 13879, 13970,
14061, 14153, 14245, 14335, 14426, 14518, 14610, 14700, 14791,
14883, 14975, 15065, 15156, 15248, 15340, 15431, 15522, 15614,
15706, 15796, 15887, 15979, 16071, 16161, 16252, 16344, 16436,
16526, 16617, 16709, 16801, 16892, 16983, 17075, 17167, 17257,
17348, 17440, 17532, 17622, 17713, 17805, 11413, 11504, 11596,
11688, 11778, 11869, 11961, 12053, 12143, 12234, 12326, 12418,
12509, 12600, 12692, 12784, 12874, 12965, 13057, 13149, 13239,
13330, 13422, 13514, 13604, 13695, 13787, 13879, 13970, 14061,
14153, 14245, 14335, 14426, 14518, 14610, 14700, 14791, 14883,
14975, 15065, 15156, 15248, 15340, 15431, 15522, 15614, 15706,
15796, 15887, 15979, 16071, 16161, 16252, 16344, 16436, 16526,
16617, 16709, 16801, 16892, 16983, 17075, 17167, 17257, 17348,
17440, 17532, 17622, 17713, 17805, 11413, 11504, 11596, 11688,
11778, 11869, 11961, 12053, 12143, 12234, 12326, 12418, 12509,
12600, 12692, 12784, 12874, 12965, 13057, 13149, 13239, 13330,
13422, 13514, 13604, 13695, 13787, 13879, 13970, 14061, 14153,
14245, 14335, 14426, 14518, 14610, 14700, 14791, 14883, 14975,
15065, 15156, 15248, 15340, 15431, 15522, 15614, 15706, 15796,
15887, 15979, 16071, 16161, 16252, 16344, 16436, 16526, 16617,
16709, 16801, 16892, 16983, 17075, 17167, 17257, 17348, 17440,
17532, 17622, 17713, 17805, 11413, 11504, 11596, 11688, 11778,
11869, 11961, 12053, 12143, 12234, 12326, 12418, 12509, 12600,
12692, 12784, 12874, 12965, 13057, 13149, 13239, 13330, 13422,
13514, 13604, 13695, 13787, 13879, 13970, 14061, 14153, 14245,
14335, 14426, 14518, 14610, 14700, 14791, 14883, 14975, 15065,
15156, 15248, 15340, 15431, 15522, 15614, 15706, 15796, 15887,
15979, 16071, 16161, 16252, 16344, 16436, 16526, 16617, 16709,
16801, 16892, 16983, 17075, 17167, 17257, 17348, 17440, 17532,
17622, 17713, 17805, 11413, 11504, 11596, 11688, 11778, 11869,
11961, 12053, 12143, 12234, 12326, 12418, 12509, 12600, 12692,
12784, 12874, 12965, 13057, 13149, 13239, 13330, 13422, 13514,
13604, 13695, 13787, 13879, 13970, 14061, 14153, 14245, 14335,
14426, 14518, 14610, 14700, 14791, 14883, 14975, 15065, 15156,
15248, 15340, 15431, 15522, 15614, 15706, 15796, 15887, 15979,
16071, 16161, 16252, 16344, 16436, 16526, 16617, 16709, 16801,
16892, 16983, 17075, 17167, 17257, 17348, 17440, 17532, 17622,
17713, 17805, 11413, 11504, 11596, 11688, 11778, 11869, 11961,
12053, 12143, 12234, 12326, 12418, 12509, 12600, 12692, 12784,
12874, 12965, 13057, 13149, 13239, 13330, 13422, 13514, 13604,
13695, 13787, 13879, 13970, 14061, 14153, 14245, 14335, 14426,
14518, 14610, 14700, 14791, 14883, 14975, 15065, 15156, 15248,
15340, 15431, 15522, 15614, 15706, 15796, 15887, 15979, 16071,
16161, 16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892,
16983, 17075, 17167, 17257, 17348, 17440, 17532, 17622, 17713,
17805
), class = c("yearquarter", "Date")), Series = c(
"10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "101", "101", "101", "101", "101", "101", "101", "101",
"101", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1011", "1011", "1011", "1011", "1011", "1011", "1011", "1011",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1012",
"1012", "1012", "1012", "1012", "1012", "1012", "1012", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "1013", "1013",
"1013", "1013", "1013", "1013", "1013", "1013", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "102", "102", "102", "102", "102", "102",
"102", "102", "102", "10