In this post, we create a Diffusion Index for the U.S. economy based on three key economic variables:
We compare the Diffusion Index with the Chicago Federal Reserve National Activity Index (CFNAIDIFF) to assess the current state of the U.S. economy and predict future trends.
We will start by loading necessary libraries and setting up our environment.
To calculate the Diffusion Index, we will download three relevant economic variables from FRED (Federal Reserve Economic Data):
AHE (Average Hourly Earnings of All Employees) CTNA (Total Nonfarm Employment) BUSAPP (Business Applications)
# Download data from FRED
getSymbols(c("BUSAPPWNSACT", "CTNA", "SMU09000000500000003"),
freq = "monthly",
src = "FRED", return.class = 'xts',
index.class = 'Date',
from = "2010-01-01",
to = Sys.Date(),
periodicity = "monthly")
## [1] "BUSAPPWNSACT" "CTNA" "SMU09000000500000003"
# Preview data
head(SMU09000000500000003, 3)
## SMU09000000500000003
## 2010-01-01 27.75
## 2010-02-01 28.22
## 2010-03-01 28.03
head(CTNA, 3)
## CTNA
## 2010-01-01 1601.0
## 2010-02-01 1601.8
## 2010-03-01 1603.7
head(BUSAPPWNSACT, 3)
## BUSAPPWNSACT
## 2010-01-02 320
## 2010-01-09 320
## 2010-01-16 590
Next, we will prepare the data by extracting monthly values for each variable and then computing their first differences (monthly changes).
# Extract monthly values
biz = to.monthly(BUSAPPWNSACT)[,4]
## Warning in to.period(x, "months", indexAt = indexAt, name = name, ...): missing
## values removed from data
ct_ctna = CTNA
us_emp = SMU09000000500000003
# Shorten data range for aesthetic purposes
biz_ss <- biz["2010-01-31/2024-09-01"] |> ts_ts()
us_ss <- us_emp["2010-01-31/2024-09-01"] |> ts_ts()
ctna_ss <- ct_ctna["2010-01-31/2024-09-01"] |> ts_ts()
# Combine data into a data frame
mydata = cbind.data.frame(biz_ss, us_ss, ctna_ss)
# Calculate first differences
mydf = mydata %>%
mutate(bizD1 = tsibble::difference(biz_ss, differences = 1),
usD1 = tsibble::difference(us_ss, differences = 1),
ctD1 = tsibble::difference(ctna_ss, differences = 1)
) %>% dplyr::select(c(bizD1, usD1, ctD1)) |> na.omit()
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
# View first differences
head(mydf, 3)
## bizD1 usD1 ctD1
## 2 80 -0.19 1.9
## 3 20 0.40 9.2
## 4 -30 0.00 8.9
Now we will calculate the Diffusion Index. We will convert each difference into either “up” (positive change), “down” (negative change), or “no change” (zero change). Then, we will calculate the Diffusion Index for each time period.
# Convert first differences to up/down/no change
mydf_mat = apply(mydf, 2, sign)
# Calculate positive and negative counts
pos = apply(mydf_mat, 1, function(row) sum(row > 0))
neg = apply(mydf_mat, 1, function(row) sum(row < 0))
# Total changes
tot = pos + neg
# Calculate Diffusion Index
index = (pos / tot - neg / tot) * 100
# Plot Diffusion Index
plot(index, type = "l", main = "Diffusion Index (U.S. Economy)",
ylab = "Index Value", xlab = "Time")
abline(h = 0, col = "darkred")
#Smoothing the Diffusion Index
To better visualize the trends, we will apply a 7-month moving average to the Diffusion Index.
ma_index = zoo::rollmean(index, 7, align = "right")
# Create Date sequence of 175 months starting from May 1, 2010
Date <- seq.Date(from = as.Date("2010-05-01"), length.out = 175, by = "month")
# Combine Date and index into a data frame
data <- data.frame(Date = Date, Index = index)
# Plot with smoothing
# Plot using ggplot2
ggplot(data, aes(x = Date, y = Index)) +
geom_line() +
geom_smooth(colour = "yellow") + # Smoothed trend line
labs(title = "Connecticut Economy",
x = "Months",
y = "Change") +
theme(axis.line.x = element_line(size = 0.75, colour = "red"),
axis.line.y = element_line(size = 0.75, colour = "red"),
legend.position = "bottom",
legend.direction = "horizontal") +
theme_tufte()
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
The Chicago Fed National Activity Index (CFNAIDIFF) is another important economic indicator. We will compare our Diffusion Index with the CFNAIDIFF to assess how they align.
# Download CFNAIDIFF data
getSymbols("CFNAIDIFF", src = "FRED", from = "2010-01-01", to = Sys.Date())
## [1] "CFNAIDIFF"
# Extract CFNAIDIFF and trim the date range
cfna_diff = CFNAIDIFF["2010-01-01/2024-09-01"] |> ts_ts()
# Trim the Date sequence to match the length of CFNAIDIFF
Date_trimmed <- seq.Date(from = as.Date("2010-01-01"), length.out = length(coredata(cfna_diff)), by = "month")
# Trim the Diffusion Index to match the length of CFNAIDIFF
index_trimmed <- index[1:length(coredata(cfna_diff))]
# Plot Diffusion Index vs CFNAIDIFF
df_comparison = data.frame(Date = Date_trimmed, Diffusion_Index = index_trimmed, CFNAIDIFF = coredata(cfna_diff))
ggplot(df_comparison, aes(x = Date)) +
geom_line(aes(y = Diffusion_Index, color = "Diffusion Index")) +
geom_line(aes(y = CFNAIDIFF, color = "CFNAIDIFF")) +
labs(title = "Diffusion Index vs CFNAIDIFF",
x = "Date",
y = "Index Value") +
scale_color_manual(values = c("Diffusion Index" = "#964B00", "CFNAIDIFF" = "red")) +
theme_minimal()
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).
# Calculate correlation
cor(index_trimmed, coredata(cfna_diff), use = "complete.obs")
## [1] 0.008485195
The Diffusion Index provides a measure of the broad trends in the U.S. economy. Comparing it with the Chicago Fed National Activity Index reveals similar patterns, allowing us to assess the current economic state. The correlation between the two indices helps us understand whether the Diffusion Index is a useful predictor for broader economic activity.
Based on the latest data, the U.S. economy appears to be interpreting the trends, stabilizing as indicated by the Diffusion Index and the CFNAIDIFF.
#References
Federal Reserve Economic Data (FRED) Chicago Federal Reserve National Activity Index (CFNAIDIFF)