Preparing the Data

# Load CSVs
DJI <- read.csv("DJI.csv", stringsAsFactors = FALSE)
GSPC <- read.csv("GSPC.csv", stringsAsFactors = FALSE)
Russel3000 <- read.csv("Russel3000.csv", stringsAsFactors = FALSE)

# Clean Close columns
DJI$Close <- as.numeric(gsub("[\\$, ]", "", DJI$Close))
GSPC$Close <- as.numeric(gsub("[\\$, ]", "", GSPC$Close))
Russel3000$Close <- as.numeric(gsub("[\\$, ]", "", Russel3000$Close))

# Convert Date
DJI$Date <- as.Date(DJI$Date)
GSPC$Date <- as.Date(GSPC$Date)
Russel3000$Date <- as.Date(Russel3000$Date)

# Rename Close columns
DJI <- rename(DJI, DJIA = Close) %>% select(Date, DJIA)
GSPC <- rename(GSPC, SP500 = Close) %>% select(Date, SP500)
Russel3000 <- rename(Russel3000, R3000 = Close) %>% select(Date, R3000)

# Merge all datasets
merged_data <- full_join(DJI, GSPC, by = "Date") %>%
               full_join(Russel3000, by = "Date") %>%
               filter(complete.cases(.))

# Date range logic
latest_date <- max(merged_data$Date)
one_year_ago <- latest_date - 365
three_months_ago <- latest_date %m-% months(3)
one_month_ago <- latest_date %m-% months(1)

# Subsets
data_1yr <- filter(merged_data, Date >= one_year_ago & Date <= latest_date)
data_3mo <- filter(merged_data, Date >= three_months_ago & Date <= latest_date)
data_1mo <- filter(merged_data, Date >= one_month_ago & Date <= latest_date)

# Pivot for plots
long_1yr <- pivot_longer(data_1yr, cols = c(DJIA, SP500, R3000),
                         names_to = "Index", values_to = "Close")
long_3mo <- pivot_longer(data_3mo, cols = c(DJIA, SP500, R3000),
                         names_to = "Index", values_to = "Close")
long_1mo <- pivot_longer(data_1mo, cols = c(DJIA, SP500, R3000),
                         names_to = "Index", values_to = "Close")

latest_date <- max(merged_data$Date)
one_year_ago <- latest_date - 365
three_months_ago <- latest_date %m-% months(3)
one_month_ago <- latest_date %m-% months(1)


df_1yr <- merged_data %>%
  filter(Date >= one_year_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000), 
               names_to = "Index", 
               values_to = "Close")

df_3months <- merged_data %>%
  filter(Date >= three_months_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df_1month <- merged_data %>%
  filter(Date >= one_month_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df2 <- merged_data %>%
  filter(Date >= one_year_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df2_3months <- merged_data %>%
  filter(Date >= three_months_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df2_1month <- merged_data %>%
  filter(Date >= one_month_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df3 <- merged_data %>%
  filter(Date >= one_year_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df3_3months <- merged_data %>%
  filter(Date >= three_months_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

df3_1month <- merged_data %>%
  filter(Date >= one_month_ago & Date <= latest_date) %>%
  pivot_longer(cols = c(DJIA, SP500, R3000),
               names_to = "Index",
               values_to = "Close")

three_months <- merged_data %>%
  filter(Date >= (max(Date) %m-% months(3)))

one_month <- merged_data %>%
  filter(Date >= (max(Date) %m-% months(1)))

Line Charts by Index

1 Year Line Chart Dow Jones Industrial Average

ggplot(df_1yr, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "1-Year Closing Prices Dow Jones Industrial Average", x = "Date", y = "Close") + theme_minimal()

3-Month Line Chart Dow Jones Industrial Average

ggplot(df_3months, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "3-Month Closing Prices Dow Jones Industrial Average", x = "Date", y = "Close") + theme_minimal()

1-Month Line Chart Dow Jones Industrial Average

ggplot(df_1month, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "1-Month Closing Prices Dow Jones Industrial Average", x = "Date", y = "Close") + theme_minimal()

1-Year Line Chart S&P

ggplot(df2, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "1-Year Closing Prices S&P500", x = "Date", y = "Close") + theme_minimal()

3-Month Line Chart S&P

ggplot(df2_3months, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "3-Month Closing Prices S&P500", x = "Date", y = "Close") + theme_minimal()

1-Month Line Chart S&P

ggplot(df2_1month, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "1-Month Closing Prices S&P500", x = "Date", y = "Close") + theme_minimal()

1-Year Line Chart Russell 3000

ggplot(df3, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "1-Year Closing Prices Russell 3000", x = "Date", y = "Close") + theme_minimal()

3-Month Line Chart Russell 3000

ggplot(df3_3months, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "3-Month Closing Prices Russell 3000", x = "Date", y = "Close") + theme_minimal()

1-Month Line Chart Russell 3000

ggplot(df3_1month, aes(x = Date, y = Close)) + geom_line(color = "steelblue") + facet_wrap(~Index, scales = "free_y") + labs(title = "1-Month Closing Prices Russell 3000", x = "Date", y = "Close") + theme_minimal()

Correlation Matricies

1 Year Correlation

cor_matrix <- cor(merged_data[, c("DJIA", "SP500", "R3000")])
corrplot(cor_matrix, method = "circle", type = "upper", tl.col = "black",
         addCoef.col = "black", title = "1-Year Correlation", mar = c(0,0,2,0))

3-Month Correlation

cor_matrix2 <- cor(three_months[, c("DJIA", "SP500", "R3000")])
corrplot(cor_matrix2, method = "circle", type = "upper", tl.col = "black",
         addCoef.col = "black", title = "3-Month Correlation", mar = c(0,0,2,0))

1-Month Correlation

cor_matrix3 <- cor(one_month[, c("DJIA", "SP500", "R3000")])
corrplot(cor_matrix3, method = "circle", type = "upper", tl.col = "black",
         addCoef.col = "black", title = "1-Month Correlation", mar = c(0,0,2,0))

Trend Lines by Index

1-Year Trend Lines

ggplot(long_1yr, aes(x = Date, y = Close)) +
  geom_line(color = "steelblue", alpha = 0.6) +
  geom_smooth(method = "lm", color = "red", se = FALSE) +
  facet_wrap(~Index, scales = "free_y") +
  labs(title = "1-Year Trend Lines for Each Index",
       x = "Date", y = "Close") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

3-Month Trend Lines

ggplot(long_3mo, aes(x = Date, y = Close)) +
  geom_line(color = "steelblue", alpha = 0.6) +
  geom_smooth(method = "lm", color = "red", se = FALSE) +
  facet_wrap(~Index, scales = "free_y") +
  labs(title = "3-Month Trend Lines for Each Index",
       x = "Date", y = "Close") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

1-Month Trend Lines

ggplot(long_1mo, aes(x = Date, y = Close)) +
  geom_line(color = "steelblue", alpha = 0.6) +
  geom_smooth(method = "lm", color = "red", se = FALSE) +
  facet_wrap(~Index, scales = "free_y") +
  labs(title = "1-Month Trend Lines for Each Index",
       x = "Date", y = "Close") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'