Deep networks for time series prediction

If you remember the initial regression problem for time series forecasting, we used the previous kings' ages at the time of their deaths to predict the next king's age at his time of death. We are going to do something similar here.

We are going to use a rolling window approach.

Our first training record will be constructed as follows:

  • The closing price from day 1 in our dataset, up to day 31, will be our X. The day 32 closing price will be our Y.

Now, our second training record will be as follows:

  • The closing price from day 2 in our dataset, up to day 32, will be our X. The closing price on day 33 will be our Y.

We will follow the same pattern. Once again, we will leverage quantmod package's Lag functionality to prepare our data:

> data.ts <- as.ts(data)
> data.zoo <- as.zoo(data.ts)
> x.data <- list()
> for (j in 1:31){
+ var.name <- paste("x.lag.",j)
+ x.data[[var.name]] <- Lag(data.zoo,j)
+ }
> final.data <- na.omit(data.frame(x.data, Y = data.zoo))
> head(final.data)
Lag.1 Lag.2 Lag.3 Lag.4 Lag.5 Lag.6 Lag.7 Lag.8 Lag.9 Lag.10 Lag.11
32 4.250000 4.136161 3.883929 4.053571 4.022321 4.102679 4.073661 3.857143 3.689732 3.529018 3.580357
33 4.075893 4.250000 4.136161 3.883929 4.053571 4.022321 4.102679 4.073661 3.857143 3.689732 3.529018
34 4.102679 4.075893 4.250000 4.136161 3.883929 4.053571 4.022321 4.102679 4.073661 3.857143 3.689732
35 3.973214 4.102679 4.075893 4.250000 4.136161 3.883929 4.053571 4.022321 4.102679 4.073661 3.857143
36 4.064732 3.973214 4.102679 4.075893 4.250000 4.136161 3.883929 4.053571 4.022321 4.102679 4.073661
37 4.151786 4.064732 3.973214 4.102679 4.075893 4.250000 4.136161 3.883929 4.053571 4.022321 4.102679
Lag.12 Lag.13 Lag.14 Lag.15 Lag.16 Lag.17 Lag.18 Lag.19 Lag.20 Lag.21 Lag.22
32 3.705357 3.629464 3.928571 3.935268 4.008929 3.794643 3.975446 4.053571 3.805804 3.712054 3.587054
33 3.580357 3.705357 3.629464 3.928571 3.935268 4.008929 3.794643 3.975446 4.053571 3.805804 3.712054
34 3.529018 3.580357 3.705357 3.629464 3.928571 3.935268 4.008929 3.794643 3.975446 4.053571 3.805804
35 3.689732 3.529018 3.580357 3.705357 3.629464 3.928571 3.935268 4.008929 3.794643 3.975446 4.053571
36 3.857143 3.689732 3.529018 3.580357 3.705357 3.629464 3.928571 3.935268 4.008929 3.794643 3.975446
37 4.073661 3.857143 3.689732 3.529018 3.580357 3.705357 3.629464 3.928571 3.935268 4.008929 3.794643
Lag.23 Lag.24 Lag.25 Lag.26 Lag.27 Lag.28 Lag.29 Lag.30 Lag.31 Y
32 3.455357 3.113839 3.312500 3.491071 3.553571 3.392857 3.714286 3.660714 3.997768 4.075893
33 3.587054 3.455357 3.113839 3.312500 3.491071 3.553571 3.392857 3.714286 3.660714 4.102679
34 3.712054 3.587054 3.455357 3.113839 3.312500 3.491071 3.553571 3.392857 3.714286 3.973214
35 3.805804 3.712054 3.587054 3.455357 3.113839 3.312500 3.491071 3.553571 3.392857 4.064732
36 4.053571 3.805804 3.712054 3.587054 3.455357 3.113839 3.312500 3.491071 3.553571 4.151786
37 3.975446 4.053571 3.805804 3.712054 3.587054 3.455357 3.113839 3.312500 3.491071 4.114397
>

We start by creating an empty x.data list. Inside the for loop we invoke the Lag function to get the last 31 closing prices.

We combine this list with our original data and omit all the rows with NA values. Now we are ready with our dataset.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset