Summary
The above visualization displays the relationship between the Dow Jones Industrial Average (DJIA), 10 Year Treasury Yield (GS10) and Federal Funds Rate between 09/2016 and 09/2025. The chart demonstrates that although all three indexes are somewhat correlated with each other, especially during periods of marked decreases such as during the Covid recession, the chart supports the notion that GS10 is a precursor of federal policy surrounding interest rate hikes and cuts. Between 2015-2019, the GS10 outperformed the DJIA and the Federal Funds Rate. However, beginning in 2019, the GS10 illustrated a marked decrease which was not reflected in interest rates cuts and DJIA decrease until approximately six months (Federal Funds Rate) to a year later (DJIA). Likewise, the GS10 demonstrated noticeable increases in 2021, about a year after the Covid recession, while while the Federal Funds Rate did not begin it’s similar upward trajectory until two years after the Covid recession. As such the GS10 in this context can be viewed as a precursor to Federal Funds Rate policy. On a side note, the DJIA appears to be a lagging indicator of economic health and driven by sentiment, as the DJIA did not react to the Covid recession until the recession was underway, and the DJIA immediately recovered after the Covid recession and trended upward during the entire ten year period, as opposed to the other two indices.

Part 1: Import data from FRED
Dow Jones Industrial Average Data (DJIA)
Data was imported from FRED via API. The DJIA data was limited to 10 years of data, with 2,609 rows of data between dates 2015-09-18 and 2025-09-17. There are two columns in the data set, ‘observation_date’ which is date of the observations and ‘DJIA’ which is where the DJIA closed on that particular day. Of note, DJIA is recorded on a semi-daily basis, meaning on days the market is open but excluding holidays.

10-Year Treasury Yield Data (GS10)
Twenty-five years of the GS10 data was obtainable via the API, but in a different format than the DJIA. The GS10 is reported monthly, as opposed to the DJIA which is reported daily. There were 300 rows and two columns in the GS10 dataset, ‘observation_date’ and the actual point measurement ‘GS10’. The dataset contained observations for dates between 2000-09-01 and 2025-08-01.

Federal Funds Rate
Twenty-five years of the Federal Funds Rate was obtainable via the API, and, again, in a different format than the DJIA. The Federal Funds Rate is reported monthly, again at odds with the DJIA data set which is reported daily. There are 300 rows in the Federal Funds Rate data set, with observations available between 2000-09-01 and 2025-08-01.

Part 2: Data Inspection
Data was inspected for NA/missing values. DJIA was determined to have NA/missing observation values for 95 observations, while GS10 and Federal Funds Rate data was determined to have no NA/missing values. NA/Missing values for the DJIA appeared to be approximately distributed evenly throughout the months and years. It was also determined that many (72) of the NA/missing values (95) were holidays. Since the NA/missing values were mostly holidays and the remaining 23 NA/missing values were approximately distributed evenly through the data, a decision was made to drop the NA/missing values from the dataset. After dropping the rows with the missing observations, the DJIA data set contained 2514 rows.

Part 3: Wrangling
Since the DJIA dates are reported daily and the GS10 and Federal Funds Rate are reported monthly, the DJIA dates were made a monthly average to allow the dates between the three datasets to be comparable/standardized. The three datasets were then joined into a single dataframe. Additionally, since the DJIA, GS10 and Federal Funds Rate are measured on different scales, meaning one is a percentage and the other two are point based measurements, each series was standardized using max-min scaling in order for the scores to be compared on the same chart.

Part 4: Plotting and Write Up
Finally, the datasets were plotted. After much experimentation a final chart was decided on. Please see below.

The above visualization displays the relationship between the Dow Jones Industrial Average (DJIA), 10 Year Treasury Yield (GS10) and Federal Funds Rate between 09/2016 and 09/2025. The chart demonstrates that although all three indexes are somewhat correlated with each other, especially during periods of marked decreases such as during the Covid recession, the chart supports the notion that GS10 is a precursor of federal policy surrounding interest rate hikes and cuts. Between 2015-2019, the GS10 outperformed the DJIA and the Federal Funds Rate. However, beginning in 2019, the GS10 illustrated a marked decrease which was not reflected in interest rates cuts and DJIA decrease until approximately six months (Federal Funds Rate) to a year later (DJIA). Likewise, the GS10 demonstrated noticeable increases in 2021, about a year after the Covid recession, while while the Federal Funds Rate did not begin it’s similar upward trajectory until two years after the Covid recession. As such the GS10 in this context can be viewed as a precursor to Federal Funds Rate policy. On a side note, the DJIA appears to be a lagging indicator of economic health and driven by sentiment, as the DJIA did not react to the Covid recession until the recession was underway, and the DJIA immediately recovered after the Covid recession and trended upward during the entire ten year period, as opposed to the other two indices.

# read CSV from Fred
read_fred_csv <- function(url) {
  con <- url(url, open = "r")  
  on.exit(try(close(con), silent = TRUE))
  dat <- read.csv(con, stringsAsFactors = FALSE)
  dat
}

# Last 25 years 
today      <- Sys.Date()
start_month<- as.Date(format(today, "%Y-%m-01"))
start_25y  <- as.Date(format(seq(start_month, length = 2, by = "-300 months")[2], "%Y-%m-01")) 
obs_start  <- format(start_25y, "%Y-%m-%d")
obs_end    <- format(today,     "%Y-%m-%d")

# DJIA
djia_url <- paste0(
  "https://fred.stlouisfed.org/graph/fredgraph.csv?id=DJIA",
  "&cosd=", obs_start, "&coed=", obs_end
)
djia_raw <- read_fred_csv(djia_url)

# Fix date column
djia_raw$DATE <- as.Date(djia_raw$observation_date)
names(djia_raw)[2] <- "DJIA"

# Show first and last dates
cat("First available date:", min(djia_raw$DATE, na.rm = TRUE), "\n")
## First available date: 16839
cat("Last available date :", max(djia_raw$DATE, na.rm = TRUE), "\n")
## Last available date : 20490
# set date to date type
djia_raw$DATE <- as.Date(djia_raw$observation_date)


str(djia_raw$DATE)
##  Date[1:2610], format: "2016-02-08" "2016-02-09" "2016-02-10" "2016-02-11" "2016-02-12" ...
# format first and last dates
first_date <- min(djia_raw$DATE, na.rm = TRUE)
last_date  <- max(djia_raw$DATE, na.rm = TRUE)

cat("First available date:", format(first_date, "%Y-%m-%d"), "\n")
## First available date: 2016-02-08
cat("Last available date :", format(last_date, "%Y-%m-%d"), "\n")
## Last available date : 2026-02-06
head(djia_raw)
##   observation_date     DJIA       DATE
## 1       2016-02-08 16027.05 2016-02-08
## 2       2016-02-09 16014.38 2016-02-09
## 3       2016-02-10 15914.74 2016-02-10
## 4       2016-02-11 15660.18 2016-02-11
## 5       2016-02-12 15973.84 2016-02-12
## 6       2016-02-15       NA 2016-02-15
# 10-Year Treasury Yield 
gs10_url <- paste0(
  "https://fred.stlouisfed.org/graph/fredgraph.csv?id=GS10",
  "&cosd=", obs_start, "&coed=", obs_end
)
gs10_raw <- read_fred_csv(gs10_url)


gs10_raw$DATE <- as.Date(gs10_raw$observation_date)
names(gs10_raw)[2] <- "GS10"
gs10_raw
##     observation_date GS10       DATE
## 1         2001-02-01 5.10 2001-02-01
## 2         2001-03-01 4.89 2001-03-01
## 3         2001-04-01 5.14 2001-04-01
## 4         2001-05-01 5.39 2001-05-01
## 5         2001-06-01 5.28 2001-06-01
## 6         2001-07-01 5.24 2001-07-01
## 7         2001-08-01 4.97 2001-08-01
## 8         2001-09-01 4.73 2001-09-01
## 9         2001-10-01 4.57 2001-10-01
## 10        2001-11-01 4.65 2001-11-01
## 11        2001-12-01 5.09 2001-12-01
## 12        2002-01-01 5.04 2002-01-01
## 13        2002-02-01 4.91 2002-02-01
## 14        2002-03-01 5.28 2002-03-01
## 15        2002-04-01 5.21 2002-04-01
## 16        2002-05-01 5.16 2002-05-01
## 17        2002-06-01 4.93 2002-06-01
## 18        2002-07-01 4.65 2002-07-01
## 19        2002-08-01 4.26 2002-08-01
## 20        2002-09-01 3.87 2002-09-01
## 21        2002-10-01 3.94 2002-10-01
## 22        2002-11-01 4.05 2002-11-01
## 23        2002-12-01 4.03 2002-12-01
## 24        2003-01-01 4.05 2003-01-01
## 25        2003-02-01 3.90 2003-02-01
## 26        2003-03-01 3.81 2003-03-01
## 27        2003-04-01 3.96 2003-04-01
## 28        2003-05-01 3.57 2003-05-01
## 29        2003-06-01 3.33 2003-06-01
## 30        2003-07-01 3.98 2003-07-01
## 31        2003-08-01 4.45 2003-08-01
## 32        2003-09-01 4.27 2003-09-01
## 33        2003-10-01 4.29 2003-10-01
## 34        2003-11-01 4.30 2003-11-01
## 35        2003-12-01 4.27 2003-12-01
## 36        2004-01-01 4.15 2004-01-01
## 37        2004-02-01 4.08 2004-02-01
## 38        2004-03-01 3.83 2004-03-01
## 39        2004-04-01 4.35 2004-04-01
## 40        2004-05-01 4.72 2004-05-01
## 41        2004-06-01 4.73 2004-06-01
## 42        2004-07-01 4.50 2004-07-01
## 43        2004-08-01 4.28 2004-08-01
## 44        2004-09-01 4.13 2004-09-01
## 45        2004-10-01 4.10 2004-10-01
## 46        2004-11-01 4.19 2004-11-01
## 47        2004-12-01 4.23 2004-12-01
## 48        2005-01-01 4.22 2005-01-01
## 49        2005-02-01 4.17 2005-02-01
## 50        2005-03-01 4.50 2005-03-01
## 51        2005-04-01 4.34 2005-04-01
## 52        2005-05-01 4.14 2005-05-01
## 53        2005-06-01 4.00 2005-06-01
## 54        2005-07-01 4.18 2005-07-01
## 55        2005-08-01 4.26 2005-08-01
## 56        2005-09-01 4.20 2005-09-01
## 57        2005-10-01 4.46 2005-10-01
## 58        2005-11-01 4.54 2005-11-01
## 59        2005-12-01 4.47 2005-12-01
## 60        2006-01-01 4.42 2006-01-01
## 61        2006-02-01 4.57 2006-02-01
## 62        2006-03-01 4.72 2006-03-01
## 63        2006-04-01 4.99 2006-04-01
## 64        2006-05-01 5.11 2006-05-01
## 65        2006-06-01 5.11 2006-06-01
## 66        2006-07-01 5.09 2006-07-01
## 67        2006-08-01 4.88 2006-08-01
## 68        2006-09-01 4.72 2006-09-01
## 69        2006-10-01 4.73 2006-10-01
## 70        2006-11-01 4.60 2006-11-01
## 71        2006-12-01 4.56 2006-12-01
## 72        2007-01-01 4.76 2007-01-01
## 73        2007-02-01 4.72 2007-02-01
## 74        2007-03-01 4.56 2007-03-01
## 75        2007-04-01 4.69 2007-04-01
## 76        2007-05-01 4.75 2007-05-01
## 77        2007-06-01 5.10 2007-06-01
## 78        2007-07-01 5.00 2007-07-01
## 79        2007-08-01 4.67 2007-08-01
## 80        2007-09-01 4.52 2007-09-01
## 81        2007-10-01 4.53 2007-10-01
## 82        2007-11-01 4.15 2007-11-01
## 83        2007-12-01 4.10 2007-12-01
## 84        2008-01-01 3.74 2008-01-01
## 85        2008-02-01 3.74 2008-02-01
## 86        2008-03-01 3.51 2008-03-01
## 87        2008-04-01 3.68 2008-04-01
## 88        2008-05-01 3.88 2008-05-01
## 89        2008-06-01 4.10 2008-06-01
## 90        2008-07-01 4.01 2008-07-01
## 91        2008-08-01 3.89 2008-08-01
## 92        2008-09-01 3.69 2008-09-01
## 93        2008-10-01 3.81 2008-10-01
## 94        2008-11-01 3.53 2008-11-01
## 95        2008-12-01 2.42 2008-12-01
## 96        2009-01-01 2.52 2009-01-01
## 97        2009-02-01 2.87 2009-02-01
## 98        2009-03-01 2.82 2009-03-01
## 99        2009-04-01 2.93 2009-04-01
## 100       2009-05-01 3.29 2009-05-01
## 101       2009-06-01 3.72 2009-06-01
## 102       2009-07-01 3.56 2009-07-01
## 103       2009-08-01 3.59 2009-08-01
## 104       2009-09-01 3.40 2009-09-01
## 105       2009-10-01 3.39 2009-10-01
## 106       2009-11-01 3.40 2009-11-01
## 107       2009-12-01 3.59 2009-12-01
## 108       2010-01-01 3.73 2010-01-01
## 109       2010-02-01 3.69 2010-02-01
## 110       2010-03-01 3.73 2010-03-01
## 111       2010-04-01 3.85 2010-04-01
## 112       2010-05-01 3.42 2010-05-01
## 113       2010-06-01 3.20 2010-06-01
## 114       2010-07-01 3.01 2010-07-01
## 115       2010-08-01 2.70 2010-08-01
## 116       2010-09-01 2.65 2010-09-01
## 117       2010-10-01 2.54 2010-10-01
## 118       2010-11-01 2.76 2010-11-01
## 119       2010-12-01 3.29 2010-12-01
## 120       2011-01-01 3.39 2011-01-01
## 121       2011-02-01 3.58 2011-02-01
## 122       2011-03-01 3.41 2011-03-01
## 123       2011-04-01 3.46 2011-04-01
## 124       2011-05-01 3.17 2011-05-01
## 125       2011-06-01 3.00 2011-06-01
## 126       2011-07-01 3.00 2011-07-01
## 127       2011-08-01 2.30 2011-08-01
## 128       2011-09-01 1.98 2011-09-01
## 129       2011-10-01 2.15 2011-10-01
## 130       2011-11-01 2.01 2011-11-01
## 131       2011-12-01 1.98 2011-12-01
## 132       2012-01-01 1.97 2012-01-01
## 133       2012-02-01 1.97 2012-02-01
## 134       2012-03-01 2.17 2012-03-01
## 135       2012-04-01 2.05 2012-04-01
## 136       2012-05-01 1.80 2012-05-01
## 137       2012-06-01 1.62 2012-06-01
## 138       2012-07-01 1.53 2012-07-01
## 139       2012-08-01 1.68 2012-08-01
## 140       2012-09-01 1.72 2012-09-01
## 141       2012-10-01 1.75 2012-10-01
## 142       2012-11-01 1.65 2012-11-01
## 143       2012-12-01 1.72 2012-12-01
## 144       2013-01-01 1.91 2013-01-01
## 145       2013-02-01 1.98 2013-02-01
## 146       2013-03-01 1.96 2013-03-01
## 147       2013-04-01 1.76 2013-04-01
## 148       2013-05-01 1.93 2013-05-01
## 149       2013-06-01 2.30 2013-06-01
## 150       2013-07-01 2.58 2013-07-01
## 151       2013-08-01 2.74 2013-08-01
## 152       2013-09-01 2.81 2013-09-01
## 153       2013-10-01 2.62 2013-10-01
## 154       2013-11-01 2.72 2013-11-01
## 155       2013-12-01 2.90 2013-12-01
## 156       2014-01-01 2.86 2014-01-01
## 157       2014-02-01 2.71 2014-02-01
## 158       2014-03-01 2.72 2014-03-01
## 159       2014-04-01 2.71 2014-04-01
## 160       2014-05-01 2.56 2014-05-01
## 161       2014-06-01 2.60 2014-06-01
## 162       2014-07-01 2.54 2014-07-01
## 163       2014-08-01 2.42 2014-08-01
## 164       2014-09-01 2.53 2014-09-01
## 165       2014-10-01 2.30 2014-10-01
## 166       2014-11-01 2.33 2014-11-01
## 167       2014-12-01 2.21 2014-12-01
## 168       2015-01-01 1.88 2015-01-01
## 169       2015-02-01 1.98 2015-02-01
## 170       2015-03-01 2.04 2015-03-01
## 171       2015-04-01 1.94 2015-04-01
## 172       2015-05-01 2.20 2015-05-01
## 173       2015-06-01 2.36 2015-06-01
## 174       2015-07-01 2.32 2015-07-01
## 175       2015-08-01 2.17 2015-08-01
## 176       2015-09-01 2.17 2015-09-01
## 177       2015-10-01 2.07 2015-10-01
## 178       2015-11-01 2.26 2015-11-01
## 179       2015-12-01 2.24 2015-12-01
## 180       2016-01-01 2.09 2016-01-01
## 181       2016-02-01 1.78 2016-02-01
## 182       2016-03-01 1.89 2016-03-01
## 183       2016-04-01 1.81 2016-04-01
## 184       2016-05-01 1.81 2016-05-01
## 185       2016-06-01 1.64 2016-06-01
## 186       2016-07-01 1.50 2016-07-01
## 187       2016-08-01 1.56 2016-08-01
## 188       2016-09-01 1.63 2016-09-01
## 189       2016-10-01 1.76 2016-10-01
## 190       2016-11-01 2.14 2016-11-01
## 191       2016-12-01 2.49 2016-12-01
## 192       2017-01-01 2.43 2017-01-01
## 193       2017-02-01 2.42 2017-02-01
## 194       2017-03-01 2.48 2017-03-01
## 195       2017-04-01 2.30 2017-04-01
## 196       2017-05-01 2.30 2017-05-01
## 197       2017-06-01 2.19 2017-06-01
## 198       2017-07-01 2.32 2017-07-01
## 199       2017-08-01 2.21 2017-08-01
## 200       2017-09-01 2.20 2017-09-01
## 201       2017-10-01 2.36 2017-10-01
## 202       2017-11-01 2.35 2017-11-01
## 203       2017-12-01 2.40 2017-12-01
## 204       2018-01-01 2.58 2018-01-01
## 205       2018-02-01 2.86 2018-02-01
## 206       2018-03-01 2.84 2018-03-01
## 207       2018-04-01 2.87 2018-04-01
## 208       2018-05-01 2.98 2018-05-01
## 209       2018-06-01 2.91 2018-06-01
## 210       2018-07-01 2.89 2018-07-01
## 211       2018-08-01 2.89 2018-08-01
## 212       2018-09-01 3.00 2018-09-01
## 213       2018-10-01 3.15 2018-10-01
## 214       2018-11-01 3.12 2018-11-01
## 215       2018-12-01 2.83 2018-12-01
## 216       2019-01-01 2.71 2019-01-01
## 217       2019-02-01 2.68 2019-02-01
## 218       2019-03-01 2.57 2019-03-01
## 219       2019-04-01 2.53 2019-04-01
## 220       2019-05-01 2.40 2019-05-01
## 221       2019-06-01 2.07 2019-06-01
## 222       2019-07-01 2.06 2019-07-01
## 223       2019-08-01 1.63 2019-08-01
## 224       2019-09-01 1.70 2019-09-01
## 225       2019-10-01 1.71 2019-10-01
## 226       2019-11-01 1.81 2019-11-01
## 227       2019-12-01 1.86 2019-12-01
## 228       2020-01-01 1.76 2020-01-01
## 229       2020-02-01 1.50 2020-02-01
## 230       2020-03-01 0.87 2020-03-01
## 231       2020-04-01 0.66 2020-04-01
## 232       2020-05-01 0.67 2020-05-01
## 233       2020-06-01 0.73 2020-06-01
## 234       2020-07-01 0.62 2020-07-01
## 235       2020-08-01 0.65 2020-08-01
## 236       2020-09-01 0.68 2020-09-01
## 237       2020-10-01 0.79 2020-10-01
## 238       2020-11-01 0.87 2020-11-01
## 239       2020-12-01 0.93 2020-12-01
## 240       2021-01-01 1.08 2021-01-01
## 241       2021-02-01 1.26 2021-02-01
## 242       2021-03-01 1.61 2021-03-01
## 243       2021-04-01 1.64 2021-04-01
## 244       2021-05-01 1.62 2021-05-01
## 245       2021-06-01 1.52 2021-06-01
## 246       2021-07-01 1.32 2021-07-01
## 247       2021-08-01 1.28 2021-08-01
## 248       2021-09-01 1.37 2021-09-01
## 249       2021-10-01 1.58 2021-10-01
## 250       2021-11-01 1.56 2021-11-01
## 251       2021-12-01 1.47 2021-12-01
## 252       2022-01-01 1.76 2022-01-01
## 253       2022-02-01 1.93 2022-02-01
## 254       2022-03-01 2.13 2022-03-01
## 255       2022-04-01 2.75 2022-04-01
## 256       2022-05-01 2.90 2022-05-01
## 257       2022-06-01 3.14 2022-06-01
## 258       2022-07-01 2.90 2022-07-01
## 259       2022-08-01 2.90 2022-08-01
## 260       2022-09-01 3.52 2022-09-01
## 261       2022-10-01 3.98 2022-10-01
## 262       2022-11-01 3.89 2022-11-01
## 263       2022-12-01 3.62 2022-12-01
## 264       2023-01-01 3.53 2023-01-01
## 265       2023-02-01 3.75 2023-02-01
## 266       2023-03-01 3.66 2023-03-01
## 267       2023-04-01 3.46 2023-04-01
## 268       2023-05-01 3.57 2023-05-01
## 269       2023-06-01 3.75 2023-06-01
## 270       2023-07-01 3.90 2023-07-01
## 271       2023-08-01 4.17 2023-08-01
## 272       2023-09-01 4.38 2023-09-01
## 273       2023-10-01 4.80 2023-10-01
## 274       2023-11-01 4.50 2023-11-01
## 275       2023-12-01 4.02 2023-12-01
## 276       2024-01-01 4.06 2024-01-01
## 277       2024-02-01 4.21 2024-02-01
## 278       2024-03-01 4.21 2024-03-01
## 279       2024-04-01 4.54 2024-04-01
## 280       2024-05-01 4.48 2024-05-01
## 281       2024-06-01 4.31 2024-06-01
## 282       2024-07-01 4.25 2024-07-01
## 283       2024-08-01 3.87 2024-08-01
## 284       2024-09-01 3.72 2024-09-01
## 285       2024-10-01 4.10 2024-10-01
## 286       2024-11-01 4.36 2024-11-01
## 287       2024-12-01 4.39 2024-12-01
## 288       2025-01-01 4.63 2025-01-01
## 289       2025-02-01 4.45 2025-02-01
## 290       2025-03-01 4.28 2025-03-01
## 291       2025-04-01 4.28 2025-04-01
## 292       2025-05-01 4.42 2025-05-01
## 293       2025-06-01 4.38 2025-06-01
## 294       2025-07-01 4.39 2025-07-01
## 295       2025-08-01 4.26 2025-08-01
## 296       2025-09-01 4.12 2025-09-01
## 297       2025-10-01 4.06 2025-10-01
## 298       2025-11-01 4.09 2025-11-01
## 299       2025-12-01 4.14 2025-12-01
## 300       2026-01-01 4.21 2026-01-01
# --- Show available date range for GS10 ---
first_gs10 <- min(gs10_raw$DATE, na.rm = TRUE)
last_gs10  <- max(gs10_raw$DATE, na.rm = TRUE)

