-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unique Correlation Pairs.R
30 lines (21 loc) · 1.16 KB
/
Unique Correlation Pairs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pairs_column <- function(x, details = F){ # Unique Correlation values
cor_matrix <- cor(x) # Calculate correlation matrix
# Extract unique pairs and their correlations
cor_pairs <- which(upper.tri(cor_matrix, diag = TRUE), arr.ind = TRUE)
# Put them into one data frame
unique_pairs <- data.frame(Variable1 = rownames(cor_matrix)[cor_pairs[, 1]],
Variable2 = rownames(cor_matrix)[cor_pairs[, 2]],
Correlation = cor_matrix[cor_pairs]
)
# Filter out pairs with correlation equal to 1
filtered_pairs <- unique_pairs[unique_pairs$Correlation != 1, ]
if (isFALSE(details)){ rownames(filtered_pairs) <- seq(nrow(filtered_pairs))
colnames(filtered_pairs) <- c("Security 1", "Security 2", "Correlation")
return(filtered_pairs) } else { # Descriptive Statistics
d <- apply(as.data.frame(filtered_pairs[,3]), 2,
function(x) c(min(x), median(x), max(x), mean(x)))
rownames(d) <- c("Min","Median 50%","Max", "Mean") # Column names
colnames(d) <- "Summary"
d }
}
pairs_column(stock_data, details = F) # Test