Re: [R] cor(X) with P-Value

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Sat 23 Jul 2005 - 20:45:27 EST

   These functions are based on posts to either R-Help or S-News by Gabor Grothendieck and Bill Venables.

# pairwise sample size
# Gabor G - 11/23/2004 R-help List
pn <- function(X){crossprod(!is.na(X))}

cor.prob <- function(X){

# Correlations Below Main Diagonal
# Significance Tests with Pairwise Deletion
# Above Main Diagonal
# Believe part of this came from Bill Venables
pair.SampSize <- pn(X)
above1 <- row(pair.SampSize) < col(pair.SampSize) pair.df <- pair.SampSize[above1] - 2
R <- cor(X, use="pair")
above2 <- row(R) < col(R)
r2 <- R[above2]^2
Fstat <- (r2 * pair.df)/(1 - r2)
R[above2] <- 1 - pf(Fstat, 1, pair.df)
R
}

mydata <- matrix(rnorm(1000), ncol=10)

cor.prob(mydata)

Worik Turei stanton wrote:
> Friends
>
> I am new to R (and statistics) so am struggling a bit.
>
> Briefly...
>
> I am interested in getting the P-Value from cor(X) where X is a matrix.
>
> I have found cor.test.
>
> Verbosely...
>
> I have 4 vectors and can generate the corellation matrix...
>
>
>

>>cor(cbind(X1, X2, X3, X4))

>
> X1 X2 X3 X4
> X1 1.00000000 -0.06190365 -0.156972795 0.182547517
> X2 -0.06190365 1.00000000 0.264352860 0.146750844
> X3 -0.15697279 0.26435286 1.000000000 -0.006380052
> X4 0.18254752 0.14675084 -0.006380052 1.000000000
>
> But I want the P-Values (gives me the significance I belive).
>
>
>>cor.test(X2, X3)

>
>
> Pearson's product-moment correlation
>
> data: X2 and X3
> t = 3.3346, df = 148, p-value = 0.001080
> alternative hypothesis: true correlation is not equal to 0
> 95 percent confidence interval:
> 0.1086963 0.4073565
> sample estimates:
> cor
> 0.2643529
>
> is very cool. Just what I want. But there is too much information for
> too little data.
>
> What I would like to do is some thing like...
>
> cor_with_p_test(cbind(X1, X2, X3, X4))
> X1 X2 X3
> X1 1 -0.06190365 -0.1569728...
> P 0.4517 0.05507 ...
>
> X2 -0.06190365 1 0.2643529 ...
> P 0.4517 0.001080 ...
> :
> :
>
> I think I could write a function for it if such a function does not exist
> if I could do...
>
>
>>CT23 <- cor.test(X2, X3)
>>CT23$P

>
> 0.001080
>
>>CT23$V

>
> 0.2643529
>
> But I do not know how.
>
> cheers
> Worik
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Jul 23 20:52:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:58 EST