I'm developing a time series linear regression model that produces a multi-output prediction of 12 steps. This takes in 10 lags as an input.
I would like to test other predictive variable now that I have the base model complete. But I'm not sure how to set up the data. My current process is below:
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
log_sales = np.array([7.95366978, 8.02092772, 8.08517875, 8.04878828, 8.06589355,
8.07496036, 8.11939859, 8.14728826, 8.29579811, 8.10016145,
8.13798045, 8.17216445, 8.28576542, 8.20985248, 8.24590926,
8.17779668, 8.3035048 , 8.25062008, 8.17976049, 8.19560957,
8.20193435, 8.24170316, 8.16280135, 8.20467183, 8.24826745,
8.07651533, 8.22147895, 8.24354551, 8.40290405, 8.24957515,
8.40424843, 8.35819746, 8.53227883, 8.46779284, 8.47449444,
8.51278348, 8.48322267, 8.4854961 , 8.66094716, 8.58783803,
8.59174392, 8.64558641, 8.64646553, 8.66544077, 8.80747189,
8.81269461, 8.52892411, 7.96450336, 9.18911674, 8.7798651 ,
8.83666459, 8.90245559, 8.92572027, 8.8607829 , 8.86827251,
8.83214991, 8.44354665, 8.41693077, 8.452548 , 8.38731227])
df = pd.Dataframe(log_sales)
T = 10 #number of lags
Ntest = 12 #number of forecasted periods
train = df_.iloc[:-Ntest]
test = df_.iloc[-Ntest:]
series = df_['log_eaches'].to_numpy()
Tx = T
Ty = Ntest
X = []
Y = []
series = np.array(df_['log_eaches'])
for t in range(len(series) - Tx - Ty + 1):
x = series[t:t+Tx]
X.append(x)
y = series[t+Tx : t+Tx+Ty]
Y.append(y)
X = np.array(X).reshape(-1, Tx)
Y = np.array(Y).reshape(-1, Ty)
N = len(X)
print(X.shape, Y.shape)
Xtrain_m, Ytrain_m = X[:-1], Y[:-1]
Xtest_m, Ytest_m = X[-1:], Y[-1:]
train_idx = df_.index <= train.index[-1]
test_idx = ~ train_idx
model = LinearRegression()
model.fit(Xtrain_m, Ytrain_m)
From the loop that puts my x and y together, I'm not sure how to add in regressors so that the model can consider them.
Here are examples of regressors.
promo_flag = np.array([0., 0., 1., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 1., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.])
promo_item_count = np.array([ -1., -1., 290., 290., -1., -1., -1., -1., 291., -1., -1.,
-1., -1., -1., 283., -1., -1., -1., -1., -1., -1., -1.,
-1., -1., -1., -1., -1., -1., 57., -1., -1., -1., -1.,
-1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,
503., -1., 476., 476., -1., -1., -1., -1., -1., -1., -1.,
-1., -1., -1., -1., -1.])
promo_duration = np.array([-1. , -1. , 26.58333333, 33. , -1. ,
-1. , -1. , -1. , 29. , -1. ,
-1. , -1. , -1. , -1. , 30. ,
-1. , -1. , -1. , -1. , -1. ,
-1. , -1. , -1. , -1. , -1. ,
-1. , -1. , -1. , 6. , -1. ,
-1. , -1. , -1. , -1. , -1. ,
-1. , -1. , -1. , -1. , -1. ,
-1. , -1. , -1. , -1. , 9. ,
-1. , 59. , 59. , -1. , -1. ,
-1. , -1. , -1. , -1. , -1. ,
-1. , -1. , -1. , -1. , -1. ])
How can I add those into X and Y to test in the model?