cat("GS10 first available date:", format(first_gs10, "%Y-%m-%d"), "\n")
## GS10 first available date: 2001-02-01
cat("GS10 last available date :", format(last_gs10, "%Y-%m-%d"), "\n")
## GS10 last available date : 2026-01-01
gs10_monthly <- data.frame(DATE = gs10_raw$DATE, GS10 = gs10_raw$GS10)
#  Federal Funds Rate 
effr_url <- paste0(
  "https://fred.stlouisfed.org/graph/fredgraph.csv?id=FEDFUNDS",
  "&cosd=", obs_start, "&coed=", obs_end
)
effr_raw <- read_fred_csv(effr_url)
effr_raw$DATE <- as.Date(effr_raw$observation_date)
names(effr_raw)[2] <- "FEDFUNDS"
effr_raw 
##     observation_date FEDFUNDS       DATE
## 1         2001-02-01     5.49 2001-02-01
## 2         2001-03-01     5.31 2001-03-01
## 3         2001-04-01     4.80 2001-04-01
## 4         2001-05-01     4.21 2001-05-01
## 5         2001-06-01     3.97 2001-06-01
## 6         2001-07-01     3.77 2001-07-01
## 7         2001-08-01     3.65 2001-08-01
## 8         2001-09-01     3.07 2001-09-01
## 9         2001-10-01     2.49 2001-10-01
## 10        2001-11-01     2.09 2001-11-01
## 11        2001-12-01     1.82 2001-12-01
## 12        2002-01-01     1.73 2002-01-01
## 13        2002-02-01     1.74 2002-02-01
## 14        2002-03-01     1.73 2002-03-01
## 15        2002-04-01     1.75 2002-04-01
## 16        2002-05-01     1.75 2002-05-01
## 17        2002-06-01     1.75 2002-06-01
## 18        2002-07-01     1.73 2002-07-01
## 19        2002-08-01     1.74 2002-08-01
## 20        2002-09-01     1.75 2002-09-01
## 21        2002-10-01     1.75 2002-10-01
## 22        2002-11-01     1.34 2002-11-01
## 23        2002-12-01     1.24 2002-12-01
## 24        2003-01-01     1.24 2003-01-01
## 25        2003-02-01     1.26 2003-02-01
## 26        2003-03-01     1.25 2003-03-01
## 27        2003-04-01     1.26 2003-04-01
## 28        2003-05-01     1.26 2003-05-01
## 29        2003-06-01     1.22 2003-06-01
## 30        2003-07-01     1.01 2003-07-01
## 31        2003-08-01     1.03 2003-08-01
## 32        2003-09-01     1.01 2003-09-01
## 33        2003-10-01     1.01 2003-10-01
## 34        2003-11-01     1.00 2003-11-01
## 35        2003-12-01     0.98 2003-12-01
## 36        2004-01-01     1.00 2004-01-01
## 37        2004-02-01     1.01 2004-02-01
## 38        2004-03-01     1.00 2004-03-01
## 39        2004-04-01     1.00 2004-04-01
## 40        2004-05-01     1.00 2004-05-01
## 41        2004-06-01     1.03 2004-06-01
## 42        2004-07-01     1.26 2004-07-01
## 43        2004-08-01     1.43 2004-08-01
## 44        2004-09-01     1.61 2004-09-01
## 45        2004-10-01     1.76 2004-10-01
## 46        2004-11-01     1.93 2004-11-01
## 47        2004-12-01     2.16 2004-12-01
## 48        2005-01-01     2.28 2005-01-01
## 49        2005-02-01     2.50 2005-02-01
## 50        2005-03-01     2.63 2005-03-01
## 51        2005-04-01     2.79 2005-04-01
## 52        2005-05-01     3.00 2005-05-01
## 53        2005-06-01     3.04 2005-06-01
## 54        2005-07-01     3.26 2005-07-01
## 55        2005-08-01     3.50 2005-08-01
## 56        2005-09-01     3.62 2005-09-01
## 57        2005-10-01     3.78 2005-10-01
## 58        2005-11-01     4.00 2005-11-01
## 59        2005-12-01     4.16 2005-12-01
## 60        2006-01-01     4.29 2006-01-01
## 61        2006-02-01     4.49 2006-02-01
## 62        2006-03-01     4.59 2006-03-01
## 63        2006-04-01     4.79 2006-04-01
## 64        2006-05-01     4.94 2006-05-01
## 65        2006-06-01     4.99 2006-06-01
## 66        2006-07-01     5.24 2006-07-01
## 67        2006-08-01     5.25 2006-08-01
## 68        2006-09-01     5.25 2006-09-01
## 69        2006-10-01     5.25 2006-10-01
## 70        2006-11-01     5.25 2006-11-01
## 71        2006-12-01     5.24 2006-12-01
## 72        2007-01-01     5.25 2007-01-01
## 73        2007-02-01     5.26 2007-02-01
## 74        2007-03-01     5.26 2007-03-01
## 75        2007-04-01     5.25 2007-04-01
## 76        2007-05-01     5.25 2007-05-01
## 77        2007-06-01     5.25 2007-06-01
## 78        2007-07-01     5.26 2007-07-01
## 79        2007-08-01     5.02 2007-08-01
## 80        2007-09-01     4.94 2007-09-01
## 81        2007-10-01     4.76 2007-10-01
## 82        2007-11-01     4.49 2007-11-01
## 83        2007-12-01     4.24 2007-12-01
## 84        2008-01-01     3.94 2008-01-01
## 85        2008-02-01     2.98 2008-02-01
## 86        2008-03-01     2.61 2008-03-01
## 87        2008-04-01     2.28 2008-04-01
## 88        2008-05-01     1.98 2008-05-01
## 89        2008-06-01     2.00 2008-06-01
## 90        2008-07-01     2.01 2008-07-01
## 91        2008-08-01     2.00 2008-08-01
## 92        2008-09-01     1.81 2008-09-01
## 93        2008-10-01     0.97 2008-10-01
## 94        2008-11-01     0.39 2008-11-01
## 95        2008-12-01     0.16 2008-12-01
## 96        2009-01-01     0.15 2009-01-01
## 97        2009-02-01     0.22 2009-02-01
## 98        2009-03-01     0.18 2009-03-01
## 99        2009-04-01     0.15 2009-04-01
## 100       2009-05-01     0.18 2009-05-01
## 101       2009-06-01     0.21 2009-06-01
## 102       2009-07-01     0.16 2009-07-01
## 103       2009-08-01     0.16 2009-08-01
## 104       2009-09-01     0.15 2009-09-01
## 105       2009-10-01     0.12 2009-10-01
## 106       2009-11-01     0.12 2009-11-01
## 107       2009-12-01     0.12 2009-12-01
## 108       2010-01-01     0.11 2010-01-01
## 109       2010-02-01     0.13 2010-02-01
## 110       2010-03-01     0.16 2010-03-01
## 111       2010-04-01     0.20 2010-04-01
## 112       2010-05-01     0.20 2010-05-01
## 113       2010-06-01     0.18 2010-06-01
## 114       2010-07-01     0.18 2010-07-01
## 115       2010-08-01     0.19 2010-08-01
## 116       2010-09-01     0.19 2010-09-01
## 117       2010-10-01     0.19 2010-10-01
## 118       2010-11-01     0.19 2010-11-01
## 119       2010-12-01     0.18 2010-12-01
## 120       2011-01-01     0.17 2011-01-01
## 121       2011-02-01     0.16 2011-02-01
## 122       2011-03-01     0.14 2011-03-01
## 123       2011-04-01     0.10 2011-04-01
## 124       2011-05-01     0.09 2011-05-01
## 125       2011-06-01     0.09 2011-06-01
## 126       2011-07-01     0.07 2011-07-01
## 127       2011-08-01     0.10 2011-08-01
## 128       2011-09-01     0.08 2011-09-01
## 129       2011-10-01     0.07 2011-10-01
## 130       2011-11-01     0.08 2011-11-01
## 131       2011-12-01     0.07 2011-12-01
## 132       2012-01-01     0.08 2012-01-01
## 133       2012-02-01     0.10 2012-02-01
## 134       2012-03-01     0.13 2012-03-01
## 135       2012-04-01     0.14 2012-04-01
## 136       2012-05-01     0.16 2012-05-01
## 137       2012-06-01     0.16 2012-06-01
## 138       2012-07-01     0.16 2012-07-01
## 139       2012-08-01     0.13 2012-08-01
## 140       2012-09-01     0.14 2012-09-01
## 141       2012-10-01     0.16 2012-10-01
## 142       2012-11-01     0.16 2012-11-01
## 143       2012-12-01     0.16 2012-12-01
## 144       2013-01-01     0.14 2013-01-01
## 145       2013-02-01     0.15 2013-02-01
## 146       2013-03-01     0.14 2013-03-01
## 147       2013-04-01     0.15 2013-04-01
## 148       2013-05-01     0.11 2013-05-01
## 149       2013-06-01     0.09 2013-06-01
## 150       2013-07-01     0.09 2013-07-01
## 151       2013-08-01     0.08 2013-08-01
## 152       2013-09-01     0.08 2013-09-01
## 153       2013-10-01     0.09 2013-10-01
## 154       2013-11-01     0.08 2013-11-01
## 155       2013-12-01     0.09 2013-12-01
## 156       2014-01-01     0.07 2014-01-01
## 157       2014-02-01     0.07 2014-02-01
## 158       2014-03-01     0.08 2014-03-01
## 159       2014-04-01     0.09 2014-04-01
## 160       2014-05-01     0.09 2014-05-01
## 161       2014-06-01     0.10 2014-06-01
## 162       2014-07-01     0.09 2014-07-01
## 163       2014-08-01     0.09 2014-08-01
## 164       2014-09-01     0.09 2014-09-01
## 165       2014-10-01     0.09 2014-10-01
## 166       2014-11-01     0.09 2014-11-01
## 167       2014-12-01     0.12 2014-12-01
## 168       2015-01-01     0.11 2015-01-01
## 169       2015-02-01     0.11 2015-02-01
## 170       2015-03-01     0.11 2015-03-01
## 171       2015-04-01     0.12 2015-04-01
## 172       2015-05-01     0.12 2015-05-01
## 173       2015-06-01     0.13 2015-06-01
## 174       2015-07-01     0.13 2015-07-01
## 175       2015-08-01     0.14 2015-08-01
## 176       2015-09-01     0.14 2015-09-01
## 177       2015-10-01     0.12 2015-10-01
## 178       2015-11-01     0.12 2015-11-01
## 179       2015-12-01     0.24 2015-12-01
## 180       2016-01-01     0.34 2016-01-01
## 181       2016-02-01     0.38 2016-02-01
## 182       2016-03-01     0.36 2016-03-01
## 183       2016-04-01     0.37 2016-04-01
## 184       2016-05-01     0.37 2016-05-01
## 185       2016-06-01     0.38 2016-06-01
## 186       2016-07-01     0.39 2016-07-01
## 187       2016-08-01     0.40 2016-08-01
## 188       2016-09-01     0.40 2016-09-01
## 189       2016-10-01     0.40 2016-10-01
## 190       2016-11-01     0.41 2016-11-01
## 191       2016-12-01     0.54 2016-12-01
## 192       2017-01-01     0.65 2017-01-01
## 193       2017-02-01     0.66 2017-02-01
## 194       2017-03-01     0.79 2017-03-01
## 195       2017-04-01     0.90 2017-04-01
## 196       2017-05-01     0.91 2017-05-01
## 197       2017-06-01     1.04 2017-06-01
## 198       2017-07-01     1.15 2017-07-01
## 199       2017-08-01     1.16 2017-08-01
## 200       2017-09-01     1.15 2017-09-01
## 201       2017-10-01     1.15 2017-10-01
## 202       2017-11-01     1.16 2017-11-01
## 203       2017-12-01     1.30 2017-12-01
## 204       2018-01-01     1.41 2018-01-01
## 205       2018-02-01     1.42 2018-02-01
## 206       2018-03-01     1.51 2018-03-01
## 207       2018-04-01     1.69 2018-04-01
## 208       2018-05-01     1.70 2018-05-01
## 209       2018-06-01     1.82 2018-06-01
## 210       2018-07-01     1.91 2018-07-01
## 211       2018-08-01     1.91 2018-08-01
## 212       2018-09-01     1.95 2018-09-01
## 213       2018-10-01     2.19 2018-10-01
## 214       2018-11-01     2.20 2018-11-01
## 215       2018-12-01     2.27 2018-12-01
## 216       2019-01-01     2.40 2019-01-01
## 217       2019-02-01     2.40 2019-02-01
## 218       2019-03-01     2.41 2019-03-01
## 219       2019-04-01     2.42 2019-04-01
## 220       2019-05-01     2.39 2019-05-01
## 221       2019-06-01     2.38 2019-06-01
## 222       2019-07-01     2.40 2019-07-01
## 223       2019-08-01     2.13 2019-08-01
## 224       2019-09-01     2.04 2019-09-01
## 225       2019-10-01     1.83 2019-10-01
## 226       2019-11-01     1.55 2019-11-01
## 227       2019-12-01     1.55 2019-12-01
## 228       2020-01-01     1.55 2020-01-01
## 229       2020-02-01     1.58 2020-02-01
## 230       2020-03-01     0.65 2020-03-01
## 231       2020-04-01     0.05 2020-04-01
## 232       2020-05-01     0.05 2020-05-01
## 233       2020-06-01     0.08 2020-06-01
## 234       2020-07-01     0.09 2020-07-01
## 235       2020-08-01     0.10 2020-08-01
## 236       2020-09-01     0.09 2020-09-01
## 237       2020-10-01     0.09 2020-10-01
## 238       2020-11-01     0.09 2020-11-01
## 239       2020-12-01     0.09 2020-12-01
## 240       2021-01-01     0.09 2021-01-01
## 241       2021-02-01     0.08 2021-02-01
## 242       2021-03-01     0.07 2021-03-01
## 243       2021-04-01     0.07 2021-04-01
## 244       2021-05-01     0.06 2021-05-01
## 245       2021-06-01     0.08 2021-06-01
## 246       2021-07-01     0.10 2021-07-01
## 247       2021-08-01     0.09 2021-08-01
## 248       2021-09-01     0.08 2021-09-01
## 249       2021-10-01     0.08 2021-10-01
## 250       2021-11-01     0.08 2021-11-01
## 251       2021-12-01     0.08 2021-12-01
## 252       2022-01-01     0.08 2022-01-01
## 253       2022-02-01     0.08 2022-02-01
## 254       2022-03-01     0.20 2022-03-01
## 255       2022-04-01     0.33 2022-04-01
## 256       2022-05-01     0.77 2022-05-01
## 257       2022-06-01     1.21 2022-06-01
## 258       2022-07-01     1.68 2022-07-01
## 259       2022-08-01     2.33 2022-08-01
## 260       2022-09-01     2.56 2022-09-01
## 261       2022-10-01     3.08 2022-10-01
## 262       2022-11-01     3.78 2022-11-01
## 263       2022-12-01     4.10 2022-12-01
## 264       2023-01-01     4.33 2023-01-01
## 265       2023-02-01     4.57 2023-02-01
## 266       2023-03-01     4.65 2023-03-01
## 267       2023-04-01     4.83 2023-04-01
## 268       2023-05-01     5.06 2023-05-01
## 269       2023-06-01     5.08 2023-06-01
## 270       2023-07-01     5.12 2023-07-01
## 271       2023-08-01     5.33 2023-08-01
## 272       2023-09-01     5.33 2023-09-01
## 273       2023-10-01     5.33 2023-10-01
## 274       2023-11-01     5.33 2023-11-01
## 275       2023-12-01     5.33 2023-12-01
## 276       2024-01-01     5.33 2024-01-01
## 277       2024-02-01     5.33 2024-02-01
## 278       2024-03-01     5.33 2024-03-01
## 279       2024-04-01     5.33 2024-04-01
## 280       2024-05-01     5.33 2024-05-01
## 281       2024-06-01     5.33 2024-06-01
## 282       2024-07-01     5.33 2024-07-01
## 283       2024-08-01     5.33 2024-08-01
## 284       2024-09-01     5.13 2024-09-01
## 285       2024-10-01     4.83 2024-10-01
## 286       2024-11-01     4.64 2024-11-01
## 287       2024-12-01     4.48 2024-12-01
## 288       2025-01-01     4.33 2025-01-01
## 289       2025-02-01     4.33 2025-02-01
## 290       2025-03-01     4.33 2025-03-01
## 291       2025-04-01     4.33 2025-04-01
## 292       2025-05-01     4.33 2025-05-01
## 293       2025-06-01     4.33 2025-06-01
## 294       2025-07-01     4.33 2025-07-01
## 295       2025-08-01     4.33 2025-08-01
## 296       2025-09-01     4.22 2025-09-01
## 297       2025-10-01     4.09 2025-10-01
## 298       2025-11-01     3.88 2025-11-01
## 299       2025-12-01     3.72 2025-12-01
## 300       2026-01-01     3.64 2026-01-01
# Show available date range 
first_effr <- min(effr_raw$DATE, na.rm = TRUE)
last_effr  <- max(effr_raw$DATE, na.rm = TRUE)

