[R] use logical in cor.test

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Mar 30 07:00:16 CEST 2010


What you have done is calculate correlations between two binary variables.  You say you wanted to calculate the correlation between two truncated variables.  One way to do this here is to make a temporary copy setting the excluded values to missing, e.g.:

> tBoth <- Both
> is.na(tBoth[tBoth > 2.5]) <- TRUE
> R <- cor(tBoth, use = "complete.obs")
> R[1,2]
>
> cor.test(tBoth[,1], tBoth[,2])

(Estimating correlations from a truncated distribution in this naive fashion seems a very odd thing to do, though...)


Bill Venables
CSIRO/CMIS Cleveland Laboratories


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of pgseye
Sent: Tuesday, 30 March 2010 2:41 PM
To: r-help at r-project.org
Subject: [R] use logical in cor.test


Hi,

I've got 4 variables that I want to effectively 'stack' so that I have a
grand R variable and a grand L variable.

This works to achieve that goal:

Twin1cor<-with(twin.wide,cbind(ACDepthR.1,ACDepthL.1))
Twin2cor<-with(twin.wide,cbind(ACDepthR.2,ACDepthL.2))
Both<-rbind(Twin1cor,Twin2cor)

> str(Both)
 num [1:1858, 1:2] 3.36 NA NA NA NA NA NA 3.92 3.5 NA ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "ACDepthR.1" "ACDepthL.1"


I then want to perform a pearson correlation of the two variables, but
exclude any value greater than 2.5. When I do this without excluding
anything, with:

cor.test(Both[,1],Both[,2])

I get:

	Pearson's product-moment correlation

data:  Both[, 1] and Both[, 2] 
t = 35.848, df = 854, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0 
95 percent confidence interval:
 0.7468716 0.8005199 
sample estimates:
      cor 
0.7750889 

But when I try:

cor.test(Both[,1]>2.5,Both[,2]>2.5)

I get:

	Pearson's product-moment correlation

data:  Both[, 1] > 2.5 and Both[, 2] > 2.5 
t = 13.3192, df = 854, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0 
95 percent confidence interval:
 0.3576600 0.4687099 
sample estimates:
     cor 
0.414728 

I'm not sure why. I know the correlation should improve from the plot by
excluding those under 2.5, but it decreases. Have I done something wrong
here?

Thanks,

Paul
-- 
View this message in context: http://n4.nabble.com/use-logical-in-cor-test-tp1744701p1744701.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list