Time Series

Back

Loading concept...

🎬 Time Series in R: Your Data’s Diary Through Time

The Story of Time: A Simple Analogy

Imagine you have a diary 📔 where you write down the temperature every single day for a whole year. Each page has a date and a number. That’s a time series—a collection of measurements taken at regular intervals over time!

Think of it like a movie 🎥: instead of a single photograph (one data point), you have frames playing in sequence, telling a story of how things change.


📦 Time Series Objects: Building Your Data Diary

What is a Time Series Object?

In R, a time series object is like a special notebook that knows:

  • What values you recorded
  • When each value was recorded
  • How often you took notes (daily? monthly? yearly?)

Creating Your First Time Series

# Monthly ice cream sales for one year
sales <- c(100, 120, 150, 200,
           250, 300, 280, 260,
           200, 150, 110, 105)

# Turn it into a time series
ice_cream_ts <- ts(sales,
                   start = c(2024, 1),
                   frequency = 12)

print(ice_cream_ts)

What does this mean?

  • start = c(2024, 1) → Starts in January 2024
  • frequency = 12 → 12 observations per year (monthly!)

Common Frequencies

Data Type Frequency Think of it as…
Yearly 1 One birthday candle per year 🎂
Quarterly 4 Four seasons 🌸☀️🍂❄️
Monthly 12 Twelve months 📅
Weekly 52 Weekly allowance 💰
Daily 365 Daily breakfast 🥣

Checking Your Time Series

# When does it start?
start(ice_cream_ts)

# When does it end?
end(ice_cream_ts)

# How often?
frequency(ice_cream_ts)

📊 Time Series Plotting: Drawing the Story

Why Plotting Matters

Imagine trying to describe a roller coaster 🎢 with just numbers vs. actually seeing its picture. The picture tells you everything instantly!

Your First Time Series Plot

# Simple plot
plot(ice_cream_ts,
     main = "Ice Cream Sales 2024",
     xlab = "Month",
     ylab = "Sales",
     col = "blue",
     lwd = 2)

What you’ll see:

  • X-axis shows time (months)
  • Y-axis shows your values (sales)
  • A line connects all the dots!

Adding Style to Your Plot

# Fancy plot with grid
plot(ice_cream_ts,
     main = "🍦 Ice Cream Sales",
     col = "darkorange",
     type = "o",    # o = both lines AND points
     pch = 19)      # solid circles

# Add a helpful grid
grid(col = "lightgray")

Reading the Plot

When you look at a time series plot, ask yourself:

  • 📈 Is it going up? (Upward trend)
  • 📉 Is it going down? (Downward trend)
  • 🔄 Does it repeat a pattern? (Seasonality)
  • 📍 Are there sudden jumps? (Anomalies)

🔗 Autocorrelation: Does Today Remember Yesterday?

The Memory of Data

Autocorrelation answers a simple question: “If today is hot, is tomorrow likely to be hot too?”

Think of it like momentum 🏃: if you’re running fast right now, you’ll probably still be running fast in the next second.

What Does “Auto” Mean?

  • Auto = Self
  • Correlation = Relationship

So autocorrelation means: How related is the data to itself at different time gaps?

Calculating Autocorrelation

