[英]Filter correlation matrix R
我有點想從相關矩陣中提取數據,我想提取高於0.8且低於0.99的值,因為我想排除正好為1的兩只股票的相關性。
這是我的代碼:
#Test
#load the packages
library(corrr)
library(ggplot2)
library(ggcorrplot)
library(dplyr)
library(quantmod)
#get the data needed
startdate <- "2001-01-03"
tickers <- c("MMM", "AA", "AXP", "T", "BAC")
portfolioprices <- NULL
for(ticker in tickers)
portfolioprices <- cbind(portfolioprices, getSymbols(ticker, from=startdate, auto.assign=F)[,4])
colnames(portfolioprices) <- tickers
#check if there is nothing wrong with the data
print(portfolioprices)
#create a correlation matrix and plot it
correlations <- cor(as.matrix(portfolioprices))
correlations <- as.data.frame(correlations)
correlations
ggcorrplot(correlations, hc.order = TRUE, type = "lower",
lab = TRUE)
作為輸出,我得到:
MMM AA AXP T BAC
MMM 1.0000000 -0.40325223 0.8772498 0.39019025 -0.2406640
AA -0.4032522 1.00000000 -0.3029517 0.06347736 0.8383226
AXP 0.8772498 -0.30295171 1.0000000 0.41189453 -0.1304659
T 0.3901902 0.06347736 0.4118945 1.00000000 -0.1297723
BAC -0.2406640 0.83832262 -0.1304659 -0.12977234 1.0000000
在理想情況下,這是數據框,我將提取與最小值0.8正相關的數據。
我不知道我是否會完全以錯誤的方式進行操作,歡迎任何反饋!
編輯:
理想情況下,我希望數據像這樣出來:
MMM AA AXP T BAC
MMM 0.8772498
AA 0.8383226
AXP 0.8772498
T
BAC 0.83832262
僅過濾相關正值的地方。 刪除不相同的值。
MMM:AXP = 0.8772498 BAC:AA = 0.8382262
如果有可能的話。
提前非常感謝您!
加載數據,使其他人輕松重現結果:
dat <- structure(list(MMM = c(1, -0.4032522, 0.8772498, 0.3901902, -0.240664
), AA = c(-0.40325223, 1, -0.30295171, 0.06347736, 0.83832262
), AXP = c(0.8772498, -0.3029517, 1, 0.4118945, -0.1304659),
T = c(0.39019025, 0.06347736, 0.41189453, 1, -0.12977234),
BAC = c(-0.240664, 0.8383226, -0.1304659, -0.1297723, 1)),
.Names = c("MMM", "AA", "AXP", "T", "BAC"),
class = "data.frame",
row.names = c("MMM", "AA", "AXP", "T", "BAC"))
現在只需獲取索引並在矩陣名稱上使用子集即可。
index <- which(abs(dat) > .80 & abs(dat) < 1, # your criteria
arr.ind = T) # the result of the which function is now in rows & columns
cbind.data.frame(stock1 = rownames(dat)[index[,1]], # get the row name
stock2 = colnames(dat)[index[,2]]) # get the column name
# stock1 stock2
#1 AXP MMM
#2 BAC AA
#3 MMM AXP
#4 AA BAC
**我假設您需要較高的絕對相關性(以確保可預測性),但是如果您只希望股票朝同一方向串聯移動 ,則只需刪除abs
功能即可。
只需在代碼末尾添加此行
correlations[correlations < 0.8 | correlations ==1] <- ""
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.