cat("FEDFUNDS first available date:", format(first_effr, "%Y-%m-%d"), "\n")
## FEDFUNDS first available date: 2001-02-01
cat("FEDFUNDS last available date :", format(last_effr, "%Y-%m-%d"), "\n")
## FEDFUNDS last available date : 2026-01-01
effr_monthly <- data.frame(DATE = effr_raw$DATE, FEDFUNDS = effr_raw$FEDFUNDS)
# Looking for NA's
check_missing <- function(df, name) {
  total <- nrow(df)
  nas   <- sum(is.na(df[, 2]))
  cat("\n", name, "\n",
      "Rows:", total, "\n",
      "Missing values:", nas, "\n",
      "Date range:", format(min(df$DATE), "%Y-%m"), "to", format(max(df$DATE), "%Y-%m"), "\n")
  
  if (nas > 0) {
    missing_idx <- which(is.na(df[, 2]))
    cat("First few missing dates:\n")
    print(head(df$DATE[missing_idx], 10))
  }
}

# Run for each source
check_missing(djia_raw, "Dow Jones INdustrial Average (SP500)")
## 
##  Dow Jones INdustrial Average (SP500) 
##  Rows: 2610 
##  Missing values: 95 
##  Date range: 2016-02 to 2026-02 
## First few missing dates:
##  [1] "2016-02-15" "2016-03-25" "2016-05-30" "2016-07-04" "2016-09-05"
##  [6] "2016-11-24" "2016-12-26" "2017-01-02" "2017-01-16" "2017-02-20"
check_missing(gs10_raw,  "10-Year Treasury Yield (GS10)")
## 
##  10-Year Treasury Yield (GS10) 
##  Rows: 300 
##  Missing values: 0 
##  Date range: 2001-02 to 2026-01
check_missing(effr_raw,  "Fed Funds Rate (FEDFUNDS)")
## 
##  Fed Funds Rate (FEDFUNDS) 
##  Rows: 300 
##  Missing values: 0 
##  Date range: 2001-02 to 2026-01
#MIssing values/NA
# Extract year/ month
djia_raw$Year  <- format(djia_raw$DATE, "%Y")
djia_raw$Month <- format(djia_raw$DATE, "%m")

