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

Time Series Analysis

Advanced 10 hrs 2 Concepts
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
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. What is the primary purpose of creating and plotting time series?
Q2. Which R package is most relevant for this topic?
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. What is the primary purpose of arima forecasting?
Q2. Which R package is most relevant for this topic?
Regression Analysis Machine Learning with caret