R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Introduction

The goal of this project is to create a Diffusion Index for three economic indicators in the United States and compare it to the Chicago Fed National Activity Index: Diffusion Index (CFNAIDIFF). This comparison helps assess alignment between economic activity and a custom diffusion index.

Data Preparation

We use FRED API to collect the following economic series: 1. PAYEMS: Total Nonfarm Employment 2. HOUST: Housing Starts 3. BUSAPPWNSAUS: Business Applications

CFNAIDIFF is also collected for comparison. Data spans from 2000 to 2025.

Libraries and Data Collection

```r # Load necessary libraries library(tidyverse) library(lubridate) library(ggplot2) library(scales) library(fredr) library(zoo)

FRED API key

fredr_set_key(“5c4c0e08770b53cb9a50d07685a20054”)

Fetch data

employment_data <- fredr(series_id = “PAYEMS”, observation_start = as.Date(“2000-01-01”)) housing_starts <- fredr(series_id = “HOUST”, observation_start = as.Date(“2000-01-01”)) business_applications <- fredr(series_id = “BUSAPPWNSAUS”, observation_start = as.Date(“2000-01-01”)) cfnaidiff <- fredr(series_id = “CFNAIDIFF”, observation_start = as.Date(“2000-01-01”))

Clean data function

clean_data <- function(data, col_name) { data %>% select(date, value) %>% rename({{col_name}} := value) %>% mutate(date = as.Date(date)) }

Clean datasets

employment_data <- clean_data(employment_data, employment) housing_starts <- clean_data(housing_starts, housing_starts) business_applications <- clean_data(business_applications, business_apps) cfnaidiff <- clean_data(cfnaidiff, cfnaidiff)

Combine data

economic_data <- employment_data %>% full_join(housing_starts, by = “date”) %>% full_join(business_applications, by = “date”) %>% drop_na()

Standardize function

standardize <- function(x) { (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE) }

Standardize and calculate diffusion index

economic_data <- economic_data %>% mutate( employment_std = standardize(employment), housing_starts_std = standardize(housing_starts), business_apps_std = standardize(business_apps), diffusion_index = rowMeans(cbind(employment_std, housing_starts_std, business_apps_std), na.rm = TRUE) )

Apply 6-month smoothing

economic_data <- economic_data %>% arrange(date) %>% mutate(smoothed_diffusion = zoo::rollapply(diffusion_index, 6, mean, fill = NA, align = “right”))

comparison_data <- economic_data %>% inner_join(cfnaidiff, by = “date”) %>% rename(cfnaidiff_smoothed = cfnaidiff) %>% mutate(cfnaidiff_smoothed = zoo::rollapply(cfnaidiff_smoothed, 6, mean, fill = NA, align = “right”))

Correlation

correlation <- cor(comparison_data\(smoothed_diffusion, comparison_data\)cfnaidiff_smoothed, use = “complete.obs”) cat(“Correlation between Diffusion Index and CFNAIDIFF:”, round(correlation, 3))

Plot comparison

p1 <- ggplot(comparison_data, aes(x = date)) + geom_line(aes(y = smoothed_diffusion, color = “Diffusion Index”), linewidth = 1) + geom_line(aes(y = cfnaidiff_smoothed, color = “CFNAIDIFF”), linewidth = 1) + labs( title = “Comparison of Diffusion Index and CFNAIDIFF”, x = “Date”, y = “Index Value”, color = “Legend” ) + scale_color_manual(values = c(“Diffusion Index” = “blue”, “CFNAIDIFF” = “red”)) + theme_minimal()

Save plot

ggsave(“diffusion_comparison_plot.png”, plot = p1, width = 10, height = 6) p1

Insights

Correlation Coefficient: The correlation between the custom Diffusion Index and CFNAIDIFF is 0.559,suggesting a moderate positive relationship.

#Visual Comparison:

The Diffusion Index (blue) and CFNAIDIFF (red) show some alignment, particularly in trends post-2015. Variations in economic indicators affect the custom index.

Conclusion

The Diffusion Index developed using three economic indicators aligns moderately well with the CFNAIDIFF, highlighting its effectiveness. Adding more indicators or refining the smoothing process could further improve accuracy.