how to get tsclean working on data frame with multiple time series

Viewed 1806

I'm in the process of creating a forecast based on the hts package but before getting this far I need to clean the data for outliers and missing values.

For this I thought of using the tsclean function in the forecast package. I got my data stored in data frame with multiple columns (time series) that I wish to get cleaned. I can get the function to work when only having one time serie, but since I do have quite a lot i'm looking for a smart way to do this.

When running the code:

SFA5 <- ts(SFA4, frequency=12, start=c(2012,1), end=c(2017,10)) 
ggt <- tsclean(SFA5[1:70, 1:94], replace.missing = TRUE)

I get this error message:

Error in na.interp(x, lambda = lambda) : The time series is not univariate.

The data is here:

https://www.dropbox.com/s/dow2jpuv5unmtgd/Data1850.xlsx?dl=0

My question is: what am i doing wrong or is the only solution to do a loop sequence

1 Answers

The error message suggests that the function takes univariate time series as its first argument only. So you need to apply tsclean to each column, as you might have guessed.

library(forecast)
ggt <- sapply(X = SFA5[1:70, 1:94], FUN = tsclean)
Related