title: “R Notebook” output: html_notebook ī—

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(modeest)
library(ggplot2)
# Import data from S&P 500 CSV files
data_SP500<- read.csv("S&P 500-1.csv")

# Import Full Bitcoin price CSV file
data_BTC_USD_full <- read.csv("BTC-USD-1.csv")

# Select only the first two columns, they are 'Date' and 'Close.price.adjusted'
data_BTC_USD <- data_BTC_USD_full[, 1:2]

# Correct the column names if they are not automatically recognized
names(data_BTC_USD) <- c("Date", "Close_Price_Adjusted")

# View the first few rows of the data
#head(data_SP500)
#head(data_BTC_USD)

# View the data types of the first two rows of each dataset
str(data_SP500)
## 'data.frame':    1510 obs. of  2 variables:
##  $ Date : chr  "1/08/2024" "31/07/2024" "30/07/2024" "29/07/2024" ...
##  $ Price: chr  "5,446.68" "5,522.30" "5,436.44" "5,463.54" ...


``` r
str(data_BTC_USD)
## 'data.frame':    2193 obs. of  2 variables:
##  $ Date                : chr  "1/08/2018" "2/08/2018" "3/08/2018" "4/08/2018" ...
##  $ Close_Price_Adjusted: num  7625 7567 7434 7033 7068 ...
# Prepare the data for analysis

# Convert date columns to Date type
data_SP500$Date <- as.Date(data_SP500$Date, format = "%d/%m/%Y")
data_BTC_USD$Date <- as.Date(data_BTC_USD$Date, format = "%d/%m/%Y")

# Convert S&P 500 price data to numeric type
data_SP500$Price <- as.numeric(gsub(",", "", data_SP500$Price))

# Recheck the structure of data to ensure the changes are applied
str(data_SP500)
## 'data.frame':    1510 obs. of  2 variables:
##  $ Date : Date, format: "2024-08-01" "2024-07-31" ...
##  $ Price: num  5447 5522 5436 5464 5459 ...
str(data_BTC_USD)
## 'data.frame':    2193 obs. of  2 variables:
##  $ Date                : Date, format: "2018-08-01" "2018-08-02" ...
##  $ Close_Price_Adjusted: num  7625 7567 7434 7033 7068 ...
# Load necessary libraries
# Calculate descriptive statistics for S&P 500
sp500_stats <- data_SP500 %>%
  reframe(
    Mean = mean(Price, na.rm = TRUE),
    Median = median(Price, na.rm = TRUE),
    Range = max(Price, na.rm = TRUE) - min(Price, na.rm = TRUE),
    StandardDeviation = sd(Price, na.rm = TRUE)
  )

# Calculate descriptive statistics for Bitcoin
btc_stats <- data_BTC_USD %>%
  reframe(
    Mean = mean(Close_Price_Adjusted, na.rm = TRUE),
    Median = median(Close_Price_Adjusted, na.rm = TRUE),
    Range = max(Close_Price_Adjusted, na.rm = TRUE) - min(Close_Price_Adjusted, na.rm = TRUE),
    StandardDeviation = sd(Close_Price_Adjusted, na.rm = TRUE)
  )

# Print the results
print("Descriptive Statistics for S&P 500:")
## [1] "Descriptive Statistics for S&P 500:"
print(sp500_stats)
##       Mean  Median  Range StandardDeviation
## 1 3826.555 3930.69 3429.8          776.8337
print("Descriptive Statistics for Bitcoin:")
## [1] "Descriptive Statistics for Bitcoin:"
print(btc_stats)
##       Mean   Median    Range StandardDeviation
## 1 26747.53 23031.09 69846.74          19162.96
# comparison of the measures of central tendency and the variability 
# Prepare data for plotting
sp500_prepared <- data.frame(Value = data_SP500$Price, Type = "S&P 500")
btc_prepared <- data.frame(Value = data_BTC_USD$Close_Price_Adjusted, Type = "Bitcoin")

# Combine data
combined_data <- rbind(sp500_prepared, btc_prepared)

# Plotting boxplots for comparison
ggplot(combined_data, aes(x = Type, y = Value, fill = Type)) +
  geom_boxplot() +
  labs(title = "Comparison of S&P 500 and Bitcoin Prices",
       x = "Dataset",
       y = "Price",
       fill = "Dataset") +
  theme_minimal() +
  theme(legend.position = "none")

# Time series plot for S&P 500
ggplot(data_SP500, aes(x = Date, y = Price)) +
  geom_line(color = "blue") +
  labs(title = "S&P 500 Price Trend over Five Years",
       x = "Date",
       y = "Price") +
  theme_minimal()

# Time series plot for Bitcoin
ggplot(data_BTC_USD, aes(x = Date, y = Close_Price_Adjusted)) +
  geom_line(color = "red") +
  labs(title = "Bitcoin Price Trend over Five Years",
       x = "Date",
       y = "Close Price") +
  theme_minimal()

# Ensure dates are correctly formatted as Date objects
data_SP500$Date <- as.Date(data_SP500$Date, format = "%d/%m/%Y")
data_BTC_USD$Date <- as.Date(data_BTC_USD$Date, format = "%d/%m/%Y")

# Merge the datasets on the Date column
correlation_data <- merge(data_SP500, data_BTC_USD, by = "Date")

# Check for any issues in the merged data (e.g., NAs, missing data)
#summary(correlation_data)

# Add columns for Year and Half based on the Date
correlation_data$Year <- format(correlation_data$Date, "%Y")
correlation_data$Half <- ifelse(as.numeric(format(correlation_data$Date, "%m")) <= 6, "H1", "H2")

# Calculate bi-annual correlation
biannual_correlation <- correlation_data %>%
  group_by(Year, Half) %>%
  summarize(Correlation = cor(Price, Close_Price_Adjusted, use = "complete.obs"), .groups = "drop")

# Print biannual correlation data to inspect
#print(biannual_correlation)

# Create an ordered factor for the x-axis
biannual_correlation$Period <- factor(interaction(biannual_correlation$Year, biannual_correlation$Half),
                                      levels = unique(interaction(biannual_correlation$Year, biannual_correlation$Half)))

# Plot using the ordered factor
ggplot(biannual_correlation, aes(x = Period, y = Correlation, group = 1)) +
  geom_line() +
  geom_point() +
  labs(title = "Bi-Annual Correlation between S&P 500 and Bitcoin",
       x = "Year and Half",
       y = "Correlation") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) # Rotate x labels for readability

# Merge the datasets on the 'Date' field to correlation work
merged_data <- merge(data_SP500, data_BTC_USD, by = "Date")

# Renaming columns in the merged dataset for clarity
names(merged_data)[names(merged_data) == "Price"] <- "SP500_Close_Price"
names(merged_data)[names(merged_data) == "Close_Price_Adjusted"] <- "BTC_Close_Price"

# Print the first few rows to verify
#head(merged_data)

# View the structure of the renamed dataset
str(merged_data)
## 'data.frame':    1510 obs. of  3 variables:
##  $ Date             : Date, format: "2018-08-01" "2018-08-02" ...
##  $ SP500_Close_Price: num  2813 2827 2840 2850 2858 ...
##  $ BTC_Close_Price  : num  7625 7567 7434 6952 6753 ...
# Compute the correlation coefficient
cor_coefficient <- cor(merged_data$SP500_Close_Price, merged_data$BTC_Close_Price, use = "complete.obs")
print(paste("Correlation coefficient between S&P 500 and Bitcoin:", cor_coefficient))
## [1] "Correlation coefficient between S&P 500 and Bitcoin: 0.897674934899167"
# Create a scatter plot
ggplot(merged_data, aes(x = SP500_Close_Price, y = BTC_Close_Price)) +
  geom_point(aes(color = Date), alpha = 0.6) +  # Color points by Date for additional insight
  geom_smooth(method = "lm", color = "blue", se = FALSE) +  # Add a linear regression line without confidence interval
  scale_color_gradient(low = "blue", high = "red") +  # Use a gradient color scale for the points
  labs(title = "Scatter Plot of S&P 500 vs Bitcoin",
       x = "S&P 500 Close Price",
       y = "Bitcoin Close Price",
       caption = sprintf("Correlation coefficient: %.2f", cor_coefficient)) +
  theme_minimal() +
  theme(legend.position = "none")  # Remove the legend to keep the focus on the data points and trend line
## `geom_smooth()` using formula = 'y ~ x'

# Shapiro-Wilk normality test for S&P 500
shapiro_test_sp500 <- shapiro.test(merged_data$SP500_Close_Price)
print(shapiro_test_sp500)
## 
##  Shapiro-Wilk normality test
## 
## data:  merged_data$SP500_Close_Price
## W = 0.961, p-value < 2.2e-16
# Shapiro-Wilk normality test for Bitcoin
shapiro_test_btc <- shapiro.test(merged_data$BTC_Close_Price)
print(shapiro_test_btc)
## 
##  Shapiro-Wilk normality test
## 
## data:  merged_data$BTC_Close_Price
## W = 0.90956, p-value < 2.2e-16
# Histogram for S&P 500
ggplot(merged_data, aes(x = SP500_Close_Price)) +
  geom_histogram(aes(y = ..density..), bins = 30, fill = "blue", alpha = 0.7) +
  geom_density(color = "red", size = 1) +
  labs(title = "Histogram and Density of S&P 500 Prices",
       x = "S&P 500 Close Price",
       y = "Density") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Histogram for Bitcoin
ggplot(merged_data, aes(x = BTC_Close_Price)) +
  geom_histogram(aes(y = ..density..), bins = 30, fill = "blue", alpha = 0.7) +
  geom_density(color = "red", size = 1) +
  labs(title = "Histogram and Density of Bitcoin Prices",
       x = "Bitcoin Close Price",
       y = "Density") +
  theme_minimal()

# Q-Q plot for S&P 500
qqnorm(merged_data$SP500_Close_Price)
qqline(merged_data$SP500_Close_Price, col = "steelblue", lwd = 2)

# Q-Q plot for Bitcoin
qqnorm(merged_data$BTC_Close_Price)
qqline(merged_data$BTC_Close_Price, col = "steelblue", lwd = 2)

```