Lag Features and Time-Based Features
What you'll learn: How to engineer powerful features from time series data by creating lagged values and extracting meaningful temporal components from timestamps.
The Core Idea
When predicting the future from time series data, you need to give your model a way to "remember" the past and understand temporal patterns. Lag features let you incorporate historical values as inputs, while time-based features capture cyclical and seasonal patterns hidden in timestamps.
Think of lag features like looking in your rear-view mirror while driving: yesterday's temperature (lag-1), last week's sales (lag-7), or last month's stock price (lag-30) often help predict tomorrow. You literally shift past values forward in time and use them as predictive features.
Lag Features
A lag-1 feature is simply the previous observation. For a sales dataset, if Monday's sales were 100 and Tuesday's were 120, then Tuesday's lag-1 feature equals 100. You can create multiple lags (lag-1, lag-7, lag-30) to capture different temporal dependencies you discovered through autocorrelation analysis.
Time-Based Features
Timestamps contain rich information you can extract:
- Hour of day (0-23): captures daily patterns like morning rush hours
- Day of week (0-6): distinguishes weekday vs weekend behavior
- Month (1-12): reveals yearly seasonality
- Season indicators: binary flags for spring, summer, fall, winter
- Is_weekend, is_holiday: categorical patterns
These features let your model learn that "sales spike on Saturdays" or "energy usage peaks at 6 PM" without explicitly programming those rules.
Why This Matters
Raw timestamps (like "2024-01-15 14:30:00") are nearly useless to models. By engineering lag and time-based features, you transform temporal structure into predictable patterns your model can learn from.
Key Takeaway: Lag features capture autocorrelation by using past values as predictors, while time-based features extract cyclical patterns from timestamps—together they form the foundation of effective time series forecasting.