# Count NAs per month/year
na_table <- aggregate(djia_raw$DJIA,
                      by = list(Year = djia_raw$Year, Month = djia_raw$Month),
                      FUN = function(x) sum(is.na(x)))

# filter to months with NAs
names(na_table)[3] <- "NA_Count"
na_table_with_na <- na_table[na_table$NA_Count > 0, ]

# Yes/No flag
na_table_with_na$Has_Missing <- "Yes"


na_table_with_na
##     Year Month NA_Count Has_Missing
## 1   2017    01        2         Yes
## 2   2018    01        2         Yes
## 3   2019    01        2         Yes
## 4   2020    01        2         Yes
## 5   2021    01        2         Yes
## 6   2022    01        1         Yes
## 7   2023    01        2         Yes
## 8   2024    01        2         Yes
## 9   2025    01        3         Yes
## 10  2026    01        2         Yes
## 11  2016    02        1         Yes
## 12  2017    02        1         Yes
## 13  2018    02        1         Yes
## 14  2019    02        1         Yes
## 15  2020    02        1         Yes
## 16  2021    02        1         Yes
## 17  2022    02        1         Yes
## 18  2023    02        1         Yes
## 19  2024    02        1         Yes
## 20  2025    02        1         Yes
## 22  2016    03        1         Yes
## 24  2018    03        1         Yes
## 30  2024    03        1         Yes
## 33  2017    04        1         Yes
## 35  2019    04        1         Yes
## 36  2020    04        1         Yes
## 37  2021    04        1         Yes
## 38  2022    04        1         Yes
## 39  2023    04        1         Yes
## 41  2025    04        1         Yes
## 42  2016    05        1         Yes
## 43  2017    05        1         Yes
## 44  2018    05        1         Yes
## 45  2019    05        1         Yes
## 46  2020    05        1         Yes
## 47  2021    05        1         Yes
## 48  2022    05        1         Yes
## 49  2023    05        1         Yes
## 50  2024    05        1         Yes
## 51  2025    05        1         Yes
## 58  2022    06        1         Yes
## 59  2023    06        1         Yes
## 60  2024    06        1         Yes
## 61  2025    06        1         Yes
## 62  2016    07        1         Yes
## 63  2017    07        1         Yes
## 64  2018    07        1         Yes
## 65  2019    07        1         Yes
## 66  2020    07        1         Yes
## 67  2021    07        1         Yes
## 68  2022    07        1         Yes
## 69  2023    07        1         Yes
## 70  2024    07        1         Yes
## 71  2025    07        1         Yes
## 82  2016    09        1         Yes
## 83  2017    09        1         Yes
## 84  2018    09        1         Yes
## 85  2019    09        1         Yes
## 86  2020    09        1         Yes
## 87  2021    09        1         Yes
## 88  2022    09        1         Yes
## 89  2023    09        1         Yes
## 90  2024    09        1         Yes
## 91  2025    09        1         Yes
## 102 2016    11        1         Yes
## 103 2017    11        1         Yes
## 104 2018    11        1         Yes
## 105 2019    11        1         Yes
## 106 2020    11        1         Yes
## 107 2021    11        1         Yes
## 108 2022    11        1         Yes
## 109 2023    11        1         Yes
## 110 2024    11        1         Yes
## 111 2025    11        1         Yes
## 112 2016    12        1         Yes
## 113 2017    12        1         Yes
## 114 2018    12        2         Yes
## 115 2019    12        1         Yes
## 116 2020    12        1         Yes
## 117 2021    12        1         Yes
## 118 2022    12        1         Yes
## 119 2023    12        1         Yes
## 120 2024    12        1         Yes
## 121 2025    12        1         Yes
#NA?missing values holidays or weekend??

