Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to extract confidence interval from cor.test function

Im trying to extract 95 percent confidence interval from the result of pearson correlation.

My output looks like this:

Pearson's product-moment correlation

data:  newX[, i] and newY
t = 2.1253, df = 6810, p-value = 0.0336
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.001998576 0.049462864
sample estimates:
       cor 
0.02574523 

I get it with the following code

t <- apply(FNR[, -1], 2, cor.test, FNR$HDL, method="pearson")

I would appreciate any help. Thanks.

like image 360
user6108949 Avatar asked Oct 26 '25 19:10

user6108949


1 Answers

cor.test returns a list with various elements, including the confidence interval. You can see the structure of the object returned by cor.test as follows (using the built-in mtcars data frame for illustration):

ct = cor.test(mtcars$mpg, mtcars$wt, method="pearson")

str(ct)
List of 9
 $ statistic  : Named num -9.56
  ..- attr(*, "names")= chr "t"
 $ parameter  : Named int 30
  ..- attr(*, "names")= chr "df"
 $ p.value    : num 1.29e-10
 $ estimate   : Named num -0.868
  ..- attr(*, "names")= chr "cor"
 $ null.value : Named num 0
  ..- attr(*, "names")= chr "correlation"
 $ alternative: chr "two.sided"
 $ method     : chr "Pearson's product-moment correlation"
 $ data.name  : chr "mtcars$mpg and mtcars$wt"
 $ conf.int   : atomic [1:2] -0.934 -0.744
  ..- attr(*, "conf.level")= num 0.95
 - attr(*, "class")= chr "htest"

Now extract the confidence interval:

ct$conf.int[1:2]   

[1] -0.9338264 -0.7440872

like image 139
eipi10 Avatar answered Oct 29 '25 08:10

eipi10