dput(df)
structure(list(Process = c("PROC050D", "PROC051D", "PROC100D",
"PROC103D", "PROC104D", "PROC106D", "PROC106D", "PROC110D", "PROC111D",
"PROC112D", "PROC113D", "PROC114D", "PROC130D", "PROC131D", "PROC132D",
"PROC154D", "PROC155D", "PROC156D", "PROC157D", "PROC158D", "PROC159D",
"PROC160D", "PROC161D", "PROC162D", "PROC163D", "PROC164D", "PROC165D",
"PROC166D", "PROC170D", "PROC171D", "PROC173D", "PROC174D", "PROC177D",
"PROC180D", "PROC181D", "PROC182D", "PROC185D", "PROC186D", "PROC187D",
"PROC190D", "PROC191D", "PROC192D", "PROC196D", "PROC197D", "PROC201D",
"PROC202D", "PROC203D", "PROC204D", "PROC205D", "PROC206D"),
Date = structure(c(15393, 15393, 15393, 15393, 15393, 15393,
15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393,
15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393,
15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393,
15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393,
15393, 15393, 15393, 15393, 15393, 15393, 15393, 15393), class = "Date"),
Duration = c(30L, 78L, 20L, 15L, 129L, 56L, 156L, 10L, 1656L,
1530L, 52L, 9L, 10L, 38L, 48L, 9L, 26L, 90L, 15L, 23L, 13L,
9L, 34L, 12L, 11L, 16L, 24L, 11L, 236L, 104L, 9L, 139L, 11L,
10L, 22L, 11L, 55L, 35L, 12L, 635L, 44L, 337L, 44L, 9L, 231L,
32L, 19L, 170L, 22L, 19L)), .Names = c("Process", "Date",
"Duration"), row.names = c(NA, 50L), class = "data.frame")
我正在尝试使用IQR方法从我的数据中捕获异常值。但是当我使用这些数据时,我也会捕获可能正常的数据。我喜欢从我的数据点中删除季节性,然后应用异常值规则。
Process列上有数千个不同的进程。我只需要捕获不正常的进程持续时间。任何想法如何从我的数据集中删除季节性?下面的代码计算异常值,但由于季节性因素,异常值可能是正常的。在计算异常值之前,我想从数据框中删除季节性。
library(data.table)
df<-df[, seventyFifth := quantile(Duration, .75), by = Process]
df<-df[, twentyFifth := quantile(Duration, .25), by = Process]
df<-df[, IQR := (seventyFifth-twentyFifth), by = Process]
df$diff<-df$Duration-df$seventyFifth
df<-df[, outlier := diff > 3 * IQR, by = Process]
答案 0 :(得分:6)
为了解决可能的季节性模式,我首先使用acf(df$Duration)
来寻找不同滞后的自相关。如果我没有看到任何东西,我可能不会担心它,除非我有一个先验的理由来建模。你的样本数据没有显示季节性的证据,因为 - 除了自相关总是1--唯一的相关性是滞后1并且是适度的:
一种方法不仅可以处理季节性成分(周期性重复发生的事件),而且可以处理趋势(标准的缓慢变化),stl()
,特别是由Rob J Hyndman在this posting中实施的方法。
Hyndman给出的decomp
函数(下面再现)非常有助于检查季节性,然后将时间序列分解为季节性(如果存在),趋势和残余成分。
decomp <- function(x,transform=TRUE)
{
#decomposes time series into seasonal and trend components
#from http://robjhyndman.com/researchtips/tscharacteristics/
require(forecast)
# Transform series
if(transform & min(x,na.rm=TRUE) >= 0)
{
lambda <- BoxCox.lambda(na.contiguous(x))
x <- BoxCox(x,lambda)
}
else
{
lambda <- NULL
transform <- FALSE
}
# Seasonal data
if(frequency(x)>1)
{
x.stl <- stl(x,s.window="periodic",na.action=na.contiguous)
trend <- x.stl$time.series[,2]
season <- x.stl$time.series[,1]
remainder <- x - trend - season
}
else #Nonseasonal data
{
require(mgcv)
tt <- 1:length(x)
trend <- rep(NA,length(x))
trend[!is.na(x)] <- fitted(gam(x ~ s(tt)))
season <- NULL
remainder <- x - trend
}
return(list(x=x,trend=trend,season=season,remainder=remainder,
transform=transform,lambda=lambda))
}
正如你所看到的那样,如果没有季节性,它会使用stl()
(使用黄土),如果没有季节性则使用回归样条。
在您的情况下,您可以这样使用该功能:
# makemodel
df.decomp <- decomp(df$Duration)
# add results into df
if (!is.null(df.decomp$season)){
df$season <- df.decomp$season} else
{df$season < - 0}
df$trend <- df.decomp$trend
df$Durationsmoothed <- df.decomp$remainder
# if you don't want to detrend
df$Durationsmoothed <- df$Durationsmoothed+df$trend
您应该查阅参考博客文章,因为它会进一步开发此分析。
答案 1 :(得分:2)
这取决于可预测性或smooth
季节性。是否可以制作松散的模型?例如,
LM <- lm(duration~sin(Date)+cos(Date))
或者一些变化。然后,只有在与预测的季节性不同的情况下才能分析数据:
P <- predict(LM)
DIF <- P-df$duration
然后你可以在dif上使用IQR
。说到dif,您可以通过Date
并使用diff
对数据进行排序来获得一些有用的信息。
df <- df[order(df$Date),]
DIF2 <- diff(df$Date)
plot(diff(df$Date))
理论上,DIF2
应该是LM
中产生的函数的导数。
作为旁注,如果有的话,我不建议采取非常系统的方法(即,如果季节性确实很复杂,加载一个包并执行BlindlyGetRidOfOultliersAdjustingForSeasonality(df)
。