'How to do Exponential smoothing using FPP3 in daily data that has holidays
I have the data on closing stock prices. This data will have gaps on weekends and also holidays when the market is closed. How do I run single, double and triple exponential smoothing using fpp3 on this? So, I can do,
fit<- db %>%
model(ETS(Close ~ error("A") + trend("N")+ season ("N")))
But it gives me an error that my data has implicit gaps and suggests that I should use fill_gaps(). But the gaps are not missing values, they are there for a reason. How do I allow the gaps and use this package?
As suggested I am trying to create a reproducible example
st1<- as.Date("2022-01-03")
st2<- as.Date("2022-01-10")
et1<- as.Date("2022-01-07")
et2<- as.Date("2022-01-14")
date1 <- seq(st1,et1,by="day")
date2 <- seq(st2,et2,by="day")
date <- c(date1,date2)
value <- c(100,110,115,90,80,120,100,90,115,85)
db <- tibble(date,value)
db <- db %>%
mutate(date = as_date(date)) %>%
as_tsibble(index = date)
We do not have data for 8th and 9th of January since it is the weekend. This is an explicit gap and the formula must completely ignore this gap. So when calculating the fitted value for 10th January, it must use the actual and fitted values of 7th January. How do we make this happen?
Imputing values for 8th and 9th January might not be a correct approach since we are creating data for something that does not exist. For instance, if have data for 8th and 9th January, that data will be used to calculate SSE and other variables. We do not want that to happen.
Solution 1:[1]
It helps to have a reproducible example, so I made a simple one using a data set in the FPP3 package. This code takes one of the data sets and simply removes the second row, thus creating missing data, similar to weekends and holidays in other data sets:
missing_data <- canadian_gas %>%
slice(-2)
Let's look at the data with the missing row, clearly February 1960 is missing:
If we try to run ETS, we get an error message:
missing_data %>%
model(ETS(Volume))
Warning message:
1 error encountered for ETS(Volume)
1 .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using tsibble::fill_gaps() if required.
There is a function called fill_gaps that will insert the missing rows, and add NAs:
missing_data <- fill_gaps(missing_data)
If we look at missing_data, we can clearly see the NA
The code still will not run, due to the NA. This can be verified in R:
missing_data %>%
model(ETS(Volume))
This returns an error message:
Warning message: 1 error encountered for ETS(Volume) 1 ETS does not support missing values.
Simply fill in the missing values in R (and I used a value of 1.43, I strongly suggest using better methods to fill in missing data in your own data set, this is just for example):
missing_data[2,2] = 1.43
Let's look at the data, to confirm 1.43 is the value for February 1960:
Now the ETS model will run because there are no missing rows and no NAs, and return a result:
missing_data %>%
model(ETS(Volume)) %>%
report()
Let's look at the report, to verify it ran:
Simply do the same process for weekends and holidays in your original data set, and then your data set should run and return a valid result.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |




