Over a million developers have joined DZone.

How to "Backcast" a Time Series in R

DZone's Guide to

How to "Backcast" a Time Series in R

· Big Data Zone ·
Free Resource

The Architect’s Guide to Big Data Application Performance. Get the Guide.

Some­times it is use­ful to “back­cast” a time series — that is, fore­cast in reverse time. Although there are no in-​​built R func­tions to do this, it is very easy to imple­ment. Sup­pose x is our time series and we want to back­cast for h peri­ods. Here is some code that should work for most uni­vari­ate time series. The exam­ple is non-​​seasonal, but the code will also work with sea­sonal data.

x <- WWWusage
h <- 20
f <- frequency(x)
# Reverse time
revx <- ts(rev(x), frequency=f)
# Forecast
fc <- forecast(auto.arima(revx), h)
# Reverse time again
fc$mean <- ts(rev(fc$mean),end=tsp(x)[1] - 1/f, frequency=f)
fc$upper <- fc$upper[h:1,]
fc$lower <- fc$lower[h:1,]
fc$x <- x
# Plot result
plot(fc, xlim=c(tsp(x)[1]-h/f, tsp(x)[2]))

Learn how taking a DataOps approach will help you speed up processes and increase data quality by providing streamlined analytics pipelines via automation and testing. Learn More.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}