滤波相关矩阵R [英] Filter correlation matrix R

查看:30
本文介绍了滤波相关矩阵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 

其中只会过滤相关的正值。 正在删除不同的值。

MM:axp=0.8772498 BAC:AA=0.8382262

如果这是可能的话。

提前谢谢您!

推荐答案

只需在代码末尾添加此行

correlations[correlations < 0.8 | correlations ==1] <- ""

希望能有所帮助!

这篇关于滤波相关矩阵R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