student ID - 00904369 title: “Analysis of [Diffusion Confusion ]” author: “[ Kodanda Naidu Motipalli]” date: “r Sys.Date(13/12/2024)” output: html_notebook: toc: true toc_depth: 3 number_sections: true —

{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE) library(ggplot2) library(dplyr) library(knitr) library(knitr)

Class Information

Load Libraries

library(ggplot2) library(dplyr) library(tidyr) library(readr)

Load datasets

employment <- read.csv(“C:/Users/kodan/OneDrive/Desktop/cfnaidiff.csv”) industrial_production <- read.csv(“C:/Users/kodan/OneDrive/Desktop/retail_sales.csv”) retail_sales <- read.csv(“C:/Users/kodan/OneDrive/Desktop/industrial_production.csv”) cfnaidiff <- read.csv(“C:/Users/kodan/OneDrive/Desktop/employment.csv”)

Ensure proper column names

colnames(employment) <- c(“date”, “value”)
colnames(industrial_production) <- c(“date”, “value”) colnames(retail_sales) <- c(“date”, “value”) colnames(cfnaidiff) <- c(“date”, “value”)

Convert ‘date’ to Date format

employment\(date <- as.Date(employment\)date) industrial_production\(date <- as.Date(industrial_production\)date) retail_sales\(date <- as.Date(retail_sales\)date) cfnaidiff\(date <- as.Date(cfnaidiff\)date)

Handle month-over-month changes and binary indicators

employment <- employment %>% arrange(date) %>% mutate(change = value - lag(value), indicator = ifelse(change > 0, 1, 0))

industrial_production <- industrial_production %>% arrange(date) %>% mutate(change = value - lag(value), indicator = ifelse(change > 0, 1, 0))

retail_sales <- retail_sales %>% arrange(date) %>% mutate(change = value - lag(value), indicator = ifelse(change > 0, 1, 0))

Construct Diffusion Index

diffusion_index <- employment %>% select(date, emp_indicator = indicator) %>% inner_join(select(industrial_production, date, ind_indicator = indicator), by = “date”) %>% inner_join(select(retail_sales, date, retail_indicator = indicator), by = “date”) %>% mutate(diffusion_index = (emp_indicator + ind_indicator + retail_indicator) / 3)

Merge with CFNAIDIFF

combined_data <- diffusion_index %>% inner_join(select(cfnaidiff, date, cfnaidiff_value = value), by = “date”)

Remove non-finite or missing values

combined_data <- combined_data %>% filter(!is.na(diffusion_index), !is.na(cfnaidiff_value))

Correlation Analysis

correlation <- cor(combined_data\(diffusion_index, combined_data\)cfnaidiff_value, use = “complete.obs”) print(paste(“Correlation Coefficient:”, round(correlation, 4)))

Visualization

ggplot(combined_data, aes(x = date)) + geom_line(aes(y = diffusion_index, color = “My Diffusion Index”), linewidth = 1) + geom_line(aes(y = cfnaidiff_value, color = “CFNAIDIFF”), linewidth = 1) + geom_smooth(aes(y = diffusion_index), method = “loess”, se = FALSE, color = “blue”, linetype = “dashed”) + geom_smooth(aes(y = cfnaidiff_value), method = “loess”, se = FALSE, color = “red”, linetype = “dashed”) + labs(title = “Comparison of Diffusion Indexes”, x = “Date”, y = “Value”, color = “Index”) + theme_minimal()