Time Series Lesson 29 – Linear Reg | Dataplexa

Linear Regression for Time Series Forecasting

Once a time series is split correctly, the next question is simple:

Can a basic model learn anything useful from past values?

Linear regression answers that question.

It is not the most powerful forecasting model, but it is the clearest way to understand how forecasting actually works.


The Real-World Problem

Suppose you manage daily electricity demand forecasting.

Every morning, you want to predict tomorrow’s usage using recent history.

You are not trying to capture complex patterns yet — you just want a reasonable baseline.


What Linear Regression Sees in Time Series

Linear regression does not understand time by itself.

So we must teach it what “past” means.

We do this by converting the series into:

  • Input → previous value
  • Target → next value

This simple idea turns forecasting into supervised learning.


Our Example Series

We continue using daily electricity usage.

Python: Time Series Data
import numpy as np

np.random.seed(4)
days = np.arange(200)
usage = 120 + 0.2*days + 10*np.sin(2*np.pi*days/7) + np.random.normal(0,4,200)

The pattern is realistic:

  • Gradual upward trend
  • Weekly cycles
  • Random noise

Turning Time Series into Regression Data

To predict tomorrow, we use today.

That means:

  • X = usage at time t
  • y = usage at time t+1
Python: Creating Lag Features
X = usage[:-1].reshape(-1,1)
y = usage[1:]

Each point now represents:

“Given today’s usage, predict tomorrow’s.”


Train–Test Split

We respect time order.

Python: Time-Based Split
split = int(len(X) * 0.8)

X_train, X_test = X[:split], X[split:]
y_train, y_test = y[:split], y[split:]

Training the Linear Regression Model

Now the model learns a simple rule:

How much tomorrow depends on today.

Python: Linear Regression Model
from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(X_train, y_train)

predictions = model.predict(X_test)

Actual Forecast vs Real Values

This plot shows how the model performs on future data.

What you should observe:

  • Predictions follow the general trend
  • Weekly patterns are partially captured
  • Sharp fluctuations are missed

This is expected behavior.


Why Linear Regression Works (and Fails)

Linear regression is good at:

  • Capturing trends
  • Providing a baseline forecast
  • Explaining relationships clearly

It struggles with:

  • Strong seasonality
  • Non-linear behavior
  • Long-term dependencies

That is not a weakness — it is a learning step.


Why This Lesson Matters

Every advanced forecasting model is compared against simple baselines.

If a complex model cannot beat linear regression, it is not worth using.

This lesson gives you that baseline understanding.


Practice Questions

Q1. Why do we create lag features?

To convert time series data into supervised learning format.

Q2. What patterns does linear regression capture best?

Overall trend and simple linear relationships.

Key Takeaways

  • Forecasting can be framed as regression
  • Lag features create learning structure
  • Linear regression provides a strong baseline

Next lesson: we’ll improve this baseline using tree-based models.