1) Select a Variable: Choose one of the following variables for your
final project: GDP, inflation, or the S&P 500.
Chosen Variable: GDP
2) Gather Official Data: Find official data to build a time series.
Use reliable sources such as government agencies or recognized financial
institutions.
The dataset corresponds to Mexico’s quarterly GDP, obtained from
INEGI.
For the purposes of this activity, the sample was restricted to the
2000-2025 period in order to focus on the most recent economic
dynamics.
# Libraries
library(readxl)
# Database (from Excel)
pib_data <- read_excel("PIB.xlsx")
# Converting the date column to Date format
pib_data$fecha <- as.Date(pib_data$fecha, format = "%d/%m/%y")
# Filter data from 2000 to 2025
pib_data <- pib_data[pib_data$fecha >= as.Date("2000-01-01") &
pib_data$fecha <= as.Date("2025-12-31"), ]
# Extracting the starting year and quarter
start_year <- as.numeric(format(min(pib_data$fecha), "%Y"))
start_month <- as.numeric(format(min(pib_data$fecha), "%m"))
# Converting month into quarter
start_quarter <- ifelse(start_month %in% c(1, 2, 3), 1,
ifelse(start_month %in% c(4, 5, 6), 2,
ifelse(start_month %in% c(7, 8, 9), 3, 4)))
# Creating the time series object with quarterly frequency
ts_data <- ts(pib_data$pib,
start = c(start_year, start_quarter),
frequency = 4)
4) Perform Exploratory Analysis: Begin exploring the data by
examining:
4.1) Plots (time series plots, histograms, etc.)
# Plotting the original time series
plot(ts_millions,
main = "Mexico GDP (Quarterly)",
ylab = "GDP (Millions of MXN)",
xlab = "Time",
col = "#0B3C5D",
lwd = 2)

# Plotting histogram
hist(ts_millions,
main = "Histogram of Mexico GDP",
xlab = "GDP (Millions of MXN)",
col = "#1B5886",
border = "white")

# Set white background
par(bg = "white")
# Plot boxplot
boxplot(ts_millions,
main = "Boxplot of Mexico GDP",
ylab = "GDP (Millions of MXN)",
col = "#6EB1D6",
border = "black")

4.2) Mean and variance
# Calculate descriptive statistics
mean(ts_data)
## [1] 21476031
var(ts_data)
## [1] 6.512128e+12
4.3) Standard deviation
sd(ts_data)
## [1] 2551887
4.4) Minimum and maximum values
min(ts_data)
## [1] 17459227
max(ts_data)
## [1] 25726612
# Extra: Summary of the series
summary(ts_data)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 17459227 19267356 21565811 21476031 23700502 25726612
4.5) Trend and seasonality patterns
# Seasonal subseries plot
monthplot(ts_millions,
main = "Seasonal Pattern of Mexico GDP",
ylab = "GDP (Millions of MXN)",
xlab = "Quarter",
col = "#0B3C5D",
lwd = 2,
cex.main = 1.2,
cex.lab = 1,
cex.axis = 0.9)

# Decompose the time series (in millions)
gdp_decomposition <- decompose(ts_millions)
# Plot decomposition
plot(gdp_decomposition)
