Search code examples
rforecastingholtwinters

Revert HoltWinters forecasted stationary data in R


I've converted a non-stationary time series into a stationary data set by using diff(log(ts.dat)), I've then forecasted using the HoltWinters model based on that data set. I now need to revert the forecast back to its non-stationary format so i can read the forecasted data in context.

below is my code

library(lubridate)
tsdat <- ts(dat, frequency = 364/7, start =decimal_date(ymd("2018-01-01")))
tsdat
plot.ts(tsdat)

#convert to stationary 
diffdat <- diff(log(tsdat))
plot.ts(diffdat)

#decompose data
dedat <- decompose(diffdat)
plot(dedat)

#forecast data along historical data
tsdatdiff <- diff(log(tsdat))
plot.ts(tsdatdiff)
View(tsdatdiff)

fcdat <- HoltWinters(tsdatdiff)
fcdat
fcdat$SSE
plot(fcdat)

#forecast future data
fcdat2 <- forecast:::forecast.HoltWinters(fcdat, h=58)
plot(fcdat2)
View(fcdat2)

I am new to forecasting models and r so any help is much appreciated.

Edit: tsdat =

[1] 239356 233505 221379 216805 181376 185011 204697 171042 178016 161664
[11] 169265 165494 143858 182354 177574 168444 157602 143029 147777 128124
[21] 123876 122930 122716 128213 123046 129200 154485 162267 146355 142686
[31] 139640 139767 143704 136917 145943 136225 149343 151274 165070 180999
[41] 159661 155912 169565 171383 172614 184754 184409 200315 198860 211969
[51] 252303 227694 229727 209950 222494 217205 192252 182349 188436 181309
[61] 172210 170766 171861 172913 154271 151161 163106 152785 160463 137944
[71] 141503 128332 125679 120253 118186 128944 118992 124912 139317 158101
[81] 144735 139579 135227 136682 139347 147254 154202 153175 153906 159348
[91] 174015 189201 168106 161848 173245 169786 179268 199458 198268 199099
[101] 210674 223629 248501 223375 227329 206112 202193 207227 209607 
166615
[111] 180929

Solution

  • Answer in comments by Bas!!

    I think what you're looking for is this: fcdat3 <- tsdat[[length(tsdat)]] * exp(diffinv(fcdat2$mean)). In general, you can invert diff(log(...)) with exp(diffinv(...)), but you need to add the last known value as an offset; this is turned into multiplication by the exp operation. Visualise the data together with its forecasts using plot.ts(c(tsdat, fcdat3))