Time Series (ts & forecasting basics)

A time series is a sequence of values measured at regular time intervals, and R's ts object stores that data together with its start date and seasonal frequency so it can be plotted, decomposed, and forecast.

Learn Time Series (ts & forecasting basics) in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll build ts objects, plot them over time, split them into trend, seasonal, and random parts, read an ACF plot, and produce a simple forecast.

What You'll Learn in This Lesson

1️⃣ Building and Plotting a ts Object

Wrap a numeric vector in ts() , telling it when the data start and how many points make one cycle via frequency . Once it's a ts, plot() automatically puts time on the x-axis.

2️⃣ Decomposing into Trend, Season, and Noise

decompose() separates a series into a smooth trend , a repeating seasonal shape, and a leftover random component. Plotting the result stacks all four panels (observed, trend, seasonal, random) so you can see each piece.

3️⃣ Autocorrelation and a First Forecast

The acf() plot reveals how each point relates to earlier ones — your main clue to trend and seasonality. Once you understand the structure, HoltWinters() gives a quick forecast, and the forecast package's auto.arima() is the modern go-to.

Your turn. Fill in the # TODO blank and run it.

Use R's classic built-in dataset to practice the whole workflow: inspect, plot, decompose multiplicatively, and forecast. No data setup needed — AirPassengers is always available.

📋 Quick Reference — Time Series

Practice quiz

What does the ts() function create?

  • A plot
  • A matrix
  • A list of dates
  • A time series object that stores values with their time structure

Answer: A time series object that stores values with their time structure. ts() wraps a numeric vector with start time and frequency.

In ts(), what does the frequency argument mean?

  • Observations in one full seasonal cycle
  • How often you measured per second
  • The number of years
  • The sampling error

Answer: Observations in one full seasonal cycle. Frequency is observations per cycle: 12 monthly, 4 quarterly, 7 daily-weekly.

What frequency would you use for monthly data with a yearly season?

  • 1
  • 12
  • 4
  • 365

Answer: 12. Monthly data with a yearly cycle uses frequency = 12.

What does decompose() split a series into?

  • Mean and variance
  • Rows and columns
  • Trend, seasonal, and random components
  • Past and future

Answer: Trend, seasonal, and random components. decompose() separates trend, seasonal, and random parts.

When should you use a multiplicative decomposition?

  • When the series is constant
  • When there is no season
  • Always by default
  • When seasonal swings grow with the level

Answer: When seasonal swings grow with the level. Use multiplicative when swings get bigger as the series climbs (e.g. AirPassengers).

What is the default type of decompose()?

  • Additive
  • Multiplicative
  • Logarithmic
  • Random

Answer: Additive. decompose() is additive (trend + seasonal + random) by default.

What does the acf() plot show?

  • The forecast
  • How each point correlates with earlier points (autocorrelation)
  • The mean per year
  • The residual variance only

Answer: How each point correlates with earlier points (autocorrelation). acf() shows autocorrelation across lags, revealing trend and seasonality.

On a monthly series, a tall ACF spike at lag 12 suggests what?

  • A data error
  • No pattern
  • A yearly seasonal cycle
  • Random noise

Answer: A yearly seasonal cycle. A spike at the seasonal lag (12 for monthly) signals seasonality.

Which base R function does exponential-smoothing forecasting?

  • HoltWinters()
  • lm()
  • table()
  • summary()

Answer: HoltWinters(). HoltWinters() ships with base R for level/trend/seasonal smoothing.

Why prefer the forecast package's auto.arima() over a single point line?

  • It is base R
  • It plots faster
  • It needs no data
  • It returns prediction intervals showing uncertainty

Answer: It returns prediction intervals showing uncertainty. auto.arima()/forecast() give intervals, not just one point estimate.