[英]How to compute correlations between all columns in R and detect highly correlated variables
我有一個包含 100 個變量和 3000 個觀察值的大數據集。 我想檢測那些高度相關或冗余的變量(列),因此刪除數據框中的維度。 我試過這個,但它只計算一列和其他列之間的相關性; 我總是收到一條錯誤消息
for(i in 1:ncol(predicteurs)){
correlations <- cor(predicteurs[,i],predicteurs[,2])
names(correlations[which.max(abs(correlations))])
}
Warning messages:
1: In cor(predicteurs[, i], predicteurs[, 2]) :
the standard deviation is zero
2: In cor(predicteurs[, i], predicteurs[, 2]) :
the standard deviation is zero
誰能幫我?
我會嘗試收集相關矩陣。
# install.packages(c('tibble', 'dplyr', 'tidyr'))
library(tibble)
library(dplyr)
library(tidyr)
d <- data.frame(x1=rnorm(10),
x2=rnorm(10),
x3=rnorm(10))
d2 <- d %>%
as.matrix %>%
cor %>%
as.data.frame %>%
rownames_to_column(var = 'var1') %>%
gather(var2, value, -var1)
var1 var2 value
1 x1 x1 1.00000000
2 x1 x2 -0.05936703
3 x1 x3 -0.37479619
4 x2 x1 -0.05936703
5 x2 x2 1.00000000
6 x2 x3 0.43716004
7 x3 x1 -0.37479619
8 x3 x2 0.43716004
9 x3 x3 1.00000000
# .5 is an arbitrary number
filter(d2, value > .5)
# remove duplicates
d2 %>%
mutate(var_order = paste(var1, var2) %>%
strsplit(split = ' ') %>%
map_chr( ~ sort(.x) %>%
paste(collapse = ' '))) %>%
mutate(cnt = 1) %>%
group_by(var_order) %>%
mutate(cumsum = cumsum(cnt)) %>%
filter(cumsum != 2) %>%
ungroup %>%
select(-var_order, -cnt, -cumsum)
var1 var2 value
1 x1 x1 1
2 x1 x2 -0.0594
3 x1 x3 -0.375
4 x2 x2 1
5 x2 x3 0.437
6 x3 x3 1
另一種看起來有效的方法可能是:
set.seed(101)
mat = matrix(runif(12), 3)
cor_mat = cor(mat)
cor_mat
# [,1] [,2] [,3] [,4]
#[1,] 1.0000000 0.1050075 0.9159599 -0.5108936
#[2,] 0.1050075 1.0000000 0.4952340 -0.9085390
#[3,] 0.9159599 0.4952340 1.0000000 -0.8129071
#[4,] -0.5108936 -0.9085390 -0.8129071 1.0000000
which(cor_mat > 0.15 & lower.tri(cor_mat), arr.ind = T, useNames = F)
# [,1] [,2]
#[1,] 3 1
#[2,] 3 2
我有同樣的問題,這是我解決它的方法:
install.packages("Hmisc") # Only run on first use
library(Hmisc)
rawdata <- read.csv("/path/to/your/datafile", sep="\t", stringsAsFactors=FALSE) # In my case the separator in the file was "\t", adjust accordingly.
ccs <- as.matrix(rawdata)
rcorr(ccs, type="pearson") # You can also use "spearman"
這與其他方法相比具有優勢,它將輸出您的相關值和相應的p值。
您可以使用corrr包。 例如:
corrr::correlate(your_data, method = "pearson")
這是tidyverse
的另一個tidyverse
- 類似於@maloneypatr's ,但步驟更少。
library(tidyverse)
## reproducible example data
set.seed(20211202)
df_data <- map_dfc(1:5, rnorm, n=10)
df_data %>%
## convert to correlation matrix
as.matrix %>% cor %>%
## set redundant to `NA`
`[<-`(lower.tri(., TRUE), NA) %>%
## back to tibble
as_tibble(rownames="var1") %>%
## long format, dropping redundant
pivot_longer(cols=-1, names_to="var2", values_to="rho", values_drop_na=TRUE) %>%
## descending sort most correlated pairs
arrange(-abs(rho))
### A tibble: 10 x 3
## var1 var2 rho
## <chr> <chr> <dbl>
## 1 ...1 ...5 0.549
## 2 ...3 ...5 0.538
## 3 ...2 ...5 0.304
## 4 ...2 ...3 0.301
## 5 ...3 ...4 -0.288
## 6 ...4 ...5 0.249
## 7 ...1 ...2 -0.0941
## 8 ...1 ...3 0.0634
## 9 ...2 ...4 -0.0627
##10 ...1 ...4 -0.00761
看起來在知道第 1-3 列后,第 5 列可能不會產生太大的差異。 但是迭代特征選擇可能是更合適的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.