Chelsea
Chelsea

Reputation: 23

Thresholding a correlation matrix by p-value

I would like to threshold a correlation matrix to keep only values that have a p-value of < 0.01. I've seen this question answered before on StackOverflow, and tried the suggested code, but the structure of the matrix disappears when the threshold is applied.

For example, I have a matrix of a bunch of numbers...

data <- matrix(rnorm(6),100,22)

I then obtain a correlation matrix...

corr_subj_data <-rcorr(data)
corr_matrix <- corr_subj_data$r
p_vals <- corr_subj_data$P

I tried to use numel (matlab package) to copy code I have for this task in Matlab. But, it doesn't seem to work here.

for(idx in numel(p_vals)) {
pval <- P_vals[idx]
    if (pval > 0.01){
        corr_matrix[idx] <- NA
}}

Basically here, I was just trying to go through each p-value in the matrix, and if it was greater than 0.01, replace the corresponding value in the correlation matrix with "NA".

I then looked this up, I found the suggested code below:

corr_matrix[p_vals < 0.01]

This thresholds the correlation matrix correctly, but I lose the 22 by 22 structure I had. It looks like it becomes just a sequence of numbers (unfamiliar with datatypes in R).

If anyone has suggestions of how to retain the structure of a correlation matrix after thresholding, I would appreciate it!

Upvotes: 2

Views: 561

Answers (1)

David Robinson
David Robinson

Reputation: 78600

Use the line:

corr_matrix[p_vals >= 0.01] <- NA

Upvotes: 2

Related Questions