# How does forecast::tsclean() detect outliers in R?

Does it use a particular z-score? I know that it does apply STL.

My data is seasonal, and had quite a few outliers, so I am just wondering how exactly it determined whether a particular data point is an outlier.

From the source code for tsoutlier which is called by tsclean:

1. They fit a smoother for seasonality and get out the residuals.
2. They get the 25th and 75th quantiles of the residuals.
3. They calculate the IQR as the difference between these quantiles.
4. They calculate the limits for what is/is not a outlier.
• Lower limit is the 25th quantile of residuals - 3 * the IQR.
• Upper limit is the 75th quantile of residuals + 3 * the IQR.
5. Any observation with a residual outside these ranges is an outlier.
 # Use super-smoother on the (seasonally adjusted) data
tt <- 1:n
mod <- supsmu(tt, xx)
resid <- xx - mod\$y

# Make sure missing values are not interpeted as outliers
if (nmiss > 0L) {
resid[missng] <- NA
}

# Limits of acceptable residuals
resid.q <- quantile(resid, probs = c(0.25, 0.75), na.rm = TRUE)
iqr <- diff(resid.q)
limits <- resid.q + 3 * iqr * c(-1, 1)

# Find residuals outside limits
if ((limits[2] - limits[1]) > 1e-14) {
outliers <- which((resid < limits[1]) | (resid > limits[2]))
} else {
outliers <- numeric(0)
}


Their code is super clearly written.