Introduction: In this analysis, we will examine the US Diffusion Index and compare it to the Chicago Fed National Activity Index: Diffusion Index (CFNAIDIFF). The US Diffusion Index is derived from economic variables such as Industrial Production, Unemployment Rate, and Retail Sales, while the CFNAIDIFF provides an overall economic activity measure for the U.S. The goal is to explore their relationships and visualize trends over time. Step 1: US Diffusion Index * 1.1 Data Selection * We will use the following economic variables to construct the US Diffusion Index:

knitr::opts_chunk$set(echo = TRUE)
variables <- c('INDPRO', 'UNRATE', 'RSAFS')

start_date <- '2010-01-01'
end_date <- '2024-09-30'
get_fred_data <- function(series_id) {
  getSymbols(Symbols = series_id, src = 'FRED', from = start_date, to = end_date, auto.assign = FALSE) %>% 
    fortify.zoo() %>% 
    rename(date = Index, !!series_id := series_id) %>% 
    select(date, !!series_id)
}

data_list <- map(variables, get_fred_data)
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(series_id)
## 
##   # Now:
##   data %>% select(all_of(series_id))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Merge all series into a single data frame
merged_data <- reduce(data_list, left_join, by = 'date')

1.3 Calculate First Differences and Binary Transformation: We will compute first differences and convert them to binary values (1 for positive changes, -1 for negative changes) to construct the diffusion index.

binary_diffusion_data <- merged_data %>% 
  mutate(across(all_of(variables), ~ (.x - lag(.x)))) %>%  # First differences
  mutate(across(all_of(variables), ~ if_else(is.na(.x), NA_real_, if_else(.x > 0, 1, -1)))) # Convert to 1 or -1

# Calculate diffusion index as proportion of positive changes
calc_diffusion <- function(x) {
  if (all(is.na(x))) {
    return(NA_real_)
  } else {
    return(mean(x == 1, na.rm = TRUE))  # Proportion of positive changes (1s)
  }
}
  diffusion_index <- binary_diffusion_data %>% 
  rowwise() %>% 
  mutate(diffusion_index = calc_diffusion(c_across(all_of(variables)))) %>% 
  ungroup() %>% 
  select(date, diffusion_index)

1.4 US Diffusion Index Visualization Below is a line plot of the US Diffusion Index with smoothing.

g1 <- ggplot(diffusion_index, aes(x = date, y = diffusion_index, color = "US Diffusion Index")) + 
  geom_line() +
  geom_smooth(se = FALSE) + 
  labs(title = 'Chart 1: US Diffusion Index', x = 'Date', y = 'Diffusion Index') +
  theme_minimal() +
  scale_color_manual(values = "darkblue") 
print(g1)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).

Step 2: Chicago Fed National Activity Index (CFNAIDIFF) * 2.1 Data Retrieval * The Chicago Fed National Activity Index: Diffusion Index (CFNAIDIFF) is retrieved from FRED.

cfnaidiff <- getSymbols(Symbols = 'CFNAIDIFF', src = 'FRED', from = start_date, to = end_date, auto.assign = FALSE) %>%
  fortify.zoo() %>%
  rename(date = Index, cfnaidiff = 'CFNAIDIFF') %>%
  select(date, cfnaidiff)

2.2 CFNAIDIFF Visualization Below is a line plot of the CFNAIDIFF with smoothing.

g2 <- ggplot(cfnaidiff, aes(x = date, y = cfnaidiff, color = "CFNAIDIFF")) + 
  geom_line() +
  geom_smooth(se = FALSE) + 
  labs(title = 'Chart 2: Chicago Fed National Activity Index: Diffusion Index (CFNAIDIFF)', x = 'Date', y = 'CFNAIDIFF') +
  theme_minimal() +
  scale_color_manual(values = "red") 
print(g2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Step 3: Comparison of US Diffusion Index and CFNAIDIFF * 3.1 Merging and Correlation * We will merge the US Diffusion Index and CFNAIDIFF and calculate the correlation between them.

comparison_df <- left_join(diffusion_index, cfnaidiff, by = 'date')

# Calculate correlation coefficient
correlation <- cor(comparison_df$diffusion_index, comparison_df$cfnaidiff, use = "pairwise.complete.obs")
cat("Correlation between US Diffusion Index and CFNAIDIFF:", correlation, "\n")
## Correlation between US Diffusion Index and CFNAIDIFF: 0.1372918

3.2 Comparison Plot The following plot compares the US Diffusion Index and CFNAIDIFF.

comparison_long <- comparison_df %>%
  pivot_longer(cols = c(diffusion_index, cfnaidiff),
               names_to = 'index', values_to = 'value')

g3 <- ggplot(comparison_long, aes(x = date, y = value, color = index)) +
  geom_line() +
  geom_smooth(se = FALSE) + 
  labs(title = 'Chart 3: US Diffusion Index vs CFNAIDIFF',
       x = 'Date',
       y = 'Index Value',
       color = 'Index') +
  theme_minimal()

print(g3)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).

3.3 Interactive Plot For a more interactive exploration of the data, we present the comparison plot using Plotly.

g3 <- ggplot(comparison_long, aes(x = date, y = value, color = index)) +
  geom_line() +
  geom_smooth(se = FALSE, na.rm = TRUE) + 
  labs(title = 'Chart 3: US Diffusion Index vs CFNAIDIFF',
       x = 'Date',
       y = 'Index Value',
       color = 'Index') +
  theme_minimal()

interactive_plot <- ggplotly(g3)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
interactive_plot

Conclusion: The analysis of the US Diffusion Index and its comparison to the Chicago Fed National Activity Index reveals the similarities and trends between these two indices over the period from 2010 to 2024. The correlation between the two indices can help assess the overall economic conditions in the U.S. and identify key turning points.