
######### Read the Excel file into a data frame

library(readxl)


library(readxl)
#For the models
#data <- Master_Thesis_Lochlan_Reeves_18022024 <- read_excel("Thesis/Python/Master_Thesis_Lochlan_Reeves_18022024.xlsx", range = "J5:R290")
#View(Master_Thesis_Lochlan_Reeves_18022024)

data <- Master_Thesis_Lochlan_Reeves_18022024 <- read_excel("Thesis/Python/Master_Thesis_Lochlan_Reeves_18022024.xlsx", range = "J5:R290")
View(Master_Thesis_Lochlan_Reeves_18022024)


# Extract numeric data excluding 'Dates' column
numeric_data <- data[, -1]
  

 
##################### PART I: Data Transformations #######################################


# Convert 'Dates' column to date type if it is not already
data$Dates <- as.Date(data$Dates)

# Calculate descriptive statistics
min_values <- apply(numeric_data, 2, min, na.rm = TRUE)
max_values <- apply(numeric_data, 2, max, na.rm = TRUE)
mean_values <- apply(numeric_data, 2, mean, na.rm = TRUE)
var_values <- apply(numeric_data, 2, var, na.rm = TRUE)

descriptive_stats <- data.frame(
  Minimum = min_values,
  Maximum = max_values,
  Mean = mean_values,
  Variance = var_values)

print(descriptive_stats)



### Outliers detection and removal

#library(gridExtra)
x = data$COFB.BR
#histogram, Q-Q plot, and boxplot
par(mfrow = c(1, 3))
hist(x, main = "Histogram")
boxplot(x, main = "Boxplot")
qqnorm(x, main = "Normal Q-Q plot")
# get mean and Standard deviation
#mean = mean(x)
#std = sd(x)






################################ PART II : Correlation Analysis #######################################


### Linear relationships - Univariate regressions

M1 <- lm(data$COFB.BR         # Fit a linear regression model
               ~ data$`CPI Index`)
summary(M1)

M2 <- lm(data$COFB.BR         # Fit a linear regression model
               ~ data$Unemployment)
summary(M2)


M3 <- lm(data$COFB.BR         # Fit a linear regression model
         ~ data$`Interest Rate`)
summary(M3)

M4 <- lm(data$COFB.BR         # Fit a linear regression model
         ~ data$Oil)
summary(M4)

M5 <- lm(data$COFB.BR         # Fit a linear regression model
         ~ data$CAC40)
summary(M5)

M6 <- lm(data$COFB.BR         # Fit a linear regression model
         ~ data$DAX40)
summary(M6)

M7 <- lm(data$COFB.BR         # Fit a linear regression model
         ~ data$NASDAQ)
summary(M7)

##### Scatter plots

y <- data$COFB.BR
x <- data$CAC40
# Set graphical parameters to display plot alone and increase size
par(mfrow=c(1,1), mar=c(5, 5, 4, 2) + 0.1, oma=c(0, 0, 0, 0))

# Create scatter plot
plot(x, y, main="Scatter Plot with Regression Line", xlab="COFB.BR", ylab="CAC40", cex.main=1.5, cex.lab=1.5)

# Fit linear regression model
fit <- lm(y ~ x)

# Add regression line to plot
abline(fit, col="red")

# Add equation of the regression line to the plot
eq <- paste("y =", round(coef(fit)[1], 4), "* x +", round(coef(fit)[2], 4))
legend("topright", legend=eq, col="red", lty=1, cex=1.2)



 ################################ PART III : Multivariate Linear Regression #######################################



M8 <- lm(data$COFB.BR         # Fit a linear regression model
         ~ data$`CPI Index`
         + data$Unemployment
         + data$`Interest Rate`
         + data$Oil
         + data$CAC40
         + data$DAX40
         + data$NASDAQ)

summary(M8) # Print the summary of the regression model



M9 <- lm(data$COFB.BR         # Fit a linear regression model
               ~ #data$`CPI Index`
               #+ data$Unemployment
               + sqrt(data$`Interest Rate`)
               #+ data$Oil
               + data$CAC40)
               #+ data$DAX40)
               #+ data$NASDAQ)
summary(M9) # Print the summary of the regression model





# PART III : Assumption Testing #######################################

######## 1) Normality assumption: #########

library(lmtest)

qqnorm(M9$residuals) #Q-Q plot of residuals
print(qqline(M9$residuals)) #--> OK, it's Normal

shapiro.test(M9$residuals) # Shapiro-Wilk test for normality of residuals

fBasics::jarqueberaTest(M9$residuals)


######## 2) Multicolinearity ########

library(car)
print(vif(M9))

independent_variables <- data[, -1] # Exclude the first column ('Dates')
correlation_matrix <- cor(independent_variables) # Calculate the correlation matrix
print(correlation_matrix) # Print the correlation matrix

######## 3)Heteroscedasticity ########

lmtest::gqtest(M9)



######## 3)Auto-Correlation ########

library(car) # Load the required library

durbinWatsonTest(M9) # Perform the Durbin-Watson test

bgtest(M9) # Perform the Breusch-Godfrey LM test