na_dates <- djia_raw$DATE[is.na(djia_raw$DJIA)]

# Check weekdays
weekday_check <- weekdays(na_dates)  # e.g., "Saturday", "Sunday"

# check holidays 
is_holiday <- function(d) {
  y <- as.integer(format(d, "%Y"))
  m <- as.integer(format(d, "%m"))
  day <- as.integer(format(d, "%d"))
  

  if ((m == 1 && day == 1) ||                     
      (m == 7 && day == 4) ||                     
      (m == 12 && day == 25)) {                   
    return(TRUE)
  }
  

  if (m == 1 && weekdays(d) == "Monday" && day >= 15 && day <= 21) return(TRUE)
  if (m == 2 && weekdays(d) == "Monday" && day >= 15 && day <= 21) return(TRUE)
  if (m == 5 && weekdays(d) == "Monday" && day >= 25) return(TRUE)
  if (m == 9 && weekdays(d) == "Monday" && day <= 7) return(TRUE)
  if (m == 11 && weekdays(d) == "Thursday" && day >= 22 && day <= 28) return(TRUE)
  
  return(FALSE)
}

# table
na_info <- data.frame(
  Date     = na_dates,
  Weekday  = weekday_check,
  Weekend  = weekday_check %in% c("Saturday", "Sunday"),
  Holiday  = sapply(na_dates, is_holiday),
  stringsAsFactors = FALSE
)

