For this assignment, we were given the task to investigate the interdependence among key economic indicators. The Dow Jones Industrial Average (DJIA), the 10-Year Treasury Yield (DGS10) and the Unemployment Rate (UNRATE) and determine whether there is a relationship. My goal is to explore the potential relationships between interest rates, market performance and unemployment levels by visualizing their trends over time and interpreting any significant patterns or insights that may emerge. In this assignment, I chose to complete the bonus opportunity by retrieving the economic indicator data directly from the Federal Reserve Economic Data (FRED) API using base R code. This approach for retrieving data allows for a more dynamic and automated access to the data that you need. Our datasets go as far back as far back as 1948 but the DJIA data only goes as far back as 2015 so this is the time frame we’ll analyze
# STEP 1 & STEP 2
# Load the required libraries
library(httr)
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.3.3
# Set my FRED API key
api_key <- "2317fa5fe52f70c295f704c77ad218d9"
# Function to get data from FRED
get_fred_data <- function(series_id, api_key) {
url <- paste0(
"https://api.stlouisfed.org/fred/series/observations?series_id=",
series_id,
"&api_key=", api_key,
"&file_type=json",
"&frequency=m"
)
response <- GET(url)
data <- fromJSON(rawToChar(response$content))
df <- data$observations
df <- data.frame(date = as.Date(df$date), value = as.numeric(df$value))
names(df)[2] <- series_id
return(df)
}
# Download data into data frames
djia <- get_fred_data("DJIA", api_key)
## Warning in data.frame(date = as.Date(df$date), value = as.numeric(df$value)):
## NAs introduced by coercion
dgs10 <- get_fred_data("DGS10", api_key)
## Warning in data.frame(date = as.Date(df$date), value = as.numeric(df$value)):
## NAs introduced by coercion
unrate <- get_fred_data("UNRATE", api_key)
# Merge datasets and align them by monthly date
merged_data <- Reduce(function(x, y) merge(x, y, by = "date", all = TRUE),
list(djia, dgs10, unrate))
# Remove rows with NA
merged_data <- na.omit(merged_data)
# View structure of the merged dataset
str(merged_data)
## 'data.frame': 119 obs. of 4 variables:
## $ date : Date, format: "2015-06-01" "2015-07-01" ...
## $ DJIA : num 17927 17795 17062 16340 17182 ...
## $ DGS10 : num 2.36 2.32 2.17 2.17 2.07 2.26 2.24 2.09 1.78 1.89 ...
## $ UNRATE: num 5.3 5.2 5.1 5 5 5.1 5 4.8 4.9 5 ...
## - attr(*, "na.action")= 'omit' Named int [1:810] 1 2 3 4 5 6 7 8 9 10 ...
## ..- attr(*, "names")= chr [1:810] "1" "2" "3" "4" ...
# STEP 3
# Set up base plot with DJIA
par(mar = c(5, 4, 5, 4) + 0.3, xpd = TRUE)
# Plot DJIA
plot(merged_data$date, merged_data$DJIA,
type = "l", col = "blue", lwd = 2,
xlab = "Date", ylab = "DJIA",
main = "Trends in DJIA, 10-Yr Yield, and Unemployment Rate")
# Add right axis for DGS10 and UNRATE
par(new = TRUE)
plot(merged_data$date, merged_data$DGS10,
type = "l", col = "red", lwd = 2, axes = FALSE, xlab = "", ylab = "")
lines(merged_data$date, merged_data$UNRATE, col = "darkgreen", lwd = 2, lty = 2)
axis(side = 4, col.axis = "black", las = 1)
mtext("10-Year Treasury Yield / Unemployment Rate (%)", side = 4, line = 3)
# Add legend
legend("top", inset = -0.15, horiz = TRUE,
legend = c("DJIA", "10-Yr Treasury Yield", "Unemployment Rate"),
col = c("blue", "red", "darkgreen"),
lty = c(1, 1, 2), lwd = 2, bty = "n")
This line plot reveals key relationships and patters between DJIA, 10-Year Treasury Yield and the Unemployment Rate from 2015 to 2024. I observed some significant patterns such as periods of DJIA market declines lead to increasing unemployment and thus resulting in decreasing interest rates which is usually conducted by the FED to stimulate the economy. This decline in the DJIA market occurred in 2020 due to the emergence of the COVID-19 virus. We can then see that periods of falling unemployment and interest rates generally correlate with rising stock market performance particularly visible during the market recovery between 2020 and 2022. These patterns suggest that investor sentiment and the fed policy shifts, which is reflected in bond yields and the unemployment rate are intertwined with equity market trends, which can guide future fed policy and investment strategies for investors.
Although the assignment required only one visualization, I chose to include a second chart as a bonus visualization to offer a different view of the data. This individual layout presents each economic indicator which are DJIA, 10-Year Treasury Yield and Unemployment Rate in its own separate time series plot. This format makes it easier to analyze individual trends clearly and observe how each variable behaved during specific time periods, such as the sharp rise in the unemployment rate in 2020 due to COVID-19 and the subsequent decrease of interest rates by the beloved Jerome Powell. By visually isolating the indicators, this approach enhances comparative analysis and provides a more focused understanding of their potential interdependencies.
# Set layout
par(mfrow = c(3, 1), mar = c(4, 4, 2, 2))
# DJIA individual Plot
plot(merged_data$date, merged_data$DJIA, type = "l",
col = "blue", lwd = 2,
main = "Dow Jones Industrial Average (DJIA)",
xlab = "", ylab = "DJIA")
# 10-Year Treasury Yield individual Plot
plot(merged_data$date, merged_data$DGS10, type = "l",
col = "red", lwd = 2,
main = "10-Year Treasury Yield (%)",
xlab = "", ylab = "Yield (%)")
# Unemployment Rate individual Plot
plot(merged_data$date, merged_data$UNRATE, type = "l",
col = "darkgreen", lwd = 2,
main = "Unemployment Rate (%)",
xlab = "Date", ylab = "Rate (%)")