我在尝试从相关矩阵中提取数据时遇到了一些困难,我希望提取高于 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) …Run Code Online (Sandbox Code Playgroud)