R:将forecast :: tsclean()的输出与datetime的原始数据相关联

时间:2018-03-22 22:27:40

标签: r dataframe time-series outliers

我正尝试使用tsclean()包中的forecast函数从时间序列数据框中删除已建立的异常值。

为了使用tsclean(),我已将原始数据转换为ts()对象。不幸的是,这会删除我的时间戳向量,这会在以后产生一些问题。将ts()对象送入tsclean()函数后,将返回正确清理的时间序列。但是,由于我的POSIXct对象中不再存在tsclean()时间戳,因此无法将此结果返回到原始数据框。我最终尝试在已清理的响应变量lm()上针对原始数据框中的解释变量df$Y执行df$X

library(forecast)
datetime <- as.POSIXct(c("2018-03-05 15:54:00", "2018-03-05 15:55:00", "2018-03-05 15:56:00", "2018-03-05 15:57:00", "2018-03-05 15:58:00"))
Y <- c(1, 5, 9, 100, 2)
X <- c(3, 4, 2, 4, 5)
df <- data.frame(datetime, Y, X)
time_series <- ts(df$Y)
time_series_clean <- tsclean(time_series)

原始数据框:

             datetime   Y X
1 2018-03-05 15:54:00   1 3
2 2018-03-05 15:55:00   5 4
3 2018-03-05 15:56:00   9 2
4 2018-03-05 15:57:00 100 4
5 2018-03-05 15:58:00   2 5

期望的结果:

             datetime   Y X
1 2018-03-05 15:54:00   1 3
2 2018-03-05 15:55:00   5 4
3 2018-03-05 15:56:00   9 2
4 2018-03-05 15:58:00   2 5

tsclean()之前:

enter image description here

tsclean()后:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,您希望从原始数据中删除异常值。解决方案是使用ImageService.Instance.LoadCompiledResource("yourFile.svg").WithCustomDataResolver(new SvgDataResolver(200,200)).AsPNGStreamAsync(); 函数:

tsoutliers

此功能也很有趣,如果您需要建议的值来替换异常值,则会给time_series_outliers <- tsoutliers(time_series) # identify outliers df <- df[-time_series_outliers$index, ] # remove them from the original data