Eliminar variables altamente correlacionadas


Tengo un dataframe enorme 5600 X 6592 y quiero eliminar cualquier variable que se correlacionan entre sí más de 0.99 Sé cómo hacer esto el largo camino, paso a paso, es decir, formando una matriz de correlación, redondeando los valores, eliminando los similares y usar la indexación para obtener mis datos "reducidos" de nuevo.

cor(mydata)
mydata <- round(mydata,2)
mydata <- mydata[,!duplicated (mydata)]
## then do the indexing...

Me gustaría saber si esto se podría hacer en comando corto, o alguna función avanzada. Estoy aprendiendo a hacer uso de las poderosas herramientas en el lenguaje R, que evita tales largos comandos innecesarios

Estaba pensando en algo como

mydata <- mydata[, which(apply(mydata, 2, function(x) !duplicated(round(cor(x),2))))]

Lo siento, sé que el comando anterior no funciona, pero espero poder hacer esto.

Un play-data que se aplica a la pregunta:

mydata <- structure(list(V1 = c(1L, 2L, 5L, 4L, 366L, 65L, 43L, 456L, 876L, 
78L, 687L, 378L, 378L, 34L, 53L, 43L), V2 = c(2L, 2L, 5L, 4L, 
366L, 65L, 43L, 456L, 876L, 78L, 687L, 378L, 378L, 34L, 53L, 
41L), V3 = c(10L, 20L, 10L, 20L, 10L, 20L, 1L, 0L, 1L, 2010L, 
20L, 10L, 10L, 10L, 10L, 10L), V4 = c(2L, 10L, 31L, 2L, 2L, 5L, 
2L, 5L, 1L, 52L, 1L, 2L, 52L, 6L, 2L, 1L), V5 = c(4L, 10L, 31L, 
2L, 2L, 5L, 2L, 5L, 1L, 52L, 1L, 2L, 52L, 6L, 2L, 3L)), .Names = c("V1", 
"V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, 
-16L))

Muchas gracias

Author: Error404, 2013-08-16

3 answers

Estoy seguro de que hay muchas maneras de hacer esto y ciertamente algunas mejores que esto, pero esto debería funcionar. Básicamente acabo de establecer el triángulo superior para ser cero y luego eliminar las filas que tienen valores superiores a 0.99.

> tmp <- cor(data)
> tmp[upper.tri(tmp)] <- 0
> diag(tmp) <- 0
# Above two commands can be replaced with 
# tmp[!lower.tri(tmp)] <- 0
#
> 
> data.new <- data[,!apply(tmp,2,function(x) any(x > 0.99))]
> head(data.new)
   V2 V3 V5
1   2 10  4
2   2 20 10
3   5 10 31
4   4 20  2
5 366 10  2
6  65 20  5
 37
Author: David,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-18 15:44:39

Este es mi código R esto sería útil para usted

library('caret')

df1 = read.csv("stack.csv")

print (df1)

     GA     PN     PC   MBP    GR    AP
1 0.033  6.652  6.681 0.194 0.874 3.177
2 0.034  9.039  6.224 0.194 1.137 3.400
3 0.035 10.936 10.304 1.015 0.911 4.900
4 0.022 10.110  9.603 1.374 0.848 4.566
5 0.035  2.963 17.156 0.599 0.823 9.406
6 0.033 10.872 10.244 1.015 0.574 4.871
7 0.035 21.694 22.389 1.015 0.859 9.259
8 0.035 10.936 10.304 1.015 0.911 4.500


df2 = cor(df1)
hc = findCorrelation(df2, cutoff=0.3) # putt any value as a "cutoff" 
hc = sort(hc)
reduced_Data = df1[,-c(hc)]
print (reduced_Data)

     GA     PN    GR    AP
1 0.033  6.652 0.874 3.177
2 0.034  9.039 1.137 3.400
3 0.035 10.936 0.911 4.900
4 0.022 10.110 0.848 4.566
5 0.035  2.963 0.823 9.406
6 0.033 10.872 0.574 4.871
7 0.035 21.694 0.859 9.259
8 0.035 10.936 0.911 4.500

Y para escribir un dato reducido en un nuevo csv solo use:

write.csv(reduced_Data, file = "outfile.csv", row.names = FALSE)
 23
Author: jax,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-14 07:10:51

@ David Un pequeño cambio en su código lo hace más robusto a correlación negativa , al proporcionar

abs(x) > 0.99 

En lugar de solo

x > 0.99

data.new <- data[,!apply(tmp,2,function(x) any(abs(x) > 0.99))]

Saludos..!!!

 9
Author: Vamshidhar H.K.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-05 10:13:41