Korrelation Matrix: Cross-Border M&A Analys
CBA-transaktioner inom TMT-sektorn 1997-2023
Den här sidan presenterar en korrelationsmatris som analyserar sambanden mellan variabler i CBA-transaktioner (M&A).
Plottet nedan visualiserar de parvisa korrelationerna mellan numeriska variabler.
Positiva korrelationer visas i blått, negativa korrelationer i rött och neutrala värden i vitt.
Cirklarnas storlek representerar styrkan av korrelationen.
R Script
Nedan är R-koden som används för att skapa korrelationsmatrisen och generera visualiseringen.
Koden använder paketet `ggplot2` för att plotta och `dplyr` för datamanipulation.
library(readr)
library(dplyr)
library(ggplot2)
library(reshape2)
# Load the cleaned dataset
file_path <- "Book1_cleaned.csv"
data <- read_csv(file_path)
# Convert non-numeric columns to factors (if needed)
data <- data %>% mutate(across(where(is.character), as.factor))
# Select only numeric columns
data_numeric <- data %>% select(where(is.numeric))
# Compute correlation matrix
cor_matrix <- cor(data_numeric, use = "pairwise.complete.obs")
# Convert correlation matrix to long format for ggplot
cor_melt <- melt(cor_matrix)
# Clean variable names (replace underscores with spaces)
cor_melt$Var1 <- gsub("_", " ", cor_melt$Var1)
cor_melt$Var2 <- gsub("_", " ", cor_melt$Var2)
# Create a lower triangular mask to match the reference image
cor_melt <- cor_melt[as.numeric(as.factor(cor_melt$Var1)) >= as.numeric(as.factor(cor_melt$Var2)), ]
# Plot correlation matrix with circles (no border and mirrored layout)
heatmap_plot <- ggplot(cor_melt, aes(Var2, Var1, fill = value, size = abs(value))) +
geom_point(shape = 21, stroke = 0) +
scale_fill_gradient2(low = "red", mid = "white", high = "blue", midpoint = 0, limits = c(-1, 1)) +
scale_size(range = c(2, 10)) +
labs(
title = "Correlation Matrix",
subtitle = "Visualization of pairwise correlations between numeric variables",
x = "",
y = ""
) +
theme_minimal(base_size = 14) +
theme(
axis.text.x = element_text(angle = 45, hjust = 1, color = "black", size = 12),
axis.text.y = element_text(color = "black", size = 12),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(hjust = 0.5, face = "bold", size = 18),
plot.subtitle = element_text(hjust = 0.5, size = 14, color = "gray40"),
legend.position = "right",
legend.text = element_text(size = 12),
legend.title = element_blank()
) +
guides(fill = guide_colorbar(title = "Correlation", barwidth = 1, barheight = 15), size = FALSE)
# Save the plot as an image
ggsave("correlation_plot.png", heatmap_plot, width = 10, height = 8, dpi = 300)
# Display plot
print(heatmap_plot)
# Optional: Save correlation matrix to a CSV file
write.csv(cor_matrix, "correlation_matrix.csv")