Joining GDP and Market Data

Introduction

In the preceding chapters, we wrangled change in GDP data and created a tibble to hold that data. We also created data objects to hold our monthly market returns. Below is a peek at those two data frames.

change_in_gdp_wrangled %>%
  head(3)
# A tibble: 3 × 3
  title         date       percent_change
  <chr>         <date>              <dbl>
1 Change in GDP 2024-04-01          0.03 
2 Change in GDP 2024-01-01          0.014
3 Change in GDP 2023-10-01          0.034
sp_returns_monthly %>%
  tail(3)
# A tibble: 3 × 4
  date         return month_year quarter_year
  <date>        <dbl> <yearmon>  <yearqtr>   
1 2024-07-01  0.00932 Jul 2024   2024 Q3     
2 2024-08-01  0.0383  Aug 2024   2024 Q3     
3 2024-09-01 -0.0216  Sep 2024   2024 Q3     

Now let’s put them together and explore how they might relate to one another. We chose this use case in order to demonstrate some data visualization capabilities alongside the concept of bucketing or binning data. As usual, we hope the tools here help us in future projects when we have more complex relationships but need fundamental tools.

Here are the packages we will be using in this chapter:

# workhorse packages
library(tidyverse)
library(tidyquant)
library(timetk)
library(readxl)
library(janitor)

# data sources
library(fredr)

# vis specific
library(gt)
library(plotly)
library(scales)

Bucketed GDP and Market Returns

Imagine we were asked to hypothesize on the relationship between GDP growth in a quarter and market returns in that quarter? It makes sense that a growing economy leads to or is correlated with a growing stock market and we might expect a positive relationship between these two variables.

Here’s a quick look at whether market returns have been positive or negative during various buckets of GDP growth. No code yet, but we will learn how to build this chart.

Surprise GDP and Market Returns

Conclusion