#results
head(na_info, 100)
##          Date   Weekday Weekend Holiday
## 1  2016-02-15    Monday   FALSE    TRUE
## 2  2016-03-25    Friday   FALSE   FALSE
## 3  2016-05-30    Monday   FALSE    TRUE
## 4  2016-07-04    Monday   FALSE    TRUE
## 5  2016-09-05    Monday   FALSE    TRUE
## 6  2016-11-24  Thursday   FALSE    TRUE
## 7  2016-12-26    Monday   FALSE   FALSE
## 8  2017-01-02    Monday   FALSE   FALSE
## 9  2017-01-16    Monday   FALSE    TRUE
## 10 2017-02-20    Monday   FALSE    TRUE
## 11 2017-04-14    Friday   FALSE   FALSE
## 12 2017-05-29    Monday   FALSE    TRUE
## 13 2017-07-04   Tuesday   FALSE    TRUE
## 14 2017-09-04    Monday   FALSE    TRUE
## 15 2017-11-23  Thursday   FALSE    TRUE
## 16 2017-12-25    Monday   FALSE    TRUE
## 17 2018-01-01    Monday   FALSE    TRUE
## 18 2018-01-15    Monday   FALSE    TRUE
## 19 2018-02-19    Monday   FALSE    TRUE
## 20 2018-03-30    Friday   FALSE   FALSE
## 21 2018-05-28    Monday   FALSE    TRUE
## 22 2018-07-04 Wednesday   FALSE    TRUE
## 23 2018-09-03    Monday   FALSE    TRUE
## 24 2018-11-22  Thursday   FALSE    TRUE
## 25 2018-12-05 Wednesday   FALSE   FALSE
## 26 2018-12-25   Tuesday   FALSE    TRUE
## 27 2019-01-01   Tuesday   FALSE    TRUE
## 28 2019-01-21    Monday   FALSE    TRUE
## 29 2019-02-18    Monday   FALSE    TRUE
## 30 2019-04-19    Friday   FALSE   FALSE
## 31 2019-05-27    Monday   FALSE    TRUE
## 32 2019-07-04  Thursday   FALSE    TRUE
## 33 2019-09-02    Monday   FALSE    TRUE
## 34 2019-11-28  Thursday   FALSE    TRUE
## 35 2019-12-25 Wednesday   FALSE    TRUE
## 36 2020-01-01 Wednesday   FALSE    TRUE
## 37 2020-01-20    Monday   FALSE    TRUE
## 38 2020-02-17    Monday   FALSE    TRUE
## 39 2020-04-10    Friday   FALSE   FALSE
## 40 2020-05-25    Monday   FALSE    TRUE
## 41 2020-07-03    Friday   FALSE   FALSE
## 42 2020-09-07    Monday   FALSE    TRUE
## 43 2020-11-26  Thursday   FALSE    TRUE
## 44 2020-12-25    Friday   FALSE    TRUE
## 45 2021-01-01    Friday   FALSE    TRUE
## 46 2021-01-18    Monday   FALSE    TRUE
## 47 2021-02-15    Monday   FALSE    TRUE
## 48 2021-04-02    Friday   FALSE   FALSE
## 49 2021-05-31    Monday   FALSE    TRUE
## 50 2021-07-05    Monday   FALSE   FALSE
## 51 2021-09-06    Monday   FALSE    TRUE
## 52 2021-11-25  Thursday   FALSE    TRUE
## 53 2021-12-24    Friday   FALSE   FALSE
## 54 2022-01-17    Monday   FALSE    TRUE
## 55 2022-02-21    Monday   FALSE    TRUE
## 56 2022-04-15    Friday   FALSE   FALSE
## 57 2022-05-30    Monday   FALSE    TRUE
## 58 2022-06-20    Monday   FALSE   FALSE
## 59 2022-07-04    Monday   FALSE    TRUE
## 60 2022-09-05    Monday   FALSE    TRUE
## 61 2022-11-24  Thursday   FALSE    TRUE
## 62 2022-12-26    Monday   FALSE   FALSE
## 63 2023-01-02    Monday   FALSE   FALSE
## 64 2023-01-16    Monday   FALSE    TRUE
## 65 2023-02-20    Monday   FALSE    TRUE
## 66 2023-04-07    Friday   FALSE   FALSE
## 67 2023-05-29    Monday   FALSE    TRUE
## 68 2023-06-19    Monday   FALSE   FALSE
## 69 2023-07-04   Tuesday   FALSE    TRUE
## 70 2023-09-04    Monday   FALSE    TRUE
## 71 2023-11-23  Thursday   FALSE    TRUE
## 72 2023-12-25    Monday   FALSE    TRUE
## 73 2024-01-01    Monday   FALSE    TRUE
## 74 2024-01-15    Monday   FALSE    TRUE
## 75 2024-02-19    Monday   FALSE    TRUE
## 76 2024-03-29    Friday   FALSE   FALSE
## 77 2024-05-27    Monday   FALSE    TRUE
## 78 2024-06-19 Wednesday   FALSE   FALSE
## 79 2024-07-04  Thursday   FALSE    TRUE
## 80 2024-09-02    Monday   FALSE    TRUE
## 81 2024-11-28  Thursday   FALSE    TRUE
## 82 2024-12-25 Wednesday   FALSE    TRUE
## 83 2025-01-01 Wednesday   FALSE    TRUE
## 84 2025-01-09  Thursday   FALSE   FALSE
## 85 2025-01-20    Monday   FALSE    TRUE
## 86 2025-02-17    Monday   FALSE    TRUE
## 87 2025-04-18    Friday   FALSE   FALSE
## 88 2025-05-26    Monday   FALSE    TRUE
## 89 2025-06-19  Thursday   FALSE   FALSE
## 90 2025-07-04    Friday   FALSE    TRUE
## 91 2025-09-01    Monday   FALSE    TRUE
## 92 2025-11-27  Thursday   FALSE    TRUE
## 93 2025-12-25  Thursday   FALSE    TRUE
## 94 2026-01-01  Thursday   FALSE    TRUE
## 95 2026-01-19    Monday   FALSE    TRUE
table(na_info$Holiday)
## 
## FALSE  TRUE 
##    23    72
## drop missing values/na

na_dates <- djia_raw$DATE[is.na(djia_raw$DJIA)]

# Remove NA
djia_clean <- djia_raw[!is.na(djia_raw$DJIA), ]

# results
cat("Original rows :", nrow(djia_raw), "\n")
## Original rows : 2610
cat("Dropped rows  :", length(na_dates), "\n")
## Dropped rows  : 95
cat("Remaining rows:", nrow(djia_clean), "\n")
## Remaining rows: 2515
#table of number of obs after dropping NA/missing values

row_counts <- data.frame(
  Dataset   = c("Original DJIA", "Cleaned DJIA"),
  Row_Count = c(nrow(djia_raw), nrow(djia_clean))
)

print(row_counts)
##         Dataset Row_Count
## 1 Original DJIA      2610
## 2  Cleaned DJIA      2515
compare_summary <- data.frame(
  Dataset  = c("DJIA (original)", "DJIA (cleaned)", 
               "10-Year Treasury (GS10)", "Fed Funds Rate (FEDFUNDS)"),
  Rows     = c(nrow(djia_raw), nrow(djia_clean), 
               nrow(gs10_raw), nrow(effr_raw)),
  NA_Count = c(sum(is.na(djia_raw$DJIA)),
               sum(is.na(djia_clean$DJIA)),
               sum(is.na(gs10_raw$GS10)),
               sum(is.na(effr_raw$FEDFUNDS)))
)

print(compare_summary, row.names = FALSE)
##                    Dataset Rows NA_Count
##            DJIA (original) 2610       95
##             DJIA (cleaned) 2515        0
##    10-Year Treasury (GS10)  300        0
##  Fed Funds Rate (FEDFUNDS)  300        0
## Collapse DJIA to one row per month (monthly average)
djia_monthly <- aggregate(DJIA ~ format(DATE, "%Y-%m"), data = djia_raw, FUN = mean, na.rm = TRUE)

# Fix column names
names(djia_monthly)[1] <- "YearMonth"
djia_monthly$DATE <- as.Date(paste0(djia_monthly$YearMonth, "-01"))
djia_monthly <- djia_monthly[, c("DATE", "DJIA")]

