如何在R中消除时间序列中的多个异常值?

如何在R中消除时间序列中的多个异常值?,r,R,我使用“异常值”包来删除一些不需要的值。但是似乎rm.outliers()函数并不能同时替换所有的异常值。rm.outliers()可能无法递归执行拒绝。然后,基本上我必须多次调用这个函数来替换所有的异常值。 以下是我遇到的问题的一个可重现的例子: require(outliers) # creating a timeseries: set.seed(12345) y = rnorm(10000) # inserting some outliers: y[4000:

我使用“异常值”包来删除一些不需要的值。但是似乎rm.outliers()函数并不能同时替换所有的异常值。rm.outliers()可能无法递归执行拒绝。然后,基本上我必须多次调用这个函数来替换所有的异常值。 以下是我遇到的问题的一个可重现的例子:

require(outliers)
   # creating a timeseries:
   set.seed(12345)
   y = rnorm(10000)
   # inserting some outliers:
   y[4000:4500] = -11
   y[4501:5000] = -10
   y[5001:5100] = -9
   y[5101:5200] = -8
   y[5201:5300] = -7
   y[5301:5400] = -6
   y[5401:5500] = -5
# plotting the timeseries + outliers:
plot(y, type="l", col="black", lwd=6, xlab="Time", ylab="w'")
# trying to get rid of some outliers by replacing them by the series mean value:
new.y = outliers::rm.outlier(y, fill=TRUE, median=FALSE)
new.y = outliers::rm.outlier(new.y, fill=TRUE, median=FALSE)
# plotting the new timeseries "after removing the outliers":
lines(new.y, col="red")
# inserting a legend:
legend("bottomleft", c("raw", "new series"), col=c("black","red"), lty=c(1,1), horiz=FALSE, bty="n")

有人知道如何改进上面的代码,以便用平均值替换所有的异常值吗?

我能想到的最好办法就是使用
for
循环,在找到异常值时跟踪它们

plot(y, type="l", col="black", lwd=6, xlab="Time", ylab="w'")

maxIter <- 100
outlierQ <- rep(F, length(y))

for (i in 1:maxIter) {
  bad <- outlier(y, logical = T)
  if (!any(bad)) break
  outlierQ[bad] <- T
  y[bad] <- mean(y[!bad])
}

y[outlierQ] <- mean(y[!outlierQ])

lines(y, col="blue")
绘图(y,type=“l”,col=“black”,lwd=6,xlab=“Time”,ylab=“w”)
马克西特