Out-of-sample Neural Networks or One-Step Forecasting RNN

By | April 18, 2018

I’ve been doing quite a bit of research on forecasting with machine learning models, specifically with multi-input neural networks. How does one predict (or forecast in this context) y+1 in the test set, when y+1 does not exist? Below, I have Jan 1st through Jan 10th. How do I predict Jan 11th?

I’ve tried the fitted function in the forecast package and it only works with time series objects. The forecast function only gives me January 10ths value, not the 11ths. I could convert the dataframe to an STL object, but am uncertain if it would apply further forecasting methods on top of my predictions.

Any thoughts or assistance would be appreciated.

Thank you.


#Create random dataframe and date column, then merge
df <- data.frame(replicate(3,sample(0:5,10,rep=TRUE)))
Date = seq(from = as.Date("2017-01-01"), to = as.Date("2017-01-10"), by = 'day')
df <- cbind(df, Date)
colnames(df)[1] <- "y"

#Create TS dataframe
ts.df <- ts(df, start = c(2017, 01), end = c(2017, 10), frequency = 1)

# Create zoo object so it works well... has time index
df <- zoo(ts.df); rm(ts.df)

#Remove Date from zoo object, as it is already indexed
df$Date <- NULL

#Putting data in the appropriate rnn format
x1 <- as.matrix(df[,2]); x2 <- as.matrix(df[,3])
y <- as.matrix(df[,1])

n_train = 7
x1_train <- as.matrix(t(x1[1:n_train,])); x2_train <- as.matrix(t(x2[1:n_train,]))
y_train <- as.matrix(y[1:n_train])

#Using the trainr function with rnn / requires the data to be in a 3d array 
x_train <- array(c(x1_train, x2_train), dim = c(dim(x1_train), 2))


#Build Model
model2 <- trainr(Y = t(y_train), X = x_train,
                 learningrate = 0.1,
                 hidden_dim = 3,
                 numepochs = 50,
                 network_type = 'rnn',
                 epoch_function = c(epoch_print,epoch_annealing),
                 sigmoid = 'logistic')

# Preparing the test data
x1_test <- as.matrix(t(x1[(n_train + 1):nrow(x1),])); x2_test <- as.matrix(t(x2[(n_train + 1):nrow(x2),]))
y_test <- as.matrix(y[(n_train + 1):nrow(x1),])

#Using the testr function with rnn / requires the data to be in a 3d array 
x_test <- array(c(x1_test, x2_test), dim = c(dim(x1_test), 2))

#Creating the predictions 
pred_test <- t(predictr(model2, x_test))

pred_test <- ts(matrix(pred_test), end = c(2017, 10), frequency = 1)

#This doesn't work, only gives me Jan 10th's value, not 11ths or 20ths..
forecast(pred_test, 10)