djia_monthly
##           DATE     DJIA
## 1   2016-02-01 16295.80
## 2   2016-03-01 17302.14
## 3   2016-04-01 17844.37
## 4   2016-05-01 17692.32
## 5   2016-06-01 17754.87
## 6   2016-07-01 18341.18
## 7   2016-08-01 18495.19
## 8   2016-09-01 18267.40
## 9   2016-10-01 18184.55
## 10  2016-11-01 18697.33
## 11  2016-12-01 19712.42
## 12  2017-01-01 19908.15
## 13  2017-02-01 20424.14
## 14  2017-03-01 20823.06
## 15  2017-04-01 20684.69
## 16  2017-05-01 20936.81
## 17  2017-06-01 21317.80
## 18  2017-07-01 21581.25
## 19  2017-08-01 21914.08
## 20  2017-09-01 22173.41
## 21  2017-10-01 23036.24
## 22  2017-11-01 23557.93
## 23  2017-12-01 24545.38
## 24  2018-01-01 25804.02
## 25  2018-02-01 24981.55
## 26  2018-03-01 24582.17
## 27  2018-04-01 24304.21
## 28  2018-05-01 24572.53
## 29  2018-06-01 24790.11
## 30  2018-07-01 24978.23
## 31  2018-08-01 25629.99
## 32  2018-09-01 26232.67
## 33  2018-10-01 25609.34
## 34  2018-11-01 25258.68
## 35  2018-12-01 23805.55
## 36  2019-01-01 24157.80
## 37  2019-02-01 25605.53
## 38  2019-03-01 25722.62
## 39  2019-04-01 26401.58
## 40  2019-05-01 25744.79
## 41  2019-06-01 26160.10
## 42  2019-07-01 27089.19
## 43  2019-08-01 26058.22
## 44  2019-09-01 26900.21
## 45  2019-10-01 26736.80
## 46  2019-11-01 27797.05
## 47  2019-12-01 28167.01
## 48  2020-01-01 28879.99
## 49  2020-02-01 28519.73
## 50  2020-03-01 22637.42
## 51  2020-04-01 23293.90
## 52  2020-05-01 24271.02
## 53  2020-06-01 26062.27
## 54  2020-07-01 26385.83
## 55  2020-08-01 27821.37
## 56  2020-09-01 27733.40
## 57  2020-10-01 28005.10
## 58  2020-11-01 29124.04
## 59  2020-12-01 30148.58
## 60  2021-01-01 30821.35
## 61  2021-02-01 31283.91
## 62  2021-03-01 32373.29
## 63  2021-04-01 33803.29
## 64  2021-05-01 34270.31
## 65  2021-06-01 34289.91
## 66  2021-07-01 34798.80
## 67  2021-08-01 35243.97
## 68  2021-09-01 34688.42
## 69  2021-10-01 35055.52
## 70  2021-11-01 35848.57
## 71  2021-12-01 35641.33
## 72  2022-01-01 35456.15
## 73  2022-02-01 34648.48
## 74  2022-03-01 34029.74
## 75  2022-04-01 34314.99
## 76  2022-05-01 32379.46
## 77  2022-06-01 31446.71
## 78  2022-07-01 31535.32
## 79  2022-08-01 33009.56
## 80  2022-09-01 30649.56
## 81  2022-10-01 30570.68
## 82  2022-11-01 33417.96
## 83  2022-12-01 33482.26
## 84  2023-01-01 33656.00
## 85  2023-02-01 33648.26
## 86  2023-03-01 32483.48
## 87  2023-04-01 33731.28
## 88  2023-05-01 33316.80
## 89  2023-06-01 33904.98
## 90  2023-07-01 34777.20
## 91  2023-08-01 34880.66
## 92  2023-09-01 34318.89
## 93  2023-10-01 33319.49
## 94  2023-11-01 34704.50
## 95  2023-12-01 36947.93
## 96  2024-01-01 37763.95
## 97  2024-02-01 38720.52
## 98  2024-03-01 39105.52
## 99  2024-04-01 38401.22
## 100 2024-05-01 39129.39
## 101 2024-06-01 38903.73
## 102 2024-07-01 40086.03
## 103 2024-08-01 40310.81
## 104 2024-09-01 41490.88
## 105 2024-10-01 42494.20
## 106 2024-11-01 43716.93
## 107 2024-12-01 43655.57
## 108 2025-01-01 43524.35
## 109 2025-02-01 44209.31
## 110 2025-03-01 42092.13
## 111 2025-04-01 39876.33
## 112 2025-05-01 41864.25
## 113 2025-06-01 42752.94
## 114 2025-07-01 44499.74
## 115 2025-08-01 44764.85
## 116 2025-09-01 45908.10
## 117 2025-10-01 46709.67
## 118 2025-11-01 47016.37
## 119 2025-12-01 48118.87
## 120 2026-01-01 49138.27
## 121 2026-02-01 49434.87
# line plot
plot(djia_monthly$DATE, djia_monthly$DJIA,
     type = "l", lwd = 2, col = "blue",
     xlab = "Year",
     ylab = "DJIA (Monthly Avg)",
     main = "DJIA Monthly Average (Past 10 Years)")
grid(nx = NA, ny = NULL, lty = "dotted")

# --- Restrict datasets across common date range ---
common_start <- max(min(djia_monthly$DATE, na.rm = TRUE),
                    min(gs10_monthly$DATE, na.rm = TRUE),
                    min(effr_monthly$DATE, na.rm = TRUE))

common_end <- min(max(djia_monthly$DATE, na.rm = TRUE),
                  max(gs10_monthly$DATE, na.rm = TRUE),
                  max(effr_monthly$DATE, na.rm = TRUE))

cat("Restricting all datasets to:", common_start, "to", common_end, "\n")
## Restricting all datasets to: 16832 to 20454
# restrict each dataset
djia_monthly   <- subset(djia_monthly, DATE >= common_start & DATE <= common_end)
gs10_monthly   <- subset(gs10_monthly, DATE >= common_start & DATE <= common_end)
effr_monthly   <- subset(effr_monthly, DATE >= common_start & DATE <= common_end)

# --- Merge all three on DATE ---
merged_data <- merge(djia_monthly, gs10_monthly, by = "DATE", all = FALSE)
merged_data <- merge(merged_data, effr_monthly, by = "DATE", all = FALSE)
merged_data <- merged_data[order(merged_data$DATE), ]

# --- Min-Max Scaling Function (0–1) ---
minmax_scale <- function(x) (x - min(x, na.rm = TRUE)) / 
                             (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))

merged_data$DJIA_mm     <- minmax_scale(merged_data$DJIA)
merged_data$GS10_mm     <- minmax_scale(merged_data$GS10)
merged_data$FEDFUNDS_mm <- minmax_scale(merged_data$FEDFUNDS)

# --- Plot setup (suppress default x-axis for custom labels) ---
plot(merged_data$DATE, merged_data$DJIA_mm, type = "n",
     xaxt = "n",  # suppress default axis
     xlab = "Year", ylab = "Min–Max Scaled (0–1)",
     main = "Trifecta of Economic Indicators (Min–Max Scaled)")

# --- Custom x-axis with every year ---
years <- seq(from = as.integer(format(min(merged_data$DATE), "%Y")),
             to   = as.integer(format(max(merged_data$DATE), "%Y")),
             by = 1)
axis(1, at = as.Date(paste0(years, "-01-01")), labels = years)

# --- Add shaded COVID recession (Feb–Apr 2020) ---
rect(as.Date("2020-02-01"), 0, as.Date("2020-04-30"), 1,
     col = rgb(0.8, 0.8, 0.8, 0.5), border = NA)

# --- Add label for shaded region ---
text(as.Date("2020-06-01"), 0.9, "COVID recession", cex = 0.8, col = "black", adj = 0)

# --- Add the three lines ---
lines(merged_data$DATE, merged_data$DJIA_mm,     lwd = 2, col = "orange",   lty = 2)
lines(merged_data$DATE, merged_data$GS10_mm,     lwd = 2, col = "black",    lty = 3)       
lines(merged_data$DATE, merged_data$FEDFUNDS_mm, lwd = 2, col = "lightblue", lty = 4) 

# --- Legend moved to top left ---
legend("topleft",
       legend = c("DJIA (Min–Max)", "10Y Treasury Yield (Min–Max)", 
                  "Fed Funds Rate (Min–Max)"),
       col = c("orange", "black", "lightblue"),
       lwd = c(2, 2, 2), lty = c(2, 3, 4),
       bty = "n", cex = 0.8)

# --- Grid ---
grid(nx = NA, ny = NULL, lty = "dotted")