识别R中的时间序列数据中的异常值

时间:2016-02-02 02:58:37

标签: r time-series outliers

我有一个带有相应变量的时间序列数据,在某个范围内增加或减少前一个值,比如+ - 10%。时间序列中的数据点与时间序列中的前一个或后一个值不一致。

例如:

time       v1
13:01:30   0.689
13:01:31   0.697
13:01:32   0.701
13:01:33   0.713
**13:01:34   0.235**
13:01:35   0.799
13:01:36   0.813
13:01:37   0.822 
**13:01:38   0**
13:01:39   0.865
13:01:40   0.869

是否有任何库可能有助于识别R中的这些异常值[数据中的0.235和0]?

更新 - dput的输出:

structure(list(time = c("13:01:30", "13:01:31", "13:01:32", "13:01:33", 
"13:01:34", "13:01:35", "13:01:36", "13:01:37", "13:01:38", "13:01:39", 
"13:01:40"), v1 = c(0.689, 0.697, 0.701, 0.713, 0.235, 0.799, 
0.813, 0.822, 0, 0.865, 0.869)), .Names = c("time", "v1"), row.names = c(NA, 
11L), class = c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助(作为模板)

# load packages
library(ggplot2)   # 2.0.0
library(ggrepel)   # 0.4
library(dplyr)     # 0.4.3

# make data_frame of OP data
ts_tdf <- data_frame(
    time = paste("13", "01", 30:40, sep = ":"),
    v1 = c(0.689, 0.697, 0.701, 0.713, 0.235, 0.799, 0.813, 0.822, 0.00, 0.865, 0.869)   
)

# calculate measure of central tendency (I like median)
v1_median <- median(ts_tdf$v1)

# create absolute deviation column, identify (n = 10) largest outliers, plot (sorted) values of new column 
ts_tdf %>%
    mutate(abs_med = abs(v1 - v1_median)) %>%
    arrange(-abs_med) %>%
    head(n = 10) %>%
    mutate(char_time = as.character(time)) %>%
    ggplot(data = ., aes(x = 1:nrow(.), y = abs_med, label = char_time)) +
    geom_point() + 
    geom_text_repel()