Time Series Analysis
R Programming & Data Analytics / Time Series Analysis

Time Series Analysis

Advanced 10 hrs 2 Concepts
Your Learning Map
📌 You already know
You can fit a regression and read its output.
🎯 You'll learn here
Representing data over time as a ts object and forecasting with ARIMA.
🌍 Where it's used
Sales, weather, prices and web traffic are all time series; forecasting them drives planning.
M1

ts Objects

Concept 1

Creating and Plotting Time Series

R's ts() creates time series objects. The forecast package provides auto.arima() for automatic model selection.

R
library(forecast)
sales <- ts(c(120,135,140,132,150,165,170,155,148,160,175,180), start=c(2023,1), frequency=12)
plot(sales)
decompose(sales) |> plot()   # trend + seasonal + remainder
R
# AirPassengers — monthly international airline passengers 1949-1960
data("AirPassengers")
plot(AirPassengers, main = "Monthly Airline Passengers (1949-1960)",
     ylab = "Passengers (thousands)", col = "#1d4ed8", lwd = 2)
Chart Output
R — Time-series line plot LIVE READY
Output below is verified. Click to run real R in your browser (first run loads ~20 MB once).
Output (verified)
[1] 280.2986
Solved Examples
Example 1 Apply the concept of Creating and Plotting Time Series to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. A ts object in R stores:
ts() wraps a numeric series with a start time and frequency (e.g. 12 for monthly).
Q2. For monthly data, the frequency argument of ts() is:
Monthly data has 12 observations per year, so frequency = 12.
Concept 2

ARIMA Forecasting

auto.arima() automatically selects the best ARIMA model using information criteria. forecast() generates future predictions with confidence intervals.

R
model <- auto.arima(sales)
summary(model)             # model parameters and fit
fc <- forecast(model, h=12)  # 12 months ahead
autoplot(fc) + labs(title='12-Month Sales Forecast')
R
# ARIMA forecast: fit model and predict next 24 months
library(forecast)
model_arima <- auto.arima(AirPassengers)
model_arima
Output
Series: AirPassengers 
ARIMA(2,1,1)(0,1,0)[12] 

Coefficients:
         ar1     ar2      ma1
      0.5960  0.2143  -0.9819
s.e.  0.0888  0.0880   0.0292

sigma^2 = 132.3:  log likelihood = -504.92
AIC=1017.84   AICc=1018.17   BIC=1029.35
R
# Plot 24-month forecast with 80% and 95% confidence intervals
forecast_24 <- forecast(model_arima, h = 24)
autoplot(forecast_24) +
  labs(title = "AirPassengers — 24-Month ARIMA Forecast",
       x = "Year", y = "Passengers (thousands)") +
  theme_minimal()
Chart Output
Solved Examples
Example 1 Apply the concept of ARIMA Forecasting to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. ARIMA models are used to:
ARIMA models the autocorrelation in a series to forecast future values.
Q2. Which function automatically selects ARIMA orders (in the forecast package)?
forecast::auto.arima() searches for the best (p,d,q) orders automatically.
Regression Analysis Machine Learning with caret