# Check autocorrelation
acf(ice_cream_ts,
    main = "How Much Does
    Sales Remember Itself?")

Reading the ACF Plot

The plot shows vertical bars:

  • Bar at lag 1: “How similar is this month to last month?”
  • Bar at lag 2: “How similar is this month to 2 months ago?”
  • Bars inside blue lines: No significant memory
  • Bars outside blue lines: Strong memory! 📢

Real-Life Example

# Temperature autocorrelation
# If Monday is cold, Tuesday
# is probably cold too!

temps <- c(32, 33, 31, 28, 25,
           24, 26, 30, 35, 38)
acf(temps)

High autocorrelation at lag 1 = Data has short-term memory (yesterday affects today)


📐 Moving Averages: Smoothing the Bumps

The Bumpy Road Problem

Imagine your daily step count: 8000, 12000, 6000, 15000, 7000…

It’s all over the place! 🎢 Hard to see if you’re actually walking more over time.

The Solution: Take an Average

A moving average is like wearing glasses that blur out the tiny details so you can see the big picture.

graph TD A["Raw Data: Bumpy 📈📉"] --> B["Moving Average Filter"] B --> C["Smooth Data: Clear Trend ➡️"]

Calculating Moving Average in R

# Install if needed
# install.packages("zoo")
library(zoo)

# 3-month moving average
smooth_sales <- rollmean(ice_cream_ts,
                         k = 3,
                         align = "center")

# Compare: original vs smooth
plot(ice_cream_ts,
     col = "gray", lty = 2)
lines(smooth_sales,
      col = "red", lwd = 2)
legend("topright",
       c("Original", "Smoothed"),
       col = c("gray", "red"),
       lty = c(2, 1))

How It Works

For a 3-point moving average:

  • Point 1: Average of values 1, 2, 3
  • Point 2: Average of values 2, 3, 4
  • Point 3: Average of values 3, 4, 5
  • …and so on!
Original: 100, 120, 150, 200, 250
Average:  (100+120+150)/3 = 123
          (120+150+200)/3 = 157
          (150+200+250)/3 = 200

Choosing Window Size

Window Effect Use When
Small (3) Still bumpy Need details
Medium (7) Nice balance Most cases
Large (12) Very smooth See long trends

✨ Exponential Smoothing: Smart Forgetting

The Memory Fade

Exponential smoothing is like human memory: recent events are vivid, but old memories fade.

  • Yesterday’s weather? Remember clearly! 🌞
  • Last week’s weather? Kind of remember…
  • Last year’s weather? Barely remember! 😶‍🌫️

The Magic Number: Alpha (α)

Alpha controls how much we trust new data vs old data:

  • α close to 1: “I mostly trust recent data!” (jumpy)
  • α close to 0: “I mostly trust old patterns!” (smooth)

Simple Exponential Smoothing in R

# Simple exponential smoothing
library(forecast)

# Let R pick the best alpha
smooth_fit <- ses(ice_cream_ts, h = 3)

# See the smoothed values
print(smooth_fit)

# Plot it
plot(smooth_fit,
     main = "Exponential Smoothing")

The Formula (Don’t Panic!)

New Estimate = α × (New Data) + (1-α) × (Old Estimate)

Example with α = 0.3:

  • Old estimate: 100
  • New data: 120
  • New estimate: 0.3 × 120 + 0.7 × 100 = 36 + 70 = 106

See? It moved a little toward 120, but not all the way!


🧩 Time Series Decomposition: Taking Apart the Puzzle

Every Time Series Has Hidden Layers

Like a cake 🎂, your time series has layers:

  1. Trend: The long-term direction (going up or down over years)
  2. Seasonal: Patterns that repeat (every summer, every December)
  3. Remainder/Random: Unpredictable surprises (a random spike)
graph TD A["Time Series Data"] --> B{Decompose} B --> C["🔼 Trend"] B --> D["🔄 Seasonal"] B --> E["❓ Random"]

Decomposing in R

# Decompose our ice cream sales
parts <- decompose(ice_cream_ts)

# See all the pieces
plot(parts)

Reading the Decomposition

You’ll see 4 plots stacked:

  1. Observed: Your original data
  2. Trend: Is business growing or shrinking?
  3. Seasonal: What months always spike?
  4. Random: What’s left unexplained?

Additive vs Multiplicative

Additive (default): Seasonal swings stay the same size

  • Winter: always -20 sales
  • Summer: always +50 sales
decompose(ice_cream_ts,
          type = "additive")

Multiplicative: Seasonal swings grow with the trend

  • When sales are low: small swings
  • When sales are high: big swings
decompose(ice_cream_ts,
          type = "multiplicative")

🔮 Forecasting Basics: Predicting Tomorrow

The Crystal Ball of Data

Forecasting is using patterns from the past to guess what happens next. Like predicting tomorrow’s weather based on today! ⛅

Simple Forecast Methods

Method 1: Naive (“Tomorrow = Today”)

# Simple: next value = last value
naive_forecast <- naive(ice_cream_ts, h = 3)
plot(naive_forecast)

Method 2: Mean (“Tomorrow = Average”)

# Use the average of all past data
mean_forecast <- meanf(ice_cream_ts, h = 3)
plot(mean_forecast)

Method 3: Seasonal Naive

# Same as this month last year
snaive_forecast <- snaive(ice_cream_ts, h = 12)
plot(snaive_forecast)

The Forecast Package Magic

library(forecast)

# Let R figure out the best method!
auto_forecast <- forecast(ice_cream_ts, h = 6)

# Beautiful plot with uncertainty
plot(auto_forecast,
     main = "6-Month Ice Cream Forecast")

Understanding Forecast Output

Point Forecast: Our best guess
80% Confidence: Pretty sure it's in this range
95% Confidence: Almost certain it's in this range

The blue shaded areas show uncertainty—the further into the future, the wider they get!

Checking Forecast Accuracy

# Split data: train on past, test on future
train <- window(ice_cream_ts, end = c(2024, 9))
test <- window(ice_cream_ts, start = c(2024, 10))

# Forecast from training data
my_forecast <- forecast(train, h = 3)

# Compare to actual
accuracy(my_forecast, test)

Key Metrics:

  • MAE (Mean Absolute Error): Average mistake size
  • RMSE: Penalizes big mistakes more
  • MAPE: Mistake as a percentage

🎯 Putting It All Together

Here’s the complete workflow for time series analysis:

graph TD A["1. Create ts Object"] --> B["2. Plot &amp; Explore"] B --> C["3. Check Autocorrelation"] C --> D["4. Smooth if Needed"] D --> E["5. Decompose"] E --> F["6. Forecast!"]

Complete Example

# 1. Create time series
my_data <- ts(c(100, 120, 150, 200, 250,
                300, 280, 260, 200, 150,
                110, 105),
              start = c(2024, 1),
              frequency = 12)

# 2. Plot it
plot(my_data, main = "My Data Story")

# 3. Check memory (autocorrelation)
acf(my_data)

# 4. Smooth it
library(zoo)
smooth <- rollmean(my_data, k = 3)

# 5. Decompose it
parts <- decompose(my_data)
plot(parts)

# 6. Forecast next 6 months!
library(forecast)
future <- forecast(my_data, h = 6)
plot(future)

🌟 Key Takeaways

Concept One-Line Summary
ts() Object Your data’s time-aware container
plot() See the story visually
acf() Does data remember itself?
rollmean() Smooth out the noise
ses() Smart, fading memory
decompose() Trend + Season + Random
forecast() Predict the future!

🚀 You Did It!

You now understand how R handles time—from creating time-aware data objects to peering into the future with forecasts. Like learning to read a diary that spans years, you can now uncover stories hidden in sequential data!

Remember: Every time series is just a story waiting to be told. Now you have the tools to read it, smooth it, break it apart, and even predict the next chapter! 📖✨

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.