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)

3) Import and Transform Data in RStudio: Upload the dataset to RStudio and convert it into a time series object using the as.ts() function.

# Converting to time series using as.ts()
ts_data <- as.ts(ts_data)

# Displaying the time series
ts_data
##          Qtr1     Qtr2     Qtr3     Qtr4
## 2000 17562072 17786512 17980809 17778211
## 2001 17895081 17758108 17721837 17568749
## 2002 17459227 17649247 17813026 17851054
## 2003 17846064 17868090 17877093 18039385
## 2004 18277012 18520992 18509751 18707302
## 2005 18748764 18752841 18943014 19294067
## 2006 19676167 19841753 19892102 19963992
## 2007 20107079 20234691 20307737 20359607
## 2008 20354106 20505531 20521779 20197940
## 2009 18952309 18633041 19309769 19731182
## 2010 19814164 20029359 20219279 20386074
## 2011 20528681 20591212 20977196 21128881
## 2012 21330425 21461669 21516987 21684247
## 2013 21703435 21614636 21750398 21857318
## 2014 22001548 22281559 22319088 22508686
## 2015 22618510 22816784 23085376 23017589
## 2016 22992150 23094709 23296235 23556605
## 2017 23693966 23720108 23578936 23908388
## 2018 24208912 24134241 24230199 24186080
## 2019 24190818 24123758 24122015 23947538
## 2020 23660840 19187221 22164196 23122943
## 2021 23288550 23507958 23301496 23574312
## 2022 23901537 24184105 24386676 24671752
## 2023 24798272 24983768 25181152 25202781
## 2024 25255205 25227221 25524271 25275173
## 2025 25360618 25489306 25506365 25726612
# Converting to millions (for a better and easier visualization)
ts_millions <- ts_data / 1e6

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)