data <- data.frame(
x1 = c(10, 20, 30, 40, 50, 60, 21, 23, 24, 26),
x2 = c(59, 15, 25, 35, 45, 54, 51, 32, 65, 32),
x3 = c(2, 8, 28, 98, 38, 10, 20, 30, 40, 50),
x4 = c(1, 4, 80, 20, 30, 10, 20, 30, 40, 50),
x5 = c(13, 9, 91, 31, 41, 10, 20, 30, 40, 50)
)
pearson_manual <- function(x, y) {
sum((x - mean(x)) * (y - mean(y))) / ((length(x) - 1) * sd(x) * sd(y))
}
r_manual <- pearson_manual(data$x1, data$x2)
r_builtin <- cor(data$x1, data$x2)
cat("Manual Pearson correlation:", round(r_manual, 4), "\n")
cat("Built-in Pearson correlation:", round(r_builtin, 4), "\n")
if (!require(corrplot)) install.packages("corrplot", dependencies = TRUE)
library(corrplot)
corr_matrix <- cor(data)
corrplot(corr_matrix, method = "color", addCoef.col = "black")
corrplot(corr_matrix, method = "circle", addCoef.col = "black")
corrplot(corr_matrix, method = "square", col = colorRampPalette(c("blue", "white", "red"))(200), addCoef.col = "black")
corrplot(corr_matrix, method = "shade", order = "hclust", addCoef.col = "black", tl.col = "black")
corrplot(corr_matrix, method = "pie", addCoef.col = "black")