How to get rid of multiple outliers in a timeseries in R? -
i'm using "outliers" package in order remove undesirable values. seems rm.outliers() funcion not replace outliers @ same time. probably, rm.outliers() not perform despikes recursively. then, have call function lot of times in order replace outliers. here reproducible example of issue i'm experiencing:
require(outliers) # creating timeseries: set.seed(12345) y = rnorm(10000) # inserting 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 timeseries + outliers: plot(y, type="l", col="black", lwd=6, xlab="time", ylab="w'") # trying rid of outliers replacing them 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 new timeseries "after removing outliers": lines(new.y, col="red") # inserting legend: legend("bottomleft", c("raw", "new series"), col=c("black","red"), lty=c(1,1), horiz=false, bty="n")
does know how improve code above, outliers replaced mean value?
best thought come use for
loop, keeping track of outliers find them.
plot(y, type="l", col="black", lwd=6, xlab="time", ylab="w'") maxiter <- 100 outlierq <- rep(f, length(y)) (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")
Comments
Post a Comment