Engineering lag features for the test set in time-series machine learning I am trying to do time series forecasting through machine learning. I want to engineer lag features, but was wondering what would be the best way to go about generating these features for the test set (or validation folds). Now, I'm sure that I can't just use test data to engineer these features - should I instead be using the predictions, and generating the lags iteratively?
For example, if I'm only using a 1-period lag, I would use the last entry in my training set as the lag for the first entry in my test set. I make my prediction, then use that as the 1-period lag for the second value in my test set, and so on. Is this the correct way to use lag features with machine learning? And is there a function in sklearn or another library that automates this process?
 A: Say that your test set starts at time point $t$ up to time point $N$, and you are using time lags of $1, 2,$ and $3$. In such case, you need data from time points $t, t+1, t+2$ to predict for $t+3$, all the way up to $N-3, N-2, N-1$ to predict for $N$. What follows, you cannot make predictions for the first three time points.
As you can see, to achieve this, you need somehow to split the data on time. Simplest solution is to take points $1,2,\dots,t-1$ to train set and points $t,t+1,\dots,N$ go to test set (assuming it is sorted). Another possibility, is to split the data on time into some number of blocks that are randomly assigned to either train, or test set (e.g. values $1,2,\dots,10$ go to train set $11,12,\dots,20$ to test set, $21,...$ again to train set, etc), you can rotate the blocks for $k$-fold cross-validation. Finally, you could use a variation of one-step ahead cross-validation.
You do not need any specialized functions for this, as this uses just the basic functionalities available in pandas, numpy etc.
A: I think it helps to think about whether I "have" the data at the time of prediction or forecast.
For example, if I am standing at the first point of the test set, do I "know" or "have" the information available in the last row of the train set?
If yes, then I can use it, and that would not incur in look-ahead bias. If I don't have it, then I should not use it as lag feature. Instead, I should only lag the values that I "know" at the first point of forecast.
There is code with examples here: https://github.com/trainindata/feature-engineering-for-time-series-forecasting/tree/main/02-Time-Series-Forecasting, notebooks 2 and 3.
There is also a class that creates lag features and can be used with the sklearn pipeline here: https://feature-engine.readthedocs.io/en/latest/user_guide/timeseries/forecasting/LagFeatures.html.
But at the moment, the user needs to make sure to provide sufficient data for the lags.
Alternatively, the open source library sktime is starting to provide this functionality.
