0
votes

I am trying to find several info between each correlation

corr.test

I have two data set df1 and df2

df1<- structure(list(col1A = c(1.64, 0.03, 0, 4.202, 2.981, 0.055, 
0, 0.002, 0.005, 0, 0.002, 0.649, 2.55, 2.762, 6.402), col2A = c(2.635, 
0.019, 0, 5.638, 3.542, 0.793, 0.259, 0, 0.046, 0.004, 0.017, 
0.971, 3.81, 3.104, 5.849), col3A = c(0.91, 0.037, 0, 5.757, 
3.916, 0.022, 0, 0, 0.003, 0, 0.262, 0.136, 2.874, 3.466, 5.003
), col4A = c(1.027, 0.021, 0, 4.697, 2.832, 0.038, 0.032, 0.001, 
0.003, 0, 0, 0.317, 2.743, 3.187, 6.455)), class = "data.frame", row.names = c(NA, 
-15L))

the second data is like below

 df2<-structure(list(col1 = c(2.172, 0, 0, 4.353, 4.581, 0.001, 0.027, 
0, 0.002, 0, 0, 0.087, 2.129, 4.317, 5.849), col2 = c(2.093, 
0, 0, 4.235, 3.166, 0, 0, 0.006, 0.01, 0, 0, 0.475, 0, 2.62, 
5.364), col3 = c(3.322, 0, 0, 4.332, 4.018, 0.049, 0.169, 0.004, 
0.02, 0, 0.032, 1.354, 2.944, 4.323, 5.44), col4 = c(0.928, 0.018, 
0, 3.943, 3.723, 0.02, 0, 0, 0, 0, 0.075, 0.136, 3.982, 3.875, 
5.83)), row.names = c("A", "AA", "AAA", "Aab", "buy", "yuyn", 
"gff", "fgd", "kil", "lilk", "hhk", "lolo", "fdd", "vgfh", "nghg"
), class = "data.frame")

I want to obtain all possible correlation between the two and extract all p values and adjusted p values

I use

library(psych)
corr.test(df1,df2, use = "pairwise",method="pearson",adjust="holm",alpha=.05,ci=TRUE,minlength=5)

it does not give me any p value. also I cannot control any sort of permutation to calculate the adjusted p value.

I was thinking to use the following

x <-df1[,1]
y <-df2[,2] 
corr_init <- cor(x,y) # original correlation
N <- 1000 # initialize number of permutations
count <- 0 # counts correlation greater than corr_init
for (i in 1:N) {
y_perm <- permute(y)
  if (cor(y_perm,x) > corr_init) count <- count+1
  }
p <- count/N #final p

but then I have do it one by one and still I need to extract each column and test ...

I am wondering if there is better way to calculate all correlation between two data, get R values, p values and P adjusted with specific number of randomization ?

1

1 Answers

0
votes

It could be done using the Hmisc package:

library(Hmisc)

df1_cor_matrix <- rcorr(as.matrix(df1), type = "pearson")
df2_cor_matrix <- rcorr(as.matrix(df2), type = "pearson")

You can then extract out the coefficients using the following:

df1_coef <- df1_cor_matrix$r
df2_coef <- df2_cor_matrix$r

You can extract the p-values using the following:

df1_p_values <- df1_cor_matrix$P
df2_p_values <- df2_cor_matrix$P

You could get the adjusted p-values using the rcorr.adjust function:

rcorr.adjust(df1_cor_matrix, type = "pearson")
rcorr.adjust(df2_cor_matrix, type = "pearson")