使用选择标准减少高度相关的变量

Reducing higly correlating variables with selection criterion

我有一个很大的不一致的数据集,其中有一堆高度相关的变量。我想要的是减少相关变量高于 0.7 阈值的数量。但是,我希望 selected/remaining 变量成为与预定义变量相关性最强的变量。例如,x 作为所选变量,以下相关矩阵:

     x      y      z      m 
 x 1      0.1    0.2    0.3
 y 0.1    1      0.9    0.11   
 z 0.2    0.9    1      0.6
 m 0.3    0.11   0.60   1

应减少为:

     x      z     m 
 x 1      0.2   0.3
 z 0.2      1   0.6
 m 0.3   0.60     1

因为 zy 超过 0.7 阈值并且 z 与 [= 的相关性更强18=]x 比 y.

如果您的 selected 列是 x,查找低于某个点的值:

low = which(df[,1]<0.0.4) 

和select其余rows/cols使用

test[-low,-low]

笨拙,但似乎有效。

# Define matrix
mat <- matrix(c(1,0.1,0.2,0.3,0.1,1,0.9,0.11,0.2,0.9,1,0.6,0.3,0.11,0.60,1), ncol = 4)

# Add names
row.names(mat) <- colnames(mat) <- c("x", "y", "z", "m")

# Specify threshold
threshold <- 0.7

# Selected variable
i <- "x"

# Get column number of selected variable
i <- which(colnames(mat) == i)

# Find element above threshold
above.threshold <- matrix(which(abs(mat) > threshold & mat != 1, arr.ind = TRUE), ncol = 2)

# Remove duplicates
above.threshold <- above.threshold[above.threshold[,1]>above.threshold[,2],,drop = FALSE] 

# Variables to remove
var.rm <- apply(above.threshold, MAR = 1, function(foo)names(which.min(mat[i,foo])))

# New matrix
mat <- mat[!(rownames(mat) %in% var.rm), !(colnames(mat) %in% var.rm)]

产生,

    x   z   m
x 1.0 0.2 0.3
z 0.2 1.0 0.6
m 0.3 0.6 1.0

如果阈值设置为 0.5,它会生成,

    x   m
x 1.0 0.3
m 0.3 1.0