Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

r - p-values of correlation coefficients

I am using R and have a question on correlations.

A<-data.frame(A1=c(1,2,3,4,5),B1=c(6,7,8,9,10),C1=c(11,12,13,14,15 ))
B<-data.frame(A2=c(6,7,7,10,11),B2=c(2,1,3,8,11),C2=c(1,5,16,7,8))
cor(A,B)
# ? ? ? ? ? A2 ? ? ? ?B2 ? ? ? C2
# A1 0.9481224 0.9190183 0.459588
# B1 0.9481224 0.9190183 0.459588
# C1 0.9481224 0.9190183 0.459588

I wanted to obtain the p-value for each of the correlation coefficients in the matrix. Is this possible?

I tried using rcorr function from Hmisc package but obtain only a single p-value and not for each correlation.

A <- as.vector(t(A))
B <- as.vector(t(B))
rcorr(A, B)
     x    y
x 1.00 0.13
y 0.13 1.00

n= 15 


P
  x      y     
x        0.6425
y 0.6425       

Similarly, I also tried using "psych" package in R to do this but unable to.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can apply rcorr directly on A and B if you convert them to matrices first :

library(Hmisc)
rcorr(as.matrix(A),as.matrix(B))

Which gives :

     A1   B1   C1   A2   B2   C2
A1 1.00 1.00 1.00 0.95 0.92 0.46
B1 1.00 1.00 1.00 0.95 0.92 0.46
C1 1.00 1.00 1.00 0.95 0.92 0.46
A2 0.95 0.95 0.95 1.00 0.97 0.16
B2 0.92 0.92 0.92 0.97 1.00 0.15
C2 0.46 0.46 0.46 0.16 0.15 1.00

n= 5 


P
   A1     B1     C1     A2     B2     C2    
A1        0.0000 0.0000 0.0141 0.0273 0.4361
B1 0.0000        0.0000 0.0141 0.0273 0.4361
C1 0.0000 0.0000        0.0141 0.0273 0.4361
A2 0.0141 0.0141 0.0141        0.0078 0.7981
B2 0.0273 0.0273 0.0273 0.0078        0.8125
C2 0.4361 0.4361 0.4361 0.7981 0.8